BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
sliaousa
Fluorite | Level 6

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!

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

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;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

3 REPLIES 3
sbxkoenk
SAS Super FREQ
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 */
mkeintz
PROC Star

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;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

sas-innovate-white.png

Register Today!

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.

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
  • 3 replies
  • 3067 views
  • 8 likes
  • 3 in conversation