If I don't know the maximum number of visits per patient, I can use : to create an array shown in the following data step.
data cus2;
set cus1;
array V(*) visit:;
do i = 1 to dim(V);
if V(i) ^= . then visit_cnt = visit_cnt + 1;
end;
run;
What if I want to create another array of the same number of elements as V, how to do it? Below is my failed attempt, which would help you understand what I try to accomplish:
data cus2;
set cus1;
array V(*) visit:;
array V2(*) visit2:; /*generate errors by SAS */
do i = 1 to dim(V);
V2(i) = V(i) + 1;
end;
run;
You can capture the number of elements in a macro variable and use that value to set the new array dimension:
/* Make some data to play with */ data cus1; array Visit{15}; do obs=1 to 100; do i=1 to dim(Visit); Visit{i}=ranuni(1); if Visit{i} > .8 then Visit{i}=.; end; Misses=NMISS(of Visit{*}); output; end; drop obs i; run; /* Find out how many elements, store in macro variable Num */ data _null_; set cus1; array V{*} visit:; call symputx('Num',dim(V)); stop; run; %PUT NOTE: There were &Num elements in the V array; /* Now create the new array using &Num as the array size */ data cus2; set cus1; array V{*} visit:; array Visit_{&Num} ; do i = 1 to dim(V); Visit_{i} = V{i} + 1; end; run;
Or, you could query the dictionary tables to find out how many varibles names start with 'visit' in the cus1 data set:
proc sql noprint; select count(*) into :Num from dictionary.columns where LIBNAME ='WORK' and MEMNAME='CUS1' and lowcase (Name) like 'visit%' ; quit;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.