BookmarkSubscribeRSS Feed
deleted_user
Not applicable
I created a macro program. I'd call it by
%task(day_num);

If I want to call it 30 times with different day_num, how would I set it in a loop (Is it the efficient way)? My day_num goes from 1 to 30 in sequence.

Thanks.

-Nilan
2 REPLIES 2
Cynthia_sas
SAS Super FREQ
Hi:
Again, a macro program with a macro %DO loop is going to help you out. There are 2 different ways to write your macro program.
[pre]
** Approach 1: use 2 macro programs;
** the macro program for your main task;
%macro task(day=1);
proc print data=sashelp.shoes(obs=&day);
title "TASK macro: value of DAY is &day";
run;
%mend task;

** then make a macro program that will just;
** invoke the %TASK macro inside a %do loop;
%macro dotask(start=, stop=);
%do i = &start %to &stop;
%task(day=&i);
%end;
%mend dotask;

%dotask(start=1,stop=4);
[/pre]

Which is not really 1 macro program, but is 2 -- the first one that does something(in this case, a PROC PRINT). And the second macro program that uses a %DO loop to invoke the first macro program. There is an alternate way to code this, which is to just use one macro program:
[pre]
** Approach 2: do everything within 1 macro program;
** and put the program code you want to generate;
** inside the %DO loop;
%macro alttask(start=, stop=);
%do i = &start %to &stop;
proc print data=sashelp.shoes(obs=&i);
title "ALTTASK macro: value of I is &i";
run;
%end;
%mend alttask;

%alttask(start=3,stop=6);

[/pre]

I prefer the use of keyword parameters for macro programs instead of positional parameters. The thing I like about keyword parameters is that you can specify default values to use if the parameter is not specified and I just find macros with keyword parameters easier to maintain and easier to invoke. But that's my preference (based on mumblety-mumble years of coding in SAS) -- you could, of course, code your macros with positional parameters. However, my examples always come with keyword parameters.

For more help with writing or invoking macro programs, consult the SAS Macro facility documentation or contact Tech Support for help with a specific coding problem.

cynthia
deleted_user
Not applicable
Hi Cynthia,

Thanks for your detailed response. I prefer the first method although it uses 2 macros. It is more readable and I don't want to modify my original macro 🙂

I found the following idea in some pdf files from sugi, that's same as your first idea.

%macro create(howmany);
%do i=1 %to &howmany;
%task(&i);
%end;
%mend create;

%create(3)

Thanks.

-Nilan

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!

New Learning Events in April

 

Join us for two new fee-based courses: Administrative Healthcare Data and SAS via Live Web Monday-Thursday, April 24-27 from 1:00 to 4:30 PM ET each day. And Administrative Healthcare Data and SAS: Hands-On Programming Workshop via Live Web on Friday, April 28 from 9:00 AM to 5:00 PM ET.

LEARN MORE

Discussion stats
  • 2 replies
  • 805 views
  • 0 likes
  • 2 in conversation