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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.