1.66k likes | 1.88k Views
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.
E N D
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 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.
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 ...
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>;
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
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
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;
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;
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
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
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;
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
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
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
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
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
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
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
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)
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 ...
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
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)
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
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
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)
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
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
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
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
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
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
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
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 ...
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.
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
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?
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.
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
Exercise • This exercise reinforces the concepts discussed previously.
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.
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;
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
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.
Exercise – Section 9.1 • This exercise reinforces the concepts discussed previously.
Section 9.2 Writing to Multiple SAS Data Sets
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.
Creating Multiple Data Sets • We have only created one data set in a DATA step. • We can create multiple data sets, not just one.