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

Hello, 

I am trying to create a new variable based on a set of other catagorical variables (all var are numerical; 1/0). 

I want to tag the new var as 1 if multiple other variables are also tagged as one. Else they are 0. I am trying to use the OR operator. 

 

Data Want; 

Set Have; 

NewVar = 0 /* Intialize var */

if var1 = 1 or var2 =1 or var3= 1 then NewVar = 1; 

Run; 

 

This is not working so far. I have tried using () around the variables and using the | operator. 

Thanks in advance. 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star
Try:

if sum(var1, var2, var3) > 1 then newvar = 1;
else newvar = 0;

View solution in original post

6 REPLIES 6
Astounding
PROC Star
Try:

if sum(var1, var2, var3) > 1 then newvar = 1;
else newvar = 0;
ballardw
Super User

@Astounding wrote:
Try:

if sum(var1, var2, var3) > 1 then newvar = 1;
else newvar = 0;

Or even shorter:

 

Newvar = (sum(var1,var2,var3)>1);

 

Sum with give the number of variables with 1 as the value

Some things that may help with possibly related code:

 

Min(var1,var2,var3) = 0 ; At least one 0 in the variables (even if some are missing)

Min(var1,var2,var3) = 1;   means All non-missing variables are 1.

Max(var1, var2,var3)=0;  means all non-missing are 0

Max(var1, var2,var3)=1;  means at least one non-missing is 1

Range(var1,var2,var3)=0;  means all non-missing are the same, possibly all 1 or all 0 but the same.

Range(var1,var2,var3)=1; means at least one 1 and at least one 0.

N(var1,var2,var3) to get number of non-missing values

Nmiss(var1,var2,var3) to get number of missing values

pbhatt
Calcite | Level 5
Thank you so much for your detailed response! I will make sure to keep track of these commands for future use!
marceloleme
Fluorite | Level 6

Try to put ";" at the end of statement "NewVar = 0". Tell us if it worked. 

pbhatt
Calcite | Level 5
The ";" typo was by accident, but yes I did try that.
That was not the issue.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 6 replies
  • 926 views
  • 3 likes
  • 5 in conversation