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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 773 views
  • 1 like
  • 2 in conversation