Help using Base SAS procedures

Splitting one variable into multiple based on criteria from another variable

Accepted Solution Solved
Reply
Contributor
Posts: 23
Accepted Solution

Splitting one variable into multiple based on criteria from another variable

Hi,

 

 

I have a question which is intuitively not very difficult, but I cannot seem to find a way to do this in SAS.

 

I have two variables: variable X (categorical: levels A and B), and a continious variable Z (numeric).

I want to create two new variables out of variable Z, with values that depend on the level of X (A or B).

See the accompanying table. Someone knows the appropriate syntax? Thanks a lot in advance!

 

XZZ1Z2
A4.24.2 
A33 
B7 7
A55 
B8 8
B8.5 8.5

Accepted Solutions
Solution
‎11-04-2016 06:17 AM
Trusted Advisor
Posts: 1,586

Re: Splitting one variable into multiple based on criteria from another variable

Posted in reply to Marjolein

I understand that Z1 and Z2 are two new variables to create:

 

Data want;

  set have;

       if x='A' then Z1=Z; else

       if x='B' then Z2=Z;

run;

View solution in original post


All Replies
Solution
‎11-04-2016 06:17 AM
Trusted Advisor
Posts: 1,586

Re: Splitting one variable into multiple based on criteria from another variable

Posted in reply to Marjolein

I understand that Z1 and Z2 are two new variables to create:

 

Data want;

  set have;

       if x='A' then Z1=Z; else

       if x='B' then Z2=Z;

run;

Super User
Posts: 11,343

Re: Splitting one variable into multiple based on criteria from another variable

Posted in reply to Marjolein

It looks like you want something like:

 

data want;

    set have;

    if X = 'A' Then Z1 = z;

    else if X='B' then Z2 = z;

run;

 

 

Note: If you have a large number of values of X to consider there are different approaches that may work such as use of the IN comparison if the same thing is done for multiple values or a different structure called SELECT if you have multiple values and each generates a different output.

PROC Star
Posts: 768

Re: Splitting one variable into multiple based on criteria from another variable

Posted in reply to Marjolein
data have;
   input X $ Z;
   datalines;
A 4. 
A 3  
B 7  
A 5  
B 8 
B 8.5
;

Data want;
  set have;
       if   x='A' then Z1=Z; 
       else x='B' then Z2=Z;
run;
Super User
Super User
Posts: 7,997

Re: Splitting one variable into multiple based on criteria from another variable

Posted in reply to Marjolein

To me, the quesiton is why.  Currently you have a dataset with two columns, one indicates data position (i.e a where=a would show all z1 for instance), and one is the result.  There are no missing elements.  What you are asking for is a dataset which is twice as big - 4 variables - with various missing elements, but doesn't actually add anything to the data.  So why bother, its just taking more room but without any benefit.

Contributor
Posts: 23

Re: Splitting one variable into multiple based on criteria from another variable

I understand your point. The reason I want to do it, is because they should really represent two distinct variables. Therefore, I really prefer to have them in seperate columns, rather than using the BY statement all the time. Or maybe my mind is rather unlogical...

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 242 views
  • 4 likes
  • 5 in conversation