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.
Please post the complete log of your step by copy/pasting it into a window opened with this button:
@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
Try to put ";" at the end of statement "NewVar = 0". Tell us if it worked.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.