BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Marjolein
Obsidian | Level 7

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
1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

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

5 REPLIES 5
Shmuel
Garnet | Level 18

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;

ballardw
Super User

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.

PeterClemmensen
Tourmaline | Level 20
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;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Marjolein
Obsidian | Level 7

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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