I need to create 3 variables(var1 var2 var3) based upon the occurrence of conditions within the subject. I have the input dataset example and output dataset example below. I think and array with do until might work but I am not sure how to implement. Any suggestions?
Input Dataset:
subject None Discomfort Bad_Vision Other
12345 1
12345 1
12345 1 1
12345 1 1
12345 1 1 1
12345 1
Output Dataset:
Subject Var1 Var2 Var3
12345 NONE
12345 Discomfort
12345 Discomfort Bad_vision
12345 Discomfort Other
12345 Discomfort Bad vision Other
12345 Other
Best is to provide example data in the form of a data step. Then there are no questions about variable type, name or characteristics. Also code can be tested against the supplied data.
Also best is to post code in a code box opened using the forum {I} icon to preserve formatting.
data have; input subject None Discomfort Bad_Vision Other; datalines; 12345 1 . . . 12345 . 1 . . 12345 . 1 1 . 12345 . 1 . 1 12345 . 1 1 1 12345 . . . 1 run; data want; set have; array a None Discomfort Bad_vision Other; array var{3} $ 10; count=0; do i=1 to dim(a); if a[i] = 1 then do; count=count+1; var[count]=vname(a[i]); end; end; keep subject var: ; run;
The above works for your example. If you ever have cases where NONE is one and the other variables are populated as well then the solution may require having a fourth variable.
the function VNAME gets the name of a variable referenced as an array element.
The VAR: with the colon is a short cut list that refers to all variables whose names start with VAR;
Best is to provide example data in the form of a data step. Then there are no questions about variable type, name or characteristics. Also code can be tested against the supplied data.
Also best is to post code in a code box opened using the forum {I} icon to preserve formatting.
data have; input subject None Discomfort Bad_Vision Other; datalines; 12345 1 . . . 12345 . 1 . . 12345 . 1 1 . 12345 . 1 . 1 12345 . 1 1 1 12345 . . . 1 run; data want; set have; array a None Discomfort Bad_vision Other; array var{3} $ 10; count=0; do i=1 to dim(a); if a[i] = 1 then do; count=count+1; var[count]=vname(a[i]); end; end; keep subject var: ; run;
The above works for your example. If you ever have cases where NONE is one and the other variables are populated as well then the solution may require having a fourth variable.
the function VNAME gets the name of a variable referenced as an array element.
The VAR: with the colon is a short cut list that refers to all variables whose names start with VAR;
Thank you. I will keep in mind the points you mentioned about data. Thanks again.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.