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 |
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;
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;
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.
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;
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.
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...
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.