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

I would like to be able to join variables 1-4 into one variable based on the value.

 

data have;
input var1 var2 var3 var4;
cards;
1 91 98 1
2 1 91 2
2 2 91 2 
1 98 2 91
2 2 2 2 ; run;

I want the output to be set so if there is a value of '1' in var1, var2, var3, or var4, the output will be 1. If there is not a value of '1' in any of the four variables, I want the output to be 0.  I'm not sure how to go about this. Thank you!

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Try this

 

data have;
input var1 var2 var3 var4;
cards;
1 91 98 1
2 1 91 2
2 2 91 2 
1 98 2 91
2 2 2 2
;
run;

data want;
   set have;
   array var var:;
   v = (1 in var);
run;

 

Result:

 

var1 var2 var3 var4 v 
1    91   98   1    1 
2    1    91   2    1 
2    2    91   2    0 
1    98   2    91   1 
2    2    2    2    0 

View solution in original post

2 REPLIES 2
mar0000
Obsidian | Level 7
data have;
input var1 var2 var3 var4;
cards;
1 91 98 1
2 1 91 2
2 2 91 2 
1 98 2 91
2 2 2 2
;
run;

The example code was messed up. This is more accurate.

PeterClemmensen
Tourmaline | Level 20

Try this

 

data have;
input var1 var2 var3 var4;
cards;
1 91 98 1
2 1 91 2
2 2 91 2 
1 98 2 91
2 2 2 2
;
run;

data want;
   set have;
   array var var:;
   v = (1 in var);
run;

 

Result:

 

var1 var2 var3 var4 v 
1    91   98   1    1 
2    1    91   2    1 
2    2    91   2    0 
1    98   2    91   1 
2    2    2    2    0 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 544 views
  • 1 like
  • 2 in conversation