DATA Step, Macro, Functions and more

Invoke SAS Macro

Reply
Contributor
Posts: 37

Invoke SAS Macro

Hello, 

 

I am having trouble using a SAS macro that was described in a paper. Can you please tell me what step I am missing? Thank you. 

 

From Valeri 2013: The macro has been developed using SAS Version 9.2. In order to implement mediation analysis via the mediation macro in SAS the investigator first opens a new SAS session and inputs the data, which has to include the outcome, treatment and mediator variables as well as the covariates to be adjusted for in the model. Macro activation requires then the investigator to save the macro script and input information in the statement:

 

 

%mediation(data= ,yvar= ,avar= ,mvar= ,cvar= ,a0= ,a1= ,m= ,nc= ,yreg= ,mreg= ,interaction=)
run;

 

 

So, I prepped my dataset and plugged in the values needed for the macro like this (values aren't important here, i just want to know how to ues this macro. 

 

 

%mediation(data=test, yvar=outcome, avar=exposure, mvar=med, cvar=cov, a0=0, a1=1, m=1, nc=1, yreg=logistic, mreg= logistic, interaction=false)
run;

 

 

Then, I get the following error:

 

 

WARNING: Apparent invocation of macro MEDIATION not resolved.
ERROR 180-322: Statement is not valid or it is used out of proper order.

 

 

 

What am I doing wrong? Do I have to define the macro elsewhere? How does SAS know what to do with this macro? 

Thank you!

Super User
Posts: 11,343

Re: Invoke SAS Macro

Posted in reply to pamplemouse22

Use of macro has two part, first defining and compiling it make it ready in the session. That would be code that looks like

%macro macroname(parameters);

<assorted SAS code>

%mend;

 

you  would Execute that block of code before using it as

 

%macroname(a b c);

to use the value a b c as parameter to the macro.

You will get the error you reference when the compile step has not be executed OR if there was an error when attempting to compile it.

Contributor
Posts: 37

Re: Invoke SAS Macro

thank you! i found the script to define the macro. 

Super User
Posts: 5,498

Re: Invoke SAS Macro

Posted in reply to pamplemouse22

It sounds like the piece you are missing is this:

 

"Macro activation requires then the investigator to save the macro script ... "

 

Did you get a copy of the macro and save it somewhere?  Did you identify to the program where the macro is stored?

 

If you did save the macro, the simplest way to define it to your program would be to add one line:

 

%include "path to the file holding the macro code";

 

That being done, you should be able to use the macro later in the same program (or in the same EG session).

Contributor
Posts: 37

Re: Invoke SAS Macro

Posted in reply to Astounding

thank you! i found the script that defines the macro. Now that I have it, the macro definition is very long and takes me about an hour to run each time. do you have suggestions to make this process faster? It's a series of proc reg. I suppressed output to make it run faster, using the followng code. Any other tips? 

ODS LISTING close;
ODS RESULTS OFF;
Super User
Posts: 19,772

Re: Invoke SAS Macro

Posted in reply to pamplemouse22

Macro definition - needs to be run first

%macro print(dataset_name);

proc print data=&dataset_name noobs label;
run;

%mend;

Macro execution:

 

%print(sashelp.class);

 

Contributor
Posts: 37

Re: Invoke SAS Macro

thank you! i found the original macro definition and am trying to run that first. Any tips to make the run time shorter for this macro? I don't want ot delete out any of the comments. 

 

I included this at at the top which is helping some, but it still takes about an hour to run, each time. 

 

ODS LISTING close;

ODS RESULTS OFF;

Ask a Question
Discussion stats
  • 6 replies
  • 137 views
  • 0 likes
  • 4 in conversation