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!
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
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.
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
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!
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.
Ready to level-up your skills? Choose your own adventure.