DATA Step, Macro, Functions and more

loop with retain or proc sql

Accepted Solution Solved
Reply
Frequent Contributor
Frequent Contributor
Posts: 133
Accepted Solution

loop with retain or proc sql

I am trying to make three separate variables based on the value in one variable. I may have multiple rows per ID and each row can be a code 1-3 in one variable (HAVE). I want to create a new variable for each value of HAVE (Want1 - Want3) and if a value appears in any of the rows for that ID then I want to place the value in the new variable even if that row has a different value for HAVE. 

 

do i need to loop through dataset to create each variable variable separately?

 

 

IDHaveWant1 Want2 Want3
A3 23
A3 23
A3 23
A2 23
B112 
B112 
B212 

Accepted Solutions
Solution
‎03-29-2017 12:34 PM
Super User
Posts: 5,099

Re: loop with retain or proc sql

Here's an approach that has a strict requirement.  The new variables (WANT1, WANT2, and WANT3) must not exist in the original data.  Then assuming your data set is sorted by ID, you could use:

 

data want;

do until (last.id);

   set have;

   by id;

   if have=1 then want1=1;

   else if have=2 then want2=2;

   else if have=3 then want3=3;

end;

do until (last.id);

   set have;

   by id;

   output;

end;

run;

 

The trick is that the top and bottom DO loops read exactly the same set of observations.  The top loop sets values for the new variables.  The bottom loop outputs the observations with the values set by the top loop.

View solution in original post


All Replies
Solution
‎03-29-2017 12:34 PM
Super User
Posts: 5,099

Re: loop with retain or proc sql

Here's an approach that has a strict requirement.  The new variables (WANT1, WANT2, and WANT3) must not exist in the original data.  Then assuming your data set is sorted by ID, you could use:

 

data want;

do until (last.id);

   set have;

   by id;

   if have=1 then want1=1;

   else if have=2 then want2=2;

   else if have=3 then want3=3;

end;

do until (last.id);

   set have;

   by id;

   output;

end;

run;

 

The trick is that the top and bottom DO loops read exactly the same set of observations.  The top loop sets values for the new variables.  The bottom loop outputs the observations with the values set by the top loop.

☑ This topic is solved.

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

Discussion stats
  • 1 reply
  • 116 views
  • 0 likes
  • 2 in conversation