Hi there,
I was wondering how to recode my dataset to get it into a planned missing/ cohort-sequential format before I flip it to long format (below). Each child was given a measure of victimization that was filled out in the fall and spring.
Currently the data is in wide format:
ID Grade V1 V2 V3 V4 V5
300 3 4 5 4 3 4
301 3 1 1 1 2 1
302 4 2 4 2 2 1
I would like to re-code it into a cohort sequential format in SAS:
Grade | ID | S3_V | F4_V | S4_V | F5_V | S5_V | F6_V | S6_V |
3
| 300 | 4 | 5 | 4 | 3 | 4 |
|
|
3 | 301 | 1 | 1 | 1 | 2 | 1 |
| |
4
| 302 |
|
| 2 | 4 | 2 | 2 | 1 |
|
|
|
|
|
|
|
In SPSS I would typically specify the grade, and recode the already existing variable into a new variable a new variable for each time point (only one is shown below):
DO IF (cgrade_2=3).
RECODE V_1 (ELSE=Copy)
INTO S3_V.
END IF.
VARIABLE LABELS S3_V 'S3_V 'Victimization Grade 3 Spring'.
EXECUTE.
I would compute all five again for the next cohort (Grade 4), again only one is shown below.
DO IF (cgrade_2=4).
RECODE V_1 (ELSE=Copy)
INTO S4_V.
END IF.
VARIABLE LABELS S4_V 'S4_V 'Victimization Grade 4 Spring'.
EXECUTE.
Would I be using the if then or Array commands? What would this code look like?
Looks like a problem for arrays. You have a five element input array that you want to move into five elements of a larger array using the GRADE variable to determine the starting point.
array original v1-v5;
array new s3 f4 s4 f5 s5 f6 s6 ;
target = 2*(grade-3);
do source=1 to dim(original);
new(target + source) = original(source);
end;
Looks like a problem for arrays. You have a five element input array that you want to move into five elements of a larger array using the GRADE variable to determine the starting point.
array original v1-v5;
array new s3 f4 s4 f5 s5 f6 s6 ;
target = 2*(grade-3);
do source=1 to dim(original);
new(target + source) = original(source);
end;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.