Hello Please help to get NEW_VAR based on C_01 to C_07 var values
if Only C_03 value as 'x' then NEW_VAR value will be the variable name and value
if more than value 'x' in C_01 to C_07 then New_var value will be 'Multiple'
can we code this in array or any simple code ?
| SUBJID | C_01 | C_02 | C_03 | C_04 | C_05 | C_06 | C_07 | New_Var |
| 10001 | ||||||||
| 10002 | x | C_03_X | ||||||
| 10003 | x | C_06_X | ||||||
| 10004 | x | x | Multiple | |||||
| 10005 | x | C_02_X | ||||||
| 10006 |
data have;
infile cards truncover;
input SUBJID (C_01 C_02 C_03 C_04 C_05 C_06 C_07) ($);
cards;
10001 . . . . . . .
10002 . . x . . . .
10003 . . . . . x .
10004 . . . x . . x
10005 . x . . . . .
10006 . . . . . . .
;
data want;
set have;
array t c_01-c_07;
length New_Var $32;
_n_=dim(t)-cmiss(of t(*));
if _n_>1 then New_Var='Multiple';
else if _n_=1 then New_Var=catx(' ',vname(t(whichc('x',of t(*)))),'X');
run;
data have;
infile cards truncover;
input SUBJID (C_01 C_02 C_03 C_04 C_05 C_06 C_07) ($);
cards;
10001 . . . . . . .
10002 . . x . . . .
10003 . . . . . x .
10004 . . . x . . x
10005 . x . . . . .
10006 . . . . . . .
;
data want;
set have;
array t c_01-c_07;
length New_Var $32;
_n_=dim(t)-cmiss(of t(*));
if _n_>1 then New_Var='Multiple';
else if _n_=1 then New_Var=catx(' ',vname(t(whichc('x',of t(*)))),'X');
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—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.