Calcite | Level 5

Using conditional OR statement

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.

1 ACCEPTED SOLUTION

Accepted Solutions
PROC Star

Re: Using conditional OR statement

Try:

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

Re: Using conditional OR statement

Please post the complete log of your step by copy/pasting it into a window opened with this button:

PROC Star

Re: Using conditional OR statement

Try:

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

Re: Using conditional OR statement

@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

Calcite | Level 5

Re: Using conditional OR statement

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

Re: Using conditional OR statement

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

Calcite | Level 5

Re: Using conditional OR statement

The ";" typo was by accident, but yes I did try that.
That was not the issue.
Discussion stats
• 6 replies
• 909 views
• 3 likes
• 5 in conversation