BookmarkSubscribeRSS Feed
sks521
Quartz | Level 8

Hi folks,

 

I want to make a macro code available to call within the SAS environment and execute it. can someone share examples or explain how to do it please?

 

Thanks

S

12 REPLIES 12
sks521
Quartz | Level 8

Thanks for your response but can you be more specific. It's a macro someone else has written and made available online. My first question is how and where do I store it and how can I invoke it within my SAS programme and execute.

Kurt_Bremser
Super User

You can simply copy/paste it into your program editor and execute the code, then it is compiled.

Or you store it in a place where your SAS session can read it and use %include in your code.

sks521
Quartz | Level 8

Hi,

 

I have tried to copy/paste and run it in my SAS programme but it doesn't give me any output.

 

For example the macro is;

%macro itsa(model=, dataset=, outcome=, time=, interrupt=, lag=);

 

So on and so forth......

 

I used the following to invoke it within the programme;

%INCLUDE "I:\CHCBradford\Syntax\ITSA.sas" ;
option mprint;
%macro itsa(model=sitsa, dataset=ITS_ACE, outcome=percent_intervention, time=month, interrupt=74, lag=12);
%test;

 

how do I use it for my data set, I mean where do I say which data set to look into for the above parameters?

 

Thanks

 

Kurt_Bremser
Super User

I STRONGLY suggest that you familiarize yourself with the syntax of at least the basic macro statements.

%macro itsa(model=sitsa, dataset=ITS_ACE, outcome=percent_intervention, time=month, interrupt=74, lag=12);
/* this is not a macro CALL, it is the start of a definition, and will do NOTHING in itself */
%test;
/* this is the call of macro test, which must have been defined somewhere with %macro test; ..... %mend; */

How you have to call this externally supplied macro needs to be taken from the documentation for that macro.

sks521
Quartz | Level 8

Thanks Kurt,

 

I am copying the macro in SAS code window below. I used this syntax to CALL it;

%macro itsa(model=, dataset=, outcome=, time=, interrupt=, lag=);

%if (&model=sitsa) %then %do;

    * Create SITSA dummy variables;

    data sitsa_vars;

         set &dataset;

         t=_n_;

         if &time<&interrupt then x=0;

         else x=1;

         if &time<&interrupt then tx=0;

         else tx+1;

         keep &time &outcome t x tx;

    run;
	 * Compute lag+1 for proc model;

    %let lagl=%eval(&lag + 1);

    * If lag is set to 0 or missing then conduct SITSA with OLS regression;

    * If lag>0 then conduct SITSA with OLS regression + Newey-West standard errors;

    %if (&lag<1) %then %do;

         ods output ParameterEstimates=sitsa_stat TestResults=sitsa_stat2 CovB=sitsa_covar;

         proc model data=sitsa_vars;

              parms b0 b1 b2 b3;

              &outcome = b0+(b1*t)+(b2*x)+(b3*tx);

              fit &outcome / covb;

              test b1+b3;

         run; quit;
		  %end;

    %else %do;

         ods output ParameterEstimates=sitsa_stat TestResults=sitsa_stat2 CovB=sitsa_covar;

         proc model data=sitsa_vars;

              parms b0 b1 b2 b3;

              &outcome = b0+(b1*t)+(b2*x)+(b3*tx);

              fit &outcome / covb gmm kernel=(bart,&lagl,0) vardef=n;

              test b1+b3;

         run; quit;

    %end;
	   * Produce tables for model coefficients;

    data sitsa_stat;

         set sitsa_stat;

         keep parameter estimate stderr probt;

         rename probt=prob;

    run;

    data sitsa_stat2;

         set sitsa_stat2;

         keep label probchisq;

         rename label=parameter probchisq=prob;

    run;
	    * Append stats from outputs;

    data sitsa_stat;

         set sitsa_stat sitsa_stat2;

    run;
	* Calculate and save sum of b1+b3 coefficients;

    proc means data=sitsa_stat(where=(parameter='b1' or parameter='b3')) sum noprint;

         output out=b1b3_est sum= / autoname;

         var estimate;

    run;

    * Save sum to macro variable;

    data _null_;

         set b1b3_est;

         call symputx("Sum", Estimate_Sum);

    run;

   * Add sum as b1+b3 parameter estimate;

    data sitsa_stat;

         set sitsa_stat;

         if parameter='b1+b3' then estimate=&Sum;

    run;

  * Save covariance to macro variable;

    data _null_;

         set sitsa_covar;

         if parameter='b3' then call symputx("Covar", b1);

    run;

    

    * Save b1 and b3 standard errors as macro variables;

    data _null_;

         set sitsa_stat;

         if parameter='b1' then call symputx("SE_1", StdErr);

         if parameter='b3' then call symputx("SE_3", StdErr);

    run;

 * Calculate b1+b3 standard error;

    %let sum_se=%sysfunc(sqrt(&SE_1.**2+&SE_3.**2+2*&Covar.));

    * Add b1+b3 standard error;

    * Calculate 95% confidence intervals for all results;

    data sitsa_stat;

         set sitsa_stat;

         if parameter='b1+b3' then StdErr=&sum_se;

         CI_LL=(estimate-1.96*StdErr);

         CI_UL=(estimate+1.96*StdErr);

    run;
	  * Calculate residual/predicted values;

    data _null_;

         set sitsa_stat;

         if parameter='b0' then call symputx("b0", Estimate);

         if parameter='b1' then call symputx("b1", Estimate);

         if parameter='b2' then call symputx("b2", Estimate);

         if parameter='b3' then call symputx("b3", Estimate);

    run;

    data sitsa_vars;

         set sitsa_vars;

         pred=&b0+(&b1*t)+(&b2*x)+(&b3*tx);

        res=(&outcome-pred);
		run;
		 * Print coefficient estimates;

    proc print data=sitsa_stat;

         id parameter;

    run;



    * Define plot characteristics;

    symbol1 color=black line=1 interpol=join value=star;

    symbol2 color=black line=1 interpol=join value=dot;                                                                                                    

    axis1 label=('Time') offset=(1,1);

    axis2 label=('Outcome') offset=(0,0);

    legend1 label=('Phase:') value=('Pre-Interruption' 'Post-Interruption');

    pattern1 color=gray;

    pattern2 color=black;

* Produce 2x plots;

    proc gplot data=sitsa_vars;                                                                                                                

        plot &outcome*t = x / haxis=axis1 vaxis=axis2 cvref=black legend=legend1;

    run; quit;

    proc gplot data=sitsa_vars;                                                                                                                

        plot &outcome*t = x / areas=2 haxis=axis1 vaxis=axis2 cvref=black legend=legend1;

    run; quit;
	 * Delete additional datasets;

    proc datasets library=work noprint;

        delete sitsa_stat sitsa_stat2 b1b3_est sitsa_covar;

    run; quit;



%end;
%mend;

  %INCLUDE itsa;

 

How do I execute it. 

 

You are right, I am not familiar with macro language but using this macro is the best way of analysing the data I have, so I am trying to utilise given resources as best as I can.

 

Thanks for all your help.

S

Kurt_Bremser
Super User

You call the macro with

%itsa(model=X1, dataset=X2, outcome=X3, time=X4, interrupt=X5, lag=X6);

where X* are just placeholders for the values you need to supply, according to the documentation.

sks521
Quartz | Level 8

Really sorry for back-to-back questions;

 

So, I copy/past the whole macro in my SAS editor, then call it as follows;

 

%itsa(model=sitsa, dataset=Counterfactual, outcome=percent_intervention, time=month, interrupt=74, lag=12);

 

Then what needs to be done to get the output?

 

Thanks

S

 

Kurt_Bremser
Super User

Look what the log says. If that does not provide a clue (no WARNING/ERROR either from running the macro definition nor from the call), get in touch with the author.

PaigeMiller
Diamond | Level 26

@sks521 wrote:

Really sorry for back-to-back questions;

 

So, I copy/past the whole macro in my SAS editor, then call it as follows;

 

%itsa(model=sitsa, dataset=Counterfactual, outcome=percent_intervention, time=month, interrupt=74, lag=12);

 

Then what needs to be done to get the output?

 


You might have to use the command

 

ods html;

before you run the macro to make the graphics appear (although for most people ODS HTML is on by default).

--
Paige Miller
sks521
Quartz | Level 8

Thanks it solved the problem.

Reeza
Super User

UCLA introductory tutorial on macro variables and macros
https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/

 

Tutorial on converting a working program to a macro
This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it 🙂

https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md

Examples of common macro usage
https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Ap...

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 12 replies
  • 2662 views
  • 1 like
  • 4 in conversation