BookmarkSubscribeRSS Feed
Rose1
Calcite | Level 5

Hello,

 

I have some data and want to create a new variable from my current variables. I basically want the following outcome:

IF var1 = 0 and var2 = 0 then newvariable = 1

If var1 =0 and var2 = 1 then newvariable=2

If var1 = 1 and var2=0 then newvariable = 3

if var1 = 1 and var2=1 then newvariable = 4

 

I have tried the following and it has not worked

 

Data new;

set old;

IF var1 = 0 and var2 = 0 then newvariable = 1;

else If var1 =0 and var2 = 1 then newvariable=2;

else If var1 = 1 and var2=0 then newvariable = 3;

else if var1 = 1 and var2=1 then newvariable = 4;

RUN;

 

Any help would be greatly appreciated!

 

7 REPLIES 7
WarrenKuhfeld
Ammonite | Level 13

What is the input?
What is the output?

Your if statements look ok.


FYI all you need is a single assignment instead of 4 ifs:

newvariable = 2 * var1 + var2 + 1;

Rose1
Calcite | Level 5

I've attached an example of my input (i've tried with and without parentheses) 

image.png

When I print the new variable "group8", I would expect to see it coded 1,2,3 or 4 for each subject, but it is just blank

 

image.png

 

 

At the end of this I basically want a new variable "group8" coded so I have 4 levels based on those original variables:

treatment 1 + diagnosis 1 = 1

treatment 2 + diagnosis 1 = 2

treatment 1 + diagnosis 2 = 3

treatment 2 + diagnosis 2 = 4

 

Hope this makes sense. Thank you for your help.

WarrenKuhfeld
Ammonite | Level 13

What is the input? Is group8 a character variable in the input data set? You need to provide more info.

Rose1
Calcite | Level 5

No, group8 is an arbitrary variable name I am trying to create based on these diagnosis and treatment variables. It does not exist in the input data set. 

 

The input for the other variables is something like this - 

 

Screen Shot 2021-03-22 at 9.48.30 PM.png

What I want is SAS to code my data based on these variables so it puts out something like this:

Screen Shot 2021-03-22 at 9.48.34 PM.png

WarrenKuhfeld
Ammonite | Level 13

No one is going to be able to help you if you show "input something like this." Show the actual input. Show the actual output. Show the actual code. If the data set is too big or proprietary, make up a small sample proof of concept data set. Showing proc contents results for the input would help too. Show the SAS log. Are there notes, errors, or warnings? Solving problems like this is simple only if you examine all the information.

Rick_SAS
SAS Super FREQ

Here is a sample data set and solution. It works for me. Try it out:

data run1;
input subject treatment diagnosis;
datalines;
1 0 1
2 1 0
3 0 0
4 0 1
5 1 0
6 1 1
7 0 1
8 1 0
9 0 0
;

data newrun1;
set run1;
if      treatment=0 and diagnosis=0 then group8=1;
else if treatment=0 and diagnosis=1 then group8=2;
else if treatment=1 and diagnosis=0 then group8=3;
else if treatment=1 and diagnosis=1 then group8=4;
run;

proc print; run;

 

Most likely, the problem is your data. Either your input data set doesn't have the variables Treatment and Diagnosis (check your spelling!) or those variables do not have the values you think they do. Use the following to examine the values of your variables:

 

proc freq data=run1;
tables treatment * diagnosis / norow nocol nopercent;
run;
andreas_lds
Jade | Level 19

@Rose1 wrote:

...

When I print the new variable "group8", I would expect to see it coded 1,2,3 or 4 for each subject, but it is just blank

Check the log! Are there any unexpected notes, warnings?

sas-innovate-wordmark-2025-midnight.png

Register Today!

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.


Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 8623 views
  • 3 likes
  • 4 in conversation