DATA Step, Macro, Functions and more

Array embedded in macro

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 93
Accepted Solution

Array embedded in macro

I have windows 7 and SAS 9.3.

I am learning macro, and this is my first time to do array within a macro program. I know that there is a function for it, but cannot really understand the function. There are 15 variables (proc1-proc15) with the same attributes in 10 datasets (data1-data10). Here is my program:

%macro create_var();

   %do i=1 to 10;

   data mylib.data&i; set mylib.data&i;

    tx=0;

   array proc[15] proc1-proc15;

       do j=1 to 15;

       if proc(j) in (8540, 8541, 8542) then tx=1;

       end;

   run;

%end;

%mend;

%create_var();

run;

The red part is my array in regular data step. Anyone can teach me how to modify it to make the macro program work? Thanks a lot.


Accepted Solutions
Solution
‎02-11-2015 05:03 PM
Super User
Posts: 17,819

Re: Array embedded in macro

There actually isn't something called a macro array. There are some workarounds but that's what they are.

Your code appears correct, what part do you need to change to make it macro?

View solution in original post


All Replies
Solution
‎02-11-2015 05:03 PM
Super User
Posts: 17,819

Re: Array embedded in macro

There actually isn't something called a macro array. There are some workarounds but that's what they are.

Your code appears correct, what part do you need to change to make it macro?

Frequent Contributor
Posts: 93

Re: Array embedded in macro

Thanks, Reeza. It does work now -- don't know why it didn't previously. Thanks a lot!

Super User
Posts: 10,500

Re: Array embedded in macro

A minor bit for efficiency. It looks like you don't care which variable contributes the values in the list so as soon as the first one is true you could leave the loop;

   array proc[15] proc1-proc15;

       do j=1 to 15;

       if proc(j) in (8540, 8541, 8542) then tx=1;

       if tx=1 then leave;

       end;

if proc1 is 8540 then this avoids comparing proc2-15. Which may save some time in large datasets.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 204 views
  • 4 likes
  • 3 in conversation