BookmarkSubscribeRSS Feed
MarcTC
Obsidian | Level 7

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;

1 REPLY 1
SASJedi
SAS Super FREQ

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;
Check out my Jedi SAS Tricks for SAS Users

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 1 reply
  • 938 views
  • 1 like
  • 2 in conversation