BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
cpolihronis
Fluorite | Level 6

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

View solution in original post

1 REPLY 1
Tom
Super User Tom
Super User

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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 535 views
  • 1 like
  • 2 in conversation