## RUN

compounded monthly; I want to know how many dollars I will have after 1 year ... From Jan1 2008 I invest each year 5000 dollars in an account that annually ...
14. Genera)ng data with DO loops

GIORGIO RUSSOLILLO - Cours de prépara)on à la cer)ﬁca)on SAS «Base Programming»

330

DO loops DO loops process a group of statements repeatedly rather than once in a DATA step. DO index-variable = start TO stop ; SAS statements; END; -  -  -  -

index-variable stores the value of the current itera)on of the DO loop start speciﬁes 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 indexvariable. 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)ﬁca)on SAS «Base Programming»

331

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)ﬁca)on SAS «Base Programming»

332

Another example 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 aZer 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)ﬁca)on SAS «Base Programming»

333

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)ﬁca)on SAS «Base Programming»

334

How SAS processes DO loops

GIORGIO RUSSOLILLO - Cours de prépara)on à la cer)ﬁca)on SAS «Base Programming»

DATA invest; DO year=2008 TO 2010; Capital+5000; Capital+(Capital*0.045); END; RUN;

335

Some examples about using start and stop

GIORGIO RUSSOLILLO - Cours de prépara)on à la cer)ﬁca)on SAS «Base Programming»

336

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)ﬁca)on SAS «Base Programming»

337

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.045/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)ﬁca)on SAS «Base Programming»

338

Genera)ng values for observa)ons for each itera)on of the DATA step Three banks make me annually earn interests compounded quarterly. I invest 5000 dollars each year. I want to know the value of my account aZer 5 years . 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;

How much does each bank make me earn?

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)ﬁca)on SAS «Base Programming»

339

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 unBl the expression becomes true -  The expression is evaluated at the bonom 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)ﬁca)on SAS «Base Programming»

340

Combining itera)ve loops and condi)onal clauses DO index-variable=start TO stop UNTIL | WHILE (expression); This statement executes the DO loop unBl (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)ﬁca)on SAS «Base Programming»

341

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)ﬁca)on SAS «Base Programming»

342