Hi,
I have a dataset with multiple score variables (var7 - var14 where 7 represents visit 7) and I am using array to check which variables had non-missing value and I want to create a new variable vislst to store these visit numbers in (e.g. 7,9,10).
Below codes can only store the last non-missing visit number and not the list of visits. I tried first. and retain but not worked yet. Please help.
data Score_trans_new;
set Score_trans;
length vislst $100.;
array values[1:8] var7 var8 var9 var10 var11 var12 var13 var14;
do i=1 to dim(values);
if not missing(values[i]) then do;
vislst=substr(vname(values[i]),7,2);
end;
end;
run;
Thanks!
Take advantage of the ability to define arrays with custom lower and upper bounds:
data score_trans_new2;
set score_trans;
length vislst $100;
array values {7:14} var7-var14;
do v=lbound(values) to hbound(values);
if values{v}^=. then vislst=catx(',',vislst,v);
end;
run;
data Score_trans;
var7=1; var8=.; var9=1; var10=1; var11=.; var12=.; var13=.; var14=.;
run;
data Score_trans_new(drop=i SUB);
length vislst $100.;
set Score_trans;
array values{8} var7-var14;
vislst='';
do i=1 to dim(values);
if not missing(values(i)) then do;
vislst=strip(vislst)!!' '!!strip(vname(values(i)));
end;
end;
SUB = 'var';
vislst = transtrn(vislst,SUB,trimn(''));
run;
/* end of program */
Take advantage of the ability to define arrays with custom lower and upper bounds:
data score_trans_new2;
set score_trans;
length vislst $100;
array values {7:14} var7-var14;
do v=lbound(values) to hbound(values);
if values{v}^=. then vislst=catx(',',vislst,v);
end;
run;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Early bird rate extended! Save $200 when you sign up by March 31.
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.