Solved
Contributor
Posts: 27

# 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!

 X Z Z1 Z2 A 4.2 4.2 A 3 3 B 7 7 A 5 5 B 8 8 B 8.5 8.5

Accepted Solutions
Solution
‎11-04-2016 06:17 AM
Posts: 1,837

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

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;

All Replies
Solution
‎11-04-2016 06:17 AM
Posts: 1,837

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

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: 13,542

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

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: 1,283

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

``````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
Posts: 9,599

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

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: 27

## 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.