Programming the statistical procedures from SAS

MACRO HELP

Accepted Solution Solved
Reply
Super Contributor
Posts: 1,040
Accepted Solution

MACRO HELP

Could you help me build a macro please...

Thanks

ods pdf file="C:\Data\Projects\Dhoom-20120911002\age.pdf";
proc freq data=test;
tables age*Pat/nopercent fisher  relrisk;
exact pchi or;
title "Age vs Pat";                       
run;

proc freq data=test;
tables age*Comp/nopercent fisher  relrisk;
exact pchi or;
title "Age vs Comp";
run;

proc freq data=test;
tables age*Any/nopercent fisher  relrisk;
exact pchi or;
title "Age vs Any";
run;
ods pdf close;

This piece of code repeats for 11 variables.

in the ods statement the name of the pdf changes next time
tables statement age changs to another next
title for the above next time :Age is replaced with another


Accepted Solutions
Solution
‎09-26-2012 05:24 PM
Super User
Super User
Posts: 6,134

Re: MACRO HELP

Before going all the way to a macro try replacing the parts that change with a macro variable reference.

Example:

%let ds=test;

%let var=age;

ods pdf file="C:\Data\Projects\Dhoom-20120911002\&var..pdf";

proc freq data=&ds;

tables &var*Pat/nopercent fisher relrisk;

exact pchi or;

title "&var vs Pat";

run;

....


Then you can try calling it a couple times changing the value of the macro variables VAR and DS.

Once you have it working you can wrap it in a macro;


%macro report1(ds,var);

ods pdf file=........

....

%mend report1;


Then you can call it 11 times by just writing 11 macro calls with different values for the parameters.

%report1(test,age)

%report1(test,ht)


View solution in original post


All Replies
Solution
‎09-26-2012 05:24 PM
Super User
Super User
Posts: 6,134

Re: MACRO HELP

Before going all the way to a macro try replacing the parts that change with a macro variable reference.

Example:

%let ds=test;

%let var=age;

ods pdf file="C:\Data\Projects\Dhoom-20120911002\&var..pdf";

proc freq data=&ds;

tables &var*Pat/nopercent fisher relrisk;

exact pchi or;

title "&var vs Pat";

run;

....


Then you can try calling it a couple times changing the value of the macro variables VAR and DS.

Once you have it working you can wrap it in a macro;


%macro report1(ds,var);

ods pdf file=........

....

%mend report1;


Then you can call it 11 times by just writing 11 macro calls with different values for the parameters.

%report1(test,age)

%report1(test,ht)


Super Contributor
Posts: 1,040

Re: MACRO HELP

Tom,

The three freqs shown above constitutes a block.

I have to compute 11 blocks like that.

I think u misunderstood the 3 freqs shown above for 3 blocks.

Super User
Super User
Posts: 6,134

Re: MACRO HELP

No. I just didn't want to retype the whole thing. Hence the use of ellipses (...) in my sample code.

Super Contributor
Posts: 1,040

Re: MACRO HELP

Ok Got u.

But in the second block also the dset name is the same. So i can keep it intact right instead of assigning it a macro variable reference???

Thnx

Super User
Super User
Posts: 6,134

Re: MACRO HELP

Yes.  I just did that to show that you are not limited to a single parameter.

Grand Advisor
Posts: 16,875

Re: MACRO HELP

Why write your own?

Do some googling to get the code and then dissect it instead with some put statements. Unless you're doing it as a learning exercise instead of work....

Here's an example of one that I've used before and like, the summary one. And if its not what you want, at least you have an idea of how to create the macro and where to start instead of scratch.

http://mayoresearch.mayo.edu/mayo/research/biostat/sasmacros.cfm

Super Contributor
Posts: 1,040

Re: MACRO HELP

Thanks Reeza

☑ This topic is SOLVED.

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

Discussion stats
  • 7 replies
  • 164 views
  • 3 likes
  • 3 in conversation