Recoding variables

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

Recoding variables

[ Edited ]

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?


Accepted Solutions
Solution
‎09-20-2016 10:44 AM
Super User
Super User
Posts: 7,039

Re: Recoding variables

Posted in reply to cpolihronis

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


All Replies
Solution
‎09-20-2016 10:44 AM
Super User
Super User
Posts: 7,039

Re: Recoding variables

Posted in reply to cpolihronis

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;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

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