Week 10- Do Loops and Arrays Flashcards

1
Q

Loops in SAS

A

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’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

General form of an iterative loop

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Nested DO Loops

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

If – then do statements

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Arrays and iterative processing

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are arrays and why use them

A

-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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Array, syntax, and rules

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Temporary arrays

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Changing output from wide format to long format

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Convert multiple outcomes per subject to one outcome per observation

A

DATA dataset;
SET data;
ARRAY tt(3) t1-t3;
DO TIME = 1 to 3;
OUTCOME = tt(TIME);
OUTPUT;
END;
DROP t1-t3;
RUN;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly