DATA Step, Macro, Functions and more

using macro in do loop

Accepted Solution Solved
Reply
Contributor
Posts: 44
Accepted Solution

using macro in do loop

Hello Community,

 

I am stuck trying to pass a temp value in a do loop. Here is what I am trying to achieve:

 

AGENCY

SCORE

A

68

A

78

B

62

C

61

 

Here is my code:

ODS HTML;

%MACRO CAR(AGENCY=);
ODS html file="\\cchd.org\odsdfs$\ODSShared\Surveillance Reports and QA\Surveillance QA\&AGENCY..html" style=Seaside;
ODS title "Contact FR-QC" ;
ods escapechar = "~";
OPTIONS NODATE NONUMBER;
option orientation=landscape;
proc report data=TEST nofs center;
where AGENCY="&AGENCY";
columns AGENCY SCORE;
run;
ODS html CLOSE;
%MEND CAR;

%CAR (AGENCY=A); /*Report for Agency A*/

 

The Agency list is dynamic and I want to generate the report by agency by looping the observations listed in AGENCY... Any insight is appreciated.

 

 


Accepted Solutions
Solution
2 weeks ago
Super User
Posts: 23,321

Re: using macro in do loop

Although you can loop, I'm a fan of the CALL EXECUTE approach since it seems a bit cleaner to me.

Here's an example of that, you can run it to test. Its broken into small steps so you can follow it, but feel free to combine them into one once you understand the concept.

 

Same idea, except printing a report for each age, sex combination in the SASHELP.CLASS table. 

https://gist.github.com/statgeek/beb97b1c6d4517dde3b2

View solution in original post


All Replies
Super User
Posts: 23,321

Re: using macro in do loop

You can create a list of the agencies using PROC SQL, and here's how you loop over such a list:

 

http://documentation.sas.com/?docsetId=mcrolref&docsetTarget=n1qvxz5u3uru7yn1nk7q64ohvwak.htm&docset...

 

proc sql noprint;
select agency into :agency_list separated by " "
from have;
quit;

%put &agency_list;

@mehretua wrote:

Hello Community,

 

I am stuck trying to pass a temp value in a do loop. Here is what I am trying to achieve:

 

AGENCY

SCORE

A

68

A

78

B

62

C

61

 

Here is my code:

ODS HTML;

%MACRO CAR(AGENCY=);
ODS html file="\\cchd.org\odsdfs$\ODSShared\Surveillance Reports and QA\Surveillance QA\&AGENCY..html" style=Seaside;
ODS title "Contact FR-QC" ;
ods escapechar = "~";
OPTIONS NODATE NONUMBER;
option orientation=landscape;
proc report data=TEST nofs center;
where AGENCY="&AGENCY";
columns AGENCY SCORE;
run;
ODS html CLOSE;
%MEND CAR;

%CAR (AGENCY=A); /*Report for Agency A*/

 

The Agency list is dynamic and I want to generate the report by agency by looping the observations listed in AGENCY... Any insight is appreciated.

 

 


 

Solution
2 weeks ago
Super User
Posts: 23,321

Re: using macro in do loop

Although you can loop, I'm a fan of the CALL EXECUTE approach since it seems a bit cleaner to me.

Here's an example of that, you can run it to test. Its broken into small steps so you can follow it, but feel free to combine them into one once you understand the concept.

 

Same idea, except printing a report for each age, sex combination in the SASHELP.CLASS table. 

https://gist.github.com/statgeek/beb97b1c6d4517dde3b2

Contributor
Posts: 44

Re: using macro in do loop

Thank you.. Call Execute does magic!

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 109 views
  • 1 like
  • 2 in conversation