14. GeneraGng data with DO loops

Transcription

14. GeneraGng data with DO loops
14. Genera)ng data with DO loops GIORGIO RUSSOLILLO -­‐ Cours de prépara)on à la cer)fica)on SAS «Base Programming» 322 DO loops DO loops process a group of statements repeatedly rather than once in a DATA step. DO index-­‐variable = start TO stop <BY increment>; SAS statements; END; -­‐ 
-­‐ 
-­‐ 
-­‐ 
index-­‐variable stores the value of the current itera)on of the DO loop start specifies the ini)al value of the index-­‐variable stop is the last index-­‐variable value that executes the DO loop increment (default = 1) is the increment/decrement value for the index-­‐
variable. It can take posi)ve or nega)ve values: in the last case start must be greater than stop. -­‐  start, stop and increment can be numbers, variables or SAS expressions, and cannot be changed within the loop GIORGIO RUSSOLILLO -­‐ Cours de prépara)on à la cer)fica)on SAS «Base Programming» 323 Some examples about using start and stop GIORGIO RUSSOLILLO -­‐ Cours de prépara)on à la cer)fica)on SAS «Base Programming» 324 An example I invest each month 100 dollars in an account that annually earns 6% interest compounded monthly; I want to know how many dollars I will have a|er 1 year DATA invest; Capital+(100+(Capital*0.06/12)) ; Capital+(100+(Capital*0.06/12)) ; Capital+(100+(Capital*0.06/12)) ; Capital+(100+(Capital*0.06/12)) ; Capital+(100+(Capital*0.06/12)) ; Capital+(100+(Capital*0.06/12)) ; Capital+(100+(Capital*0.06/12)) ; Capital+(100+(Capital*0.06/12)) ; Capital+(100+(Capital*0.06/12)) ; Capital+(100+(Capital*0.06/12)) ; Capital+(100+(Capital*0.06/12)) ; Capital+(100+(Capital*0.06/12)) ; RUN; PROC PRINT DATA=Invest; RUN; DATA Invest; DO month=1 TO 12; Capital+(100+(Capital*0.06/12)); END; RUN; PROC PRINT DATA=Invest; RUN; GIORGIO RUSSOLILLO -­‐ Cours de prépara)on à la cer)fica)on SAS «Base Programming» 325 How SAS processes DO loops From Jan1 2008 I invest each year 5000 dollars in an account that annually earns 4.5% interest 3 years I want to calculate the value of the account a|er 3 years DATA invest; DO year=2008 TO 2010; Capital+5000; Capital+(Capital*0.045); END; RUN; PROC PRINT DATA=Invest; RUN; GIORGIO RUSSOLILLO -­‐ Cours de prépara)on à la cer)fica)on SAS «Base Programming» 326 Specifying a series of items You can specify the values of the index-­‐variable also as a list DO index-­‐variable=value1, value2, value3, ..; SAS statements; END; To list items in a series, you must specify either -­‐  All numeric values (e.g.: index-­‐variable = 2 ,5,8, 10) -­‐  All character values (e.g.: index-­‐variable = ‘mon’, ‘tue, ‘wen’, ‘thr’, ‘fri’, ‘sat’, ‘sun’) -­‐  All variable names (e.g.: index-­‐variable = varname1, varname2, varname3) DATA invest; DO year=2008, 2009, 2010; Capital+5000; Capital+(Capital*0.045); END; RUN; PROC PRINT DATA=Invest; RUN; GIORGIO RUSSOLILLO -­‐ Cours de prépara)on à la cer)fica)on SAS «Base Programming» 327 How the loop is executed GIORGIO RUSSOLILLO -­‐ Cours de prépara)on à la cer)fica)on SAS «Base Programming» DATA invest; DO year=2008 TO 2010; Capital+5000; Capital+(Capital*0.045); END; RUN; 328 Crea)ng an observa)on for each itera)on of the DO loop You can create an observa)on for each itera)on by introducing an explicit output in the loop Example: I want to follow the increment of my bank account each year DATA invest; DO year=2008 TO 2010; Capital+5000; Capital+(Capital*0.045); OUTPUT; END; OUTPUT; RUN; PROC PRINT DATA=Invest; RUN; GIORGIO RUSSOLILLO -­‐ Cours de prépara)on à la cer)fica)on SAS «Base Programming» 329 Nested loops I invest 5000 dollars each year in an account that annually earns 4.5% interest compounded quarterly. I want to know the value of my account each 3 months for the next 5 years DATA invest; DO year=1 TO 5; Capital+5000; DO quarter=1 TO 4; Capital+(Capital*0.45/4); OUTPUT; END; END; RUN; PROC PRINT DATA=Invest; RUN; Remember to: -­‐  Assing an unique index-­‐variable in each itera)ve do statement -­‐  End each DO loop with an END statement GIORGIO RUSSOLILLO -­‐ Cours de prépara)on à la cer)fica)on SAS «Base Programming» 330 Genera)ng values for observa)ons for each itera)on of the DATA step How much does each bank make me earn? DATA invest (DROP=quarter year); SET Lib9_3.bankRates; Capital=0; DO year=1 TO 5; Capital+5000; DO quarter=1 TO 4; Capital+(Capital*Rates/4); END; END; RUN; PROC PRINT DATA=Invest; RUN; This DATA step iterates 3 )mes (once for each observa)on), and returns the 3 values of the new variable Capital GIORGIO RUSSOLILLO -­‐ Cours de prépara)on à la cer)fica)on SAS «Base Programming» 331 Condi)onally execu)ng DO loops How many years it will take to earn one million dollars if I deposit $5000 each year into an account that earns 4.5% interest? DO WHILE (expression); DO UNTIL (expression); SAS statements; SAS statements; END; END; -­‐  The loop is executed unAl the expression becomes true -­‐  The expression is evaluated at the bokom of the loop -­‐  -­‐> It always executes at least once DATA invest; DO UNTIL (Capital > 1000000); Year +1; Capital+5000; Capital+(Capital*0.045); END; RUN; PROC PRINT DATA=Invest; RUN; -­‐  The loop is executed while the expression is true -­‐  The expression is evaluated at the top of the loop DATA invest; DO WHILE (Capital <= 1000000); Year +1; Capital+5000; Capital+(Capital*0.045); END; RUN; PROC PRINT DATA=Invest; RUN; GIORGIO RUSSOLILLO -­‐ Cours de prépara)on à la cer)fica)on SAS «Base Programming» 332 Combining itera)ve loops and condi)onal clauses DO index-­‐variable=start TO stop <BY increment> UNTIL | WHILE (expression); This statement executes the DO loop unAl (while) the expression becomes (is) true or un)l the value of index-­‐variable exceeds stop DATA invest; DO year=1 TO 60 UNTIL (Capital > 1000000); Capital+5000; DATA invest; Capital+(Capital*0.045); DO year=1 TO 60 WHILE (Capital <= 1000000); END; Capital+5000; RUN; Capital+(Capital*0.045); PROC PRINT DATA=Invest; END; RUN; RUN; PROC PRINT DATA=Invest; RUN; GIORGIO RUSSOLILLO -­‐ Cours de prépara)on à la cer)fica)on SAS «Base Programming» 333 Crea)ng samples You can create a sample dataset by enclosing the SET stamentent in a DO loop. For example you can select every tenth observa)on of the 500 observa)ons of a dataset called Giorgio and put them in a new dataset called Sample with the following program: DATA Giorgio; DO UNTIL (Count = 500); Count+1; OUTPUT; END; RUN; PROC PRINT DATA=Giorgio; RUN; (With this code a new dataset called Giorgio is created. In Giorgio there is just a variable called Count which contains integers from 1 to 500) DATA Sample; DO i=10 TO 500 BY 10; SET Giorgio POINT=i; OUTPUT; END; STOP; RUN; PROC PRINT DATA=Sample; RUN; GIORGIO RUSSOLILLO -­‐ Cours de prépara)on à la cer)fica)on SAS «Base Programming» 334 

Documents pareils

1. Base Programming

1. Base Programming -  A step begins by DATA or PROC statements -  A step ends by RUN or QUIT statement (although the RUN statement is not always required between steps, using it can make the SAS program easier to r...

Plus en détail