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

Hello everyone,

 

I am trying to create a new binary variable based on the conditions of multiple other variables, see below for example dataset. Var1-3 values can be 0-3, -99, or missing  and these are 3 non-overlapping variables (i.e. if there is a value for Var1, there will not be a value for Var2-3). I'd like my NEW_VAR to code for the presence of a "legit" value (which I am defining as either a 1-3 in any Var1-3.). basically if any of the values for Var1-3 = 0 or -99 then NEW_VAR=0, else NEW_VAR=1. The problem with the code I below is that everything gets coded as 0 in the NEW_VAR. Any help is much appreciated.

 

Attempted code:

data want;
set have;
if (var1>=1 or var2>=1 or var3>=1) then new_var=1;
if (var1<=0 or var2<=0 or var3<=0)then new_var=0;
run;

Desired output:

ID Var1 Var2 Var3 NEW_VAR
1 3 . . 1
2 . . 0 0
3 . -99 . 0
4 . . 2

1

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

 

data want;
set have;

new_var = coalesce(var1, var2, var3) in (1, 2, 3);

run;

Coalesce will get the single value from the 3 so you don't need all three comparisons. 

IN will check if the value is between 1 and 3, and if so, returns a 1, otherwise it returns 0.

 


@monsterpie wrote:

Hello everyone,

 

I am trying to create a new binary variable based on the conditions of multiple other variables, see below for example dataset. Var1-3 values can be 0-3, -99, or missing  and these are 3 non-overlapping variables (i.e. if there is a value for Var1, there will not be a value for Var2-3). I'd like my NEW_VAR to code for the presence of a "legit" value (which I am defining as either a 1-3 in any Var1-3.). basically if any of the values for Var1-3 = 0 or -99 then NEW_VAR=0, else NEW_VAR=1. The problem with the code I below is that everything gets coded as 0 in the NEW_VAR. Any help is much appreciated.

 

Attempted code:

data want;
set have;
if (var1>=1 or var2>=1 or var3>=1) then new_var=1;
if (var1<=0 or var2<=0 or var3<=0)then new_var=0;
run;

Desired output:

ID Var1 Var2 Var3 NEW_VAR
1 3 . . 1
2 . . 0 0
3 . -99 . 0
4 . . 2

1


 

 

View solution in original post

1 REPLY 1
Reeza
Super User

 

data want;
set have;

new_var = coalesce(var1, var2, var3) in (1, 2, 3);

run;

Coalesce will get the single value from the 3 so you don't need all three comparisons. 

IN will check if the value is between 1 and 3, and if so, returns a 1, otherwise it returns 0.

 


@monsterpie wrote:

Hello everyone,

 

I am trying to create a new binary variable based on the conditions of multiple other variables, see below for example dataset. Var1-3 values can be 0-3, -99, or missing  and these are 3 non-overlapping variables (i.e. if there is a value for Var1, there will not be a value for Var2-3). I'd like my NEW_VAR to code for the presence of a "legit" value (which I am defining as either a 1-3 in any Var1-3.). basically if any of the values for Var1-3 = 0 or -99 then NEW_VAR=0, else NEW_VAR=1. The problem with the code I below is that everything gets coded as 0 in the NEW_VAR. Any help is much appreciated.

 

Attempted code:

data want;
set have;
if (var1>=1 or var2>=1 or var3>=1) then new_var=1;
if (var1<=0 or var2<=0 or var3<=0)then new_var=0;
run;

Desired output:

ID Var1 Var2 Var3 NEW_VAR
1 3 . . 1
2 . . 0 0
3 . -99 . 0
4 . . 2

1


 

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 1 reply
  • 1599 views
  • 1 like
  • 2 in conversation