BookmarkSubscribeRSS Feed
yong_liu
Fluorite | Level 6

I have several separate SAS codes inside a process flow. Now I want to add loops (by year and lookback) on top of the SAS codes starting from ”2-extract input files” (see the figure attached) to the end. Of course I can put all the codes together and add a Macro like:

 

%MACRO mFilter(nYr_, lookback_);

Read in macro variables and put all needed codes here

%MEND;

 

* run multiple years;

 

%MACRO mMain(nYr1_, nYr2_, lookback_);

 

     %DO y = &nYr1_ %TO &nYr2_;

     %DO x = 1 %TO &lookback_;

 

           %mFilter(&y, &x)

 

     %END;

%MEND;

 

     %mMain(&mYear1, &mYear2, &mLookback); * values given;

 

My question is: Can SAS EG do the loops easily by adding prompts or other ways to the programs or to the process flow while keeping the SAS code programs separate so that it is easy to check logs, and if yes, can you provide details ?

 

Capture.JPG

Thanks a lot.

4 REPLIES 4
Patrick
Opal | Level 21

Assuming you organize your individual programs within a project as a flow (linked programs) which all execute on the same server and same session:

You could add two additional code nodes and link them into your flow - A) where your loop macro needs to begin, B) where your loop macro needs to end.

 

Node A) would have code like:

%macro Loop(stop=);
  %do i=1 %to &stop;
  .....

Node B) would have matching code like:

   ...
   %end;
%mend;

%Loop(stop=&Param_From_Prompt);

... and then define the Prompt with name (here in the example) Param_From_Prompt.

 

yong_liu
Fluorite | Level 6

Thanks for the response. I will give a try

Reeza
Super User
You probably can, but in general, SAS offers methods for 'group by' analysis within most proc and data steps so it's usually easier to use that approach overall instead of macro loops. It's definitely more efficient. You can find a paper called Don't Be Loopy, by David Cassell that outlines why this is a better approach.
yong_liu
Fluorite | Level 6

Thanks for the info.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 1072 views
  • 2 likes
  • 3 in conversation