1 / 165

Chapter 9

Chapter 9. Controlling Input and Output. Section 9.1. Outputting Multiple Observations. Objectives. Explicitly control the output of multiple observations to a SAS data set. SAS Vocabulary. OUTPUT. Taking Control of Output.

Télécharger la présentation

Chapter 9

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 9 Controlling Input and Output

  2. Section 9.1 Outputting Multiple Observations

  3. Objectives • Explicitly control the output of multiple observations to a SAS data set.

  4. SAS Vocabulary • OUTPUT

  5. Taking Control of Output The DATA step controls the output to the data sets that we create. Automatic output to the data set occurs at the RUN statement, or the end of the DATA step.

  6. Automatic Output (Review) • When one observation is read from prog2.growth, one observationis written to forecast. data forecast; set prog2.growth; <additional SAS statements>; run; 2. Automatic return 1. Automatic output ...

  7. Remember The OUTPUT Statement • We can take control of the output by using the OUTPUT statement. SAS will write the current contents of the PDV to a SAS data set when it executes the OUTPUT statement. • The OUTPUT statement in a DATA step overrides the automatic output. SAS will not output to the data set at the RUN statement if there is at least one OUTPUT statement in the DATA step. OUTPUT <SAS-data-set-1 …SAS-data-set-n>;

  8. Example - A Forecasting Application • The growth rate of each division of an airline is forecast in prog2.growth. • If each of the five divisions grows at its respective rate for the next three years, what will be the approximate size of each division at the end of each of the three years? Num Division Emps Increase APTOPS 205 0.075 FINACE 198 0.040 FLTOPS 187 0.080 Partial listing of prog2.growth

  9. A Forecasting Application • The output SAS data set, forecast, should contain 15 observations. Each row read into the DATA step should produce 3 observations. • Partial Listing of forecast New Division Increase Year Total APTOPS 0.075 1 220.38 APTOPS 0.075 2 236.90 APTOPS 0.075 3 254.67 FINACE 0.040 1 205.92 FINACE 0.040 2 214.16

  10. A Forecasting Application data forecast; drop NumEmps; set prog2.growth; Year=1; NewTotal=NumEmps*(1+Increase); output forecast; Year=2; NewTotal=NewTotal*(1+Increase); output forecast; Year=3; NewTotal=NewTotal*(1+Increase); output forecast; run;

  11. A Forecasting Application The OUTPUT statement writes the information in the PDV to the new SAS data set. data forecast; drop NumEmps; set prog2.growth; Year=1; NewTotal=NumEmps*(1+Increase); output; Year=2; NewTotal=NewTotal*(1+Increase); output; Year=3; NewTotal=NewTotal*(1+Increase); output; run;

  12. Partial Listing ofprog2.growth Compile Num Division Emps Increase APTOPS 205 0.075 FINACE 198 0.040 FLTOPS 187 0.080 A Forecasting Application data forecast; drop NumEmps; set prog2.growth; Year=1; NewTotal=NumEmps*(1+Increase); output; Year=2; NewTotal=NewTotal*(1+Increase); output; Year=3; NewTotal=NewTotal*(1+Increase); output; run; PDV

  13. Partial Listing ofprog2.growth Num Division Emps Increase APTOPS 205 0.075 FINACE 198 0.040 FLTOPS 187 0.080 DIVISION $ 6 NUMEMPS N 8 INCREASE N 8 data forecast; drop NumEmps; set prog2.growth; Year=1; NewTotal=NumEmps*(1+Increase); output; Year=2; NewTotal=NewTotal*(1+Increase); output; Year=3; NewTotal=NewTotal*(1+Increase); output; run; PDV

  14. Partial Listing ofprog2.growth Num Division Emps Increase APTOPS 205 0.075 FINACE 198 0.040 FLTOPS 187 0.080 PDV YEAR N 8 DIVISION $ 6 NUMEMPS N 8 INCREASE N 8 data forecast; drop NumEmps; set prog2.growth; Year=1; NewTotal=NumEmps*(1+Increase); output; Year=2; NewTotal=NewTotal*(1+Increase); output; Year=3; NewTotal=NewTotal*(1+Increase); output; run;

  15. Partial Listing ofprog2.growth Num Division Emps Increase APTOPS 205 0.075 FINACE 198 0.040 FLTOPS 187 0.080 YEAR N 8 NEWTOTAL N 8 DIVISION $ 6 NUMEMPS N 8 INCREASE N 8 data forecast; drop NumEmps; set prog2.growth; Year=1; NewTotal=NumEmps*(1+Increase); output; Year=2; NewTotal=NewTotal*(1+Increase); output; Year=3; NewTotal=NewTotal*(1+Increase); output; run; PDV

  16. Partial Listing ofprog2.growth Num Division Emps Increase APTOPS 205 0.075 FINACE 198 0.040 FLTOPS 187 0.080 YEAR N 8 NEWTOTAL N 8 DIVISION $ 6 NUMEMPS N 8 INCREASE N 8 data forecast; drop NumEmps; set prog2.growth; Year=1; NewTotal=NumEmps*(1+Increase); output; Year=2; NewTotal=NewTotal*(1+Increase); output; Year=3; NewTotal=NewTotal*(1+Increase); output; run; PDV D

  17. Partial Listing ofprog2.growth Execute Num Division Emps Increase APTOPS 205 0.075 FINACE 198 0.040 FLTOPS 187 0.080 YEAR N 8 NEWTOTAL N 8 DIVISION $ 6 NUMEMPS N 8 INCREASE N 8 D data forecast; drop NumEmps; set prog2.growth; Year=1; NewTotal=NumEmps*(1+Increase); output; Year=2; NewTotal=NewTotal*(1+Increase); output; Year=3; NewTotal=NewTotal*(1+Increase); output; run; Partial Listing offorecast New Division Year Total PDV

  18. Partial Listing ofprog2.growth Num Division Emps Increase APTOPS 205 0.075 FINACE 198 0.040 FLTOPS 187 0.080 NEWTOTAL N 8 DIVISION $ 6 NUMEMPS N 8 INCREASE N 8 YEAR N 8 D . . . . data forecast; drop NumEmps; set prog2.growth; Year=1; NewTotal=NumEmps*(1+Increase); output; Year=2; NewTotal=NewTotal*(1+Increase); output; Year=3; NewTotal=NewTotal*(1+Increase); output; run; Partial Listing offorecast New Division Year Total Initialize PDV to missing PDV

  19. Partial Listing ofprog2.growth Num Division Emps Increase APTOPS 205 0.075 FINACE 198 0.040 FLTOPS 187 0.080 Partial Listing offorecast New Division Year Total NEWTOTAL N 8 DIVISION $ 6 NUMEMPS N 8 INCREASE N 8 YEAR N 8 D . APTOPS 205 0.075 . data forecast; drop NumEmps; set prog2.growth; Year=1; NewTotal=NumEmps*(1+Increase); output; Year=2; NewTotal=NewTotal*(1+Increase); output; Year=3; NewTotal=NewTotal*(1+Increase); output; run; PDV

  20. Partial Listing ofprog2.growth Num Division Emps Increase APTOPS 205 0.075 FINACE 198 0.040 FLTOPS 187 0.080 Partial Listing offorecast New Division Year Total NEWTOTAL N 8 DIVISION $ 6 NUMEMPS N 8 INCREASE N 8 YEAR N 8 D . APTOPS 205 0.075 . data forecast; drop NumEmps; set prog2.growth; Year=1; NewTotal=NumEmps*(1+Increase); output; Year=2; NewTotal=NewTotal*(1+Increase); output; Year=3; NewTotal=NewTotal*(1+Increase); output; run; PDV

  21. Partial Listing ofprog2.growth Num Division Emps Increase APTOPS 205 0.075 FINACE 198 0.040 FLTOPS 187 0.080 Partial Listing offorecast New Division Year Total NEWTOTAL N 8 DIVISION $ 6 NUMEMPS N 8 INCREASE N 8 YEAR N 8 D . APTOPS 205 0.075 1 data forecast; drop NumEmps; set prog2.growth; Year=1; NewTotal=NumEmps*(1+Increase); output; Year=2; NewTotal=NewTotal*(1+Increase); output; Year=3; NewTotal=NewTotal*(1+Increase); output; run; PDV

  22. Partial Listing ofprog2.growth Num Division Emps Increase APTOPS 205 0.075 FINACE 198 0.040 FLTOPS 187 0.080 NEWTOTAL N 8 DIVISION $ 6 NUMEMPS N 8 INCREASE N 8 YEAR N 8 D 220.38 APTOPS 205 0.075 1 data forecast; drop NumEmps; set prog2.growth; Year=1; NewTotal=NumEmps*(1+Increase); output; Year=2; NewTotal=NewTotal*(1+Increase); output; Year=3; NewTotal=NewTotal*(1+Increase); output; run; Partial Listing offorecast New Division Year Total PDV 205*(1+0.075)

  23. NEWTOTAL N 8 DIVISION $ 6 NUMEMPS N 8 INCREASE N 8 YEAR N 8 D 220.38 APTOPS 205 0.075 1 Write out first observation to forecast. Partial Listing ofprog2.growth data forecast; drop NumEmps; set prog2.growth; Year=1; NewTotal=NumEmps*(1+Increase); output; Year=2; NewTotal=NewTotal*(1+Increase); output; Year=3; NewTotal=NewTotal*(1+Increase); output; run; Num Division Emps Increase APTOPS 205 0.075 FINACE 198 0.040 FLTOPS 187 0.080 Partial Listing offorecast New Division Year Total APTOPS 1 220.38 PDV ...

  24. Partial Listing ofprog2.growth Num Division Emps Increase APTOPS 205 0.075 FINACE 198 0.040 FLTOPS 187 0.080 NEWTOTAL N 8 DIVISION $ 6 NUMEMPS N 8 INCREASE N 8 YEAR N 8 D 220.38 APTOPS 205 0.075 2 data forecast; drop NumEmps; set prog2.growth; Year=1; NewTotal=NumEmps*(1+Increase); output; Year=2; NewTotal=NewTotal*(1+Increase); output; Year=3; NewTotal=NewTotal*(1+Increase); output; run; Partial Listing offorecast New Division Year Total APTOPS 1 220.38 PDV

  25. Partial Listing ofprog2.growth Num Division Emps Increase APTOPS 205 0.075 FINACE 198 0.040 FLTOPS 187 0.080 Partial Listing offorecast New Division Year Total APTOPS 1 220.38 NEWTOTAL N 8 DIVISION $ 6 NUMEMPS N 8 INCREASE N 8 YEAR N 8 D 236.90 APTOPS 205 0.075 2 data forecast; drop NumEmps; set prog2.growth; Year=1; NewTotal=NumEmps*(1+Increase); output; Year=2; NewTotal=NewTotal*(1+Increase); output; Year=3; NewTotal=NewTotal*(1+Increase); output; run; PDV 220.38*(1+0.075)

  26. Partial Listing ofprog2.growth Num Division Emps Increase APTOPS 205 0.075 FINACE 198 0.040 FLTOPS 187 0.080 Partial Listing offorecast New Division Year Total APTOPS 1 220.38 APTOPS 2 236.90 NEWTOTAL N 8 DIVISION $ 6 NUMEMPS N 8 INCREASE N 8 YEAR N 8 D 236.90 APTOPS 205 0.075 2 Write out second observation to forecast. data forecast; drop NumEmps; set prog2.growth; Year=1; NewTotal=NumEmps*(1+Increase); output; Year=2; NewTotal=NewTotal*(1+Increase); output; Year=3; NewTotal=NewTotal*(1+Increase); output; run; PDV

  27. Partial Listing ofprog2.growth Num Division Emps Increase APTOPS 205 0.075 FINACE 198 0.040 FLTOPS 187 0.080 Partial Listing offorecast New Division Year Total APTOPS 1 220.38 APTOPS 2 236.90 NEWTOTAL N 8 DIVISION $ 6 NUMEMPS N 8 INCREASE N 8 YEAR N 8 D 236.90 APTOPS 205 0.075 3 data forecast; drop NumEmps; set prog2.growth; Year=1; NewTotal=NumEmps*(1+Increase); output; Year=2; NewTotal=NewTotal*(1+Increase); output; Year=3; NewTotal=NewTotal*(1+Increase); output; run; PDV

  28. Partial Listing ofprog2.growth Num Division Emps Increase APTOPS 205 0.075 FINACE 198 0.040 FLTOPS 187 0.080 Partial Listing offorecast New Division Year Total APTOPS 1 220.38 APTOPS 2 236.90 NEWTOTAL N 8 DIVISION $ 6 NUMEMPS N 8 INCREASE N 8 YEAR N 8 D 254.67 APTOPS 205 0.075 3 data forecast; drop NumEmps; set prog2.growth; Year=1; NewTotal=NumEmps*(1+Increase); output; Year=2; NewTotal=NewTotal*(1+Increase); output; Year=3; NewTotal=NewTotal*(1+Increase); output; run; PDV 236.90*(1+0.075)

  29. Partial Listing ofprog2.growth Num Division Emps Increase APTOPS 205 0.075 FINACE 198 0.040 FLTOPS 187 0.080 Partial Listing offorecast New Division Year Total APTOPS 1 220.38 APTOPS 2 236.90 APTOPS 3 254.67 NEWTOTAL N 8 DIVISION $ 6 NUMEMPS N 8 INCREASE N 8 YEAR N 8 D 254.67 APTOPS 205 0.075 3 Write out third observation to forecast. data forecast; drop NumEmps; set prog2.growth; Year=1; NewTotal=NumEmps*(1+Increase); output; Year=2; NewTotal=NewTotal*(1+Increase); output; Year=3; NewTotal=NewTotal*(1+Increase); output; run; PDV

  30. Partial Listing ofprog2.growth Num Division Emps Increase APTOPS 205 0.075 FINACE 198 0.040 FLTOPS 187 0.080 Partial Listing offorecast New Division Year Total APTOPS 1 220.38 APTOPS 2 236.90 APTOPS 3 254.67 NEWTOTAL N 8 DIVISION $ 6 NUMEMPS N 8 INCREASE N 8 YEAR N 8 D 254.67 APTOPS 205 0.075 3 data forecast; drop NumEmps; set prog2.growth; Year=1; NewTotal=NumEmps*(1+Increase); output; Year=2; NewTotal=NewTotal*(1+Increase); output; Year=3; NewTotal=NewTotal*(1+Increase); output; run; No Automatic output PDV

  31. Partial Listing ofprog2.growth Num Division Emps Increase APTOPS 205 0.075 FINACE 198 0.040 FLTOPS 187 0.080 Partial Listing offorecast New Division Year Total APTOPS 1 220.38 APTOPS 2 236.90 APTOPS 3 254.67 NEWTOTAL N 8 DIVISION $ 6 NUMEMPS N 8 INCREASE N 8 YEAR N 8 D 254.67 APTOPS 205 0.075 3 data forecast; drop NumEmps; set prog2.growth; Year=1; NewTotal=NumEmps*(1+Increase); output; Year=2; NewTotal=NewTotal*(1+Increase); output; Year=3; NewTotal=NewTotal*(1+Increase); output; run; Automatic return No Automatic output PDV

  32. Partial Listing ofprog2.growth Num Division Emps Increase APTOPS 205 0.075 FINACE 198 0.040 FLTOPS 187 0.080 Partial Listing offorecast New Division Year Total APTOPS 1 220.38 APTOPS 2 236.90 APTOPS 3 254.67 NEWTOTAL N 8 DIVISION $ 6 NUMEMPS N 8 INCREASE N 8 YEAR N 8 D . APTOPS 205 0.075 . data forecast; drop NumEmps; set prog2.growth; Year=1; NewTotal=NumEmps*(1+Increase); output; Year=2; NewTotal=NewTotal*(1+Increase); output; Year=3; NewTotal=NewTotal*(1+Increase); output; run; PDV

  33. Partial Listing ofprog2.growth Num Division Emps Increase APTOPS 205 0.075 FINACE 198 0.040 FLTOPS 187 0.080 Partial Listing offorecast New Division Year Total APTOPS 1 220.38 APTOPS 2 236.90 APTOPS 3 254.67 NEWTOTAL N 8 DIVISION $ 6 NUMEMPS N 8 INCREASE N 8 YEAR N 8 D . FINACE 198 0.040 . data forecast; drop NumEmps; set prog2.growth; Year=1; NewTotal=NumEmps*(1+Increase); output; Year=2; NewTotal=NewTotal*(1+Increase); output; Year=3; NewTotal=NewTotal*(1+Increase); output; run; PDV

  34. Partial Listing ofprog2.growth Num Division Emps Increase APTOPS 205 0.075 FINACE 198 0.040 FLTOPS 187 0.080 Partial Listing offorecast New Division Year Total APTOPS 1 220.38 APTOPS 2 236.90 APTOPS 3 254.67 NEWTOTAL N 8 DIVISION $ 6 NUMEMPS N 8 INCREASE N 8 YEAR N 8 D . FINACE 198 0.040 . data forecast; drop NumEmps; set prog2.growth; Year=1; NewTotal=NumEmps*(1+Increase); output; Year=2; NewTotal=NewTotal*(1+Increase); output; Year=3; NewTotal=NewTotal*(1+Increase); output; run; PDV

  35. Partial Listing ofprog2.growth Num Division Emps Increase APTOPS 205 0.075 FINACE 198 0.040 FLTOPS 187 0.080 Partial Listing offorecast New Division Year Total APTOPS 1 220.38 APTOPS 2 236.90 APTOPS 3 254.67 NEWTOTAL N 8 DIVISION $ 6 NUMEMPS N 8 INCREASE N 8 YEAR N 8 D . FINACE 198 0.040 1 data forecast; drop NumEmps; set prog2.growth; Year=1; NewTotal=NumEmps*(1+Increase); output; Year=2; NewTotal=NewTotal*(1+Increase); output; Year=3; NewTotal=NewTotal*(1+Increase); output; run; PDV

  36. Partial Listing ofprog2.growth Num Division Emps Increase APTOPS 205 0.075 FINACE 198 0.040 FLTOPS 187 0.080 Partial Listing offorecast New Division Year Total APTOPS 1 220.38 APTOPS 2 236.90 APTOPS 3 254.67 NEWTOTAL N 8 DIVISION $ 6 NUMEMPS N 8 INCREASE N 8 YEAR N 8 D . FINACE 198 0.040 1 data forecast; drop NumEmps; set prog2.growth; Year=1; NewTotal=NumEmps*(1+Increase); output; Year=2; NewTotal=NewTotal*(1+Increase); output; Year=3; NewTotal=NewTotal*(1+Increase); output; run; Continue executing DATA step until all observations fromprog2.growth are read. PDV ...

  37. A Forecasting Application Five rows were read. Each row was output 3 times to give us 15 observations. Partial Log NOTE: There were 5 observations read from the data set PROG2.GROWTH. NOTE: The data set WORK.FORECAST has 15 observations and 4 variables.

  38. A Forecasting Application proc print data=forecast noobs; format NewTotal 6.; run; Partial PROC PRINT Output New Division Increase Year Total APTOPS 0.075 1 220 APTOPS 0.075 2 237 APTOPS 0.075 3 255 FINACE 0.040 1 206 FINACE 0.040 2 214

  39. A Forecasting Application What is the result if we remove the last OUTPUT statement? • Open the file c02s1d1.sas in the PROG2 directory. • Remove the last OUTPUT statement and submit. data forecast; drop NumEmps; set prog2.growth; Year=1; NewTotal=NumEmps*(1+Increase); output; Year=2; NewTotal=NewTotal*(1+Increase); output; Year=3; NewTotal=NewTotal*(1+Increase); run; • What happened?

  40. A Forecasting Application • SAS would not output the last row. Year would get set to 3 and and NumEmps would be calculated, but the row would not be output to the data set. • Why? • There is at least one explicit OUTPUT statement in the DATA step. Automatic output is no longer in effect. • Notice there are no notes, warnings or errors in the SAS Log.

  41. A Forecasting Application proc print data=forecast noobs; format NewTotal 6.; run; Partial PROC PRINT Output New Division Increase Year Total APTOPS 0.075 1 220 APTOPS 0.075 2 237 FINACE 0.040 1 206 FINACE 0.040 2 214 FLTOPS 0.080 1 336

  42. Exercise • This exercise reinforces the concepts discussed previously.

  43. Exercises • Use the schools data set to create a data set called allchoices. • The data set should contain two new variables: • Choice – Value should be either First, Second or Third. • School – The name of the school corresponding to the choice. • Each student should be listed 3 times in the new data set. (One row for each choice.) • Produce 2 reports, suppressing the date and page numbers: • A Listing Report that contains the variables: • StudentID, Name, Choice and School • A Frequency Report analyzing School. Do not show the cumulative frequencies or the percentages.

  44. Exercises - Solution data allchoices; set schools; Choice = 'First '; School = firstchoice; output; Choice = 'Second'; School = secondchoice; output; Choice = 'Third'; School = thirdchoice; output; run; options nodate nonumber; procprintdata=allchoices; var StudentId name choice school; run; procfreq data=allchoices; tables school /nocumnopercent; run;

  45. Exercises - Solution Partial PROC PRINT Output: Student Obs ID Name Choice School 1 1005 Chaz Richardson First University of Connecticut 2 1005 Chaz Richardson Second Wake Forest University 3 1005 Chaz Richardson Third University of Alabama 4 1154 Barbara Muir First Winthrop University 5 1154 Barbara Muir Second University of Central Florida 6 1154 Barbara Muir Third Spelman College 7 1155 Angel Reisman First Howard University 8 1155 Angel Reisman Second Emory University 9 1155 Angel Reisman Third University of Nevada-Las Vegas

  46. Exercises - Solution Partial PROC FREQ Output: School Frequency ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ Appalachian State University 5 Arizona State University 7 Babson College 6 Baylor University 6 Bentley College 5 Boston College 1 Boston University 4 Case Western Reserve University 3 Clemson University 2 Save your program as Create_Schools.sasto be used later.

  47. Exercise – Section 9.1 • This exercise reinforces the concepts discussed previously.

  48. Section 9.2 Writing to Multiple SAS Data Sets

  49. Objectives • Create multiple SAS data sets in a single DATA step. • Use conditional processing to control the data set(s) to which an observation is written.

  50. Creating Multiple Data Sets • We have only created one data set in a DATA step. • We can create multiple data sets, not just one.

More Related