Week 10- Do Loops and Arrays Flashcards
Loops in SAS
The basic iterative due statement in SAS has Syntex
Do value= start TO stop
And END statement marks the end of the loop
DO = 1,2,3,4;
DO = ‘winter’, ‘summer’
General form of an iterative loop
DATA dataset;
DO i=1 to 5; First, SAS puts a value of one to i
Y=i**2; then does action on 1
OUTPUT; output results for 1 and goes back up to 2 OK
END;
Nested DO Loops
Each level one will have one to three rows and one to four columns, then same for level 2
DATA dataset;
DO level = 1 to 2;
DO row = 1 to 3;
DO col = 1 to 4;
INPUT values @@;
END;
END;
END;
RUN;
If – then do statements
The DO statement causes all statements following to be treated as a unit until matching END statement appears. To avoid writing a bunch of IF conditions use a DO group in the THEN clause
IF city = Boston THEN
DO;
Museums = 3;
Other = 2;
END;
Arrays and iterative processing
Using an ARRAY to define a group of elements, then using an iterative DO loop to cycle through the elements in the array
ARRAY arrayname (n of elements) elements/values;
DO i = 1 to 20; or 1 TO DIM(arrayname)
IF arrayname (i) = value THEN arrayname (i) = element (values);
End;
If you don’t know and then (*) will be sit on the dimension of the elements
What are arrays and why use them
-Created only in the data step
-Reduce the amount of data step program
-Commonly used to repeat to her more lines of code with the only difference being the name of the variable reference
-In groups variables together for easier referencing
-An array can be used to reference variables that already exist in the data or create new variables
Array, syntax, and rules
A rename cannot be the same as the variable name
All elements must either be character or number
Can use [] {} or ()
For character arrays $goes before length in element statements
Use [*]character and dim(arrayname) when you don’t know how many character variables there are.
Temporary arrays
Are useful if you have several constant values that are needed for the data step, but you don’t want to store them permanently
-They do not appear in that output data set in variables are not created
Use temporary
Changing output from wide format to long format
Create a new record in the data set during every iteration of the dual loop
Since there are 10 iterations, every patient will have 10 records in the new data set
The new set will have 10 times as many records as the original data set
ARRAY Dx {10} dx1-dx10;
DO i = 1 to 10;
IF Dx(i);
New Dx = Dx(i);
Output;
End;
Convert multiple outcomes per subject to one outcome per observation
DATA dataset;
SET data;
ARRAY tt(3) t1-t3;
DO TIME = 1 to 3;
OUTCOME = tt(TIME);
OUTPUT;
END;
DROP t1-t3;
RUN;