DATA Step, Macro, Functions and more

replicate an array

Reply
Frequent Contributor
Posts: 77

replicate an array

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;

SAS Employee
Posts: 104

Re: replicate an array

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;
Ask a Question
Discussion stats
  • 1 reply
  • 114 views
  • 1 like
  • 2 in conversation