Macro Magic III
Macro Magic III
SAUSAG Presentation (30mins)
Overview
Overview

Macro Magic Posters have been displayed
at the last 2 SUGA presentations.

Continue the trend…

Hopefully everyone will learn something
new…
SAS Macro - Introduction
SAS Macro - Introduction

Used to generate (often repeated or
conditional) SAS code
SAS Macro - Introduction
SAS Macro - Introduction
proc print data=db.yr2000 noobs;
var month income expense;
run;
proc print data=db.yr2001 noobs;
var month income expense;
run;
SAS Macro - Introduction
SAS Macro - Introduction
%macro myprint(dset);
proc print data=&dset noobs;
var month income expense;
run;
%mend;
%myprint(db.yr2000)
%myprint(db.yr2001)
SAS Macro - Introduction
SAS Macro - Introduction
%macro myprint(dset);
proc print data=&dset noobs;
var month income expense;
run;
%mend;
%myprint(db.yr2000)
%myprint(db.yr2001)
SAS Macro - Introduction
SAS Macro - Introduction
%macro myprint(dset);
proc print data=&dset noobs;
var month income expense;
run;
%mend;
%myprint(db.yr2000)
%myprint(db.yr2001)
SAS Macro - Introduction
SAS Macro - Introduction
%macro myprint(dset);
proc print data=&dset noobs;
var month income expense;
run;
%mend;
%myprint(db.yr2000)
%myprint(db.yr2001)
SAS Macro - Introduction
SAS Macro - Introduction
%macro myprint(dset);
proc print data=db.yr2000 noobs;
var month income expense;
run;
%mend;
%myprint(db.yr2000)
%myprint(db.yr2001)
Executed
Code
Error Checking
Error Checking

Production SAS jobs should have error
checking

May need to run certain steps based on
failure

&syserr, &syslibrc, &sysfilrc, &syslckrc

0=success, +ve=error, -ve=warning

%goto
Error Checking
Error Checking
%macro update_db;
libname db "Q:DataFiles" access=readonly;
%if &syslibrc ne 0 %then %goto err_lib;
proc append base=db.invoices data=work.invoices;
run;
%if &syserr ne 0 %then %goto err_append;
%goto finish;
Error Checking
Error Checking
%err_lib:
%email_db("update_db: Library failure",admins);
%goto finish;
%err_append:
%email_db("update_db: Append failure",admins);
%goto finish;
%finish:
%mend update_db;
Error Checking
Error Checking

OPTIONS ERRORABEND; is a good
alternative, although has less control.

When an ERROR occurs, SAS will stop.

Beware this will close down an interactive
session unexpectedly!!!
Platform Checking
Platform Checking

&sysscp can be used to determine the
platform

Standard macro library for all platforms, with
occasional conditional code

%if &sysscp=WIN %then %do; …

%put &sysscp;
Platform Checking
Platform Checking
%macro myalloc(file);
%if &sysscp=WIN %then %do;
libname data "Q:Data&file"
access=readonly;
%end;
%else %if &sysscp=OS %then %do;
libname data "PROD.Q.&file..DATA"
disp=shr;
%end;
%mend myalloc;
Version of SAS Checking
Version of SAS Checking

Some installations may have more than one
version of SAS installed

%if &sysver=8.2 %then %do; …
Call Execute
Call Execute

Call Execute enables you to execute code
conditionally without macro

CALL EXECUTE(arg);

The argument is executed at the next step
boundary
Call Execute
Call Execute
Call Execute
Call Execute
%macro myprint(style);
proc print data=sasuser.houses;
title "Style of '&style'";
where style="&style";
run;
%mend myprint;
Call Execute
Call Execute
proc freq data=sasuser.houses noprint;
table style / out=work.styles;
run;
Call Execute
Call Execute
proc freq data=sasuser.houses noprint;
table style / out=work.styles;
run;
Call Execute
Call Execute
proc freq data=sasuser.houses noprint;
table style / out=work.styles;
run;
data _null_;
set work.styles;
call execute('%myprint('!!style!!');');
run;
Call Execute
Call Execute
67 data _null_;
68 set work.styles;
69 call execute('%myprint('!!style!!');');
70 run;
NOTE: There were 4 observations read from the data set ...
NOTE: CALL EXECUTE generated line.
1 + proc print data=sasuser.houses; title "Style of
'CONDO'"; where style="CONDO";
run;
Version 8 Enhancements
Version 8 Enhancements

%scan(argument,n<,delimiter>);

Now allows negative indexes
fullfile=d:DataBudget2001.xls
%let file=%scan(&fullfile,-1,);
file=2001.xls
Version 6 Goodies
Version 6 Goodies

%sysfunc(function(arg(s))<,format>);

Enables you to use standard SAS functions
not normally available to SAS Macros.
title "%sysfunc(date(),worddate.)";
title "July 24, 2002";
Version 6 Goodies
Version 6 Goodies

%sysfunc(function(arg(s))<,format>);

Enables you to use standard SAS functions
not normally available to SAS Macros.
%let date=%sysfunc(today());
title
"%sysfunc(intnx(MONTH,&date,0),date9.)";
title "01JUL2002";
Version 6 Goodies - %sysfunc
Version 6 Goodies - %sysfunc
%macro printds(dset);
%if %sysfunc(exist(&dset))=1 %then %do;
proc print data=&dset;
run;
%end;
%else
%put The data set &dset does not
exist.;
%mend printds;
Version 6 Goodies - %sysfunc
Version 6 Goodies - %sysfunc
SAS Functions Not Available with %SYSFUNC and
%QSYSFUNC
DIF DIM HBOUND
IORCMSG INPUT LAG
LBOUND MISSING PUT
RESOLVE SYMGET All Variable Information
Functions
Utility Macros
Utility Macros

Simple dataset locking without SAS/SHARE

%lock_dataset

%unlock_dataset
Utility Macros - Lock
Utility Macros - Lock
%macro lock_dataset(dsn);
%global lock_dataset_rc;
%let lock_dataset_rc = 0;
%let _max_attempts = 10;
%let _attempt = 0;
%check_exists:
%if %sysfunc(exist(&dsn)) ne 1 %then
%goto missing_dsn;
%lock_dataset:
lock &dsn;
%let rc = &syslckrc;
Utility Macros - Lock
Utility Macros - Lock
%if &rc ne 0 %then %goto try_again;
%goto finish;
%try_again:
lock &dsn clear;
%let _attempt = %eval(&_attempt + 1);
%if &_attempt eq &_max_attempts %then
%goto lock_error;
%if &_attempt ne &_max_attempts %then
%let rc = %sysfunc(sleep(1,1));
%goto lock_dataset;
Utility Macros - Lock
Utility Macros - Lock
%lock_error:
%let lock_dataset_rc = 10;
%goto finish;
%missing_dsn:
%let lock_dataset_rc = 20;
%goto finish;
%finish:
%mend lock_dataset;
Utility Macros - Unlock
Utility Macros - Unlock
%macro unlock_dataset(dsn);
%global unlock_dataset_rc;
%let unlock_dataset_rc = 0;
%check_exists:
%if %sysfunc(exist(&dsn)) ne 1 %then
%goto missing_dsn;
%unlock_dataset:
lock &dsn clear;
%let rc = &syslckrc;
%if &rc ne 0 %then %goto unlock_error;
%goto finish;
Utility Macros - Unlock
Utility Macros - Unlock
%unlock_error:
%let unlock_dataset_rc = 10;
%goto finish;
%missing_dsn:
%let unlock_dataset_rc = 20;
%goto finish;
%finish:
%mend unlock_dataset;
Macro Comments
Macro Comments

Don't forget to comment your code with:
/* Standard comment type 1 */
* Standard comment type 2 ;
%* Macro comments look like this ;
Conclusion
Conclusion

The SAS Macro Language is a powerful tool
for use with the SAS programming language

Hopefully the techniques presented will be
helpful in your own application development
References
References

SAS OnlineDoc
BASE SAS; SAS Macro Language Reference

SAS Institute Training
SAS Macro Language (2 Day)
Advanced SAS Macro Programming (2 Day)

http://www.sas.com
Macro Magic III
Macro Magic III
Questions???
Macro Magic III
Macro Magic III
Presentation available on our website:
http://www.qual-it.com.au
michael_matthews@qual-it.com.au

Macros in SAS, an Introductory Slideshow

  • 1.
    Macro Magic III MacroMagic III SAUSAG Presentation (30mins)
  • 2.
    Overview Overview  Macro Magic Postershave been displayed at the last 2 SUGA presentations.  Continue the trend…  Hopefully everyone will learn something new…
  • 3.
    SAS Macro -Introduction SAS Macro - Introduction  Used to generate (often repeated or conditional) SAS code
  • 4.
    SAS Macro -Introduction SAS Macro - Introduction proc print data=db.yr2000 noobs; var month income expense; run; proc print data=db.yr2001 noobs; var month income expense; run;
  • 5.
    SAS Macro -Introduction SAS Macro - Introduction %macro myprint(dset); proc print data=&dset noobs; var month income expense; run; %mend; %myprint(db.yr2000) %myprint(db.yr2001)
  • 6.
    SAS Macro -Introduction SAS Macro - Introduction %macro myprint(dset); proc print data=&dset noobs; var month income expense; run; %mend; %myprint(db.yr2000) %myprint(db.yr2001)
  • 7.
    SAS Macro -Introduction SAS Macro - Introduction %macro myprint(dset); proc print data=&dset noobs; var month income expense; run; %mend; %myprint(db.yr2000) %myprint(db.yr2001)
  • 8.
    SAS Macro -Introduction SAS Macro - Introduction %macro myprint(dset); proc print data=&dset noobs; var month income expense; run; %mend; %myprint(db.yr2000) %myprint(db.yr2001)
  • 9.
    SAS Macro -Introduction SAS Macro - Introduction %macro myprint(dset); proc print data=db.yr2000 noobs; var month income expense; run; %mend; %myprint(db.yr2000) %myprint(db.yr2001) Executed Code
  • 10.
    Error Checking Error Checking  ProductionSAS jobs should have error checking  May need to run certain steps based on failure  &syserr, &syslibrc, &sysfilrc, &syslckrc  0=success, +ve=error, -ve=warning  %goto
  • 11.
    Error Checking Error Checking %macroupdate_db; libname db "Q:DataFiles" access=readonly; %if &syslibrc ne 0 %then %goto err_lib; proc append base=db.invoices data=work.invoices; run; %if &syserr ne 0 %then %goto err_append; %goto finish;
  • 12.
    Error Checking Error Checking %err_lib: %email_db("update_db:Library failure",admins); %goto finish; %err_append: %email_db("update_db: Append failure",admins); %goto finish; %finish: %mend update_db;
  • 13.
    Error Checking Error Checking  OPTIONSERRORABEND; is a good alternative, although has less control.  When an ERROR occurs, SAS will stop.  Beware this will close down an interactive session unexpectedly!!!
  • 14.
    Platform Checking Platform Checking  &sysscpcan be used to determine the platform  Standard macro library for all platforms, with occasional conditional code  %if &sysscp=WIN %then %do; …  %put &sysscp;
  • 15.
    Platform Checking Platform Checking %macromyalloc(file); %if &sysscp=WIN %then %do; libname data "Q:Data&file" access=readonly; %end; %else %if &sysscp=OS %then %do; libname data "PROD.Q.&file..DATA" disp=shr; %end; %mend myalloc;
  • 16.
    Version of SASChecking Version of SAS Checking  Some installations may have more than one version of SAS installed  %if &sysver=8.2 %then %do; …
  • 17.
    Call Execute Call Execute  CallExecute enables you to execute code conditionally without macro  CALL EXECUTE(arg);  The argument is executed at the next step boundary
  • 18.
  • 19.
    Call Execute Call Execute %macromyprint(style); proc print data=sasuser.houses; title "Style of '&style'"; where style="&style"; run; %mend myprint;
  • 20.
    Call Execute Call Execute procfreq data=sasuser.houses noprint; table style / out=work.styles; run;
  • 21.
    Call Execute Call Execute procfreq data=sasuser.houses noprint; table style / out=work.styles; run;
  • 22.
    Call Execute Call Execute procfreq data=sasuser.houses noprint; table style / out=work.styles; run; data _null_; set work.styles; call execute('%myprint('!!style!!');'); run;
  • 23.
    Call Execute Call Execute 67data _null_; 68 set work.styles; 69 call execute('%myprint('!!style!!');'); 70 run; NOTE: There were 4 observations read from the data set ... NOTE: CALL EXECUTE generated line. 1 + proc print data=sasuser.houses; title "Style of 'CONDO'"; where style="CONDO"; run;
  • 24.
    Version 8 Enhancements Version8 Enhancements  %scan(argument,n<,delimiter>);  Now allows negative indexes fullfile=d:DataBudget2001.xls %let file=%scan(&fullfile,-1,); file=2001.xls
  • 25.
    Version 6 Goodies Version6 Goodies  %sysfunc(function(arg(s))<,format>);  Enables you to use standard SAS functions not normally available to SAS Macros. title "%sysfunc(date(),worddate.)"; title "July 24, 2002";
  • 26.
    Version 6 Goodies Version6 Goodies  %sysfunc(function(arg(s))<,format>);  Enables you to use standard SAS functions not normally available to SAS Macros. %let date=%sysfunc(today()); title "%sysfunc(intnx(MONTH,&date,0),date9.)"; title "01JUL2002";
  • 27.
    Version 6 Goodies- %sysfunc Version 6 Goodies - %sysfunc %macro printds(dset); %if %sysfunc(exist(&dset))=1 %then %do; proc print data=&dset; run; %end; %else %put The data set &dset does not exist.; %mend printds;
  • 28.
    Version 6 Goodies- %sysfunc Version 6 Goodies - %sysfunc SAS Functions Not Available with %SYSFUNC and %QSYSFUNC DIF DIM HBOUND IORCMSG INPUT LAG LBOUND MISSING PUT RESOLVE SYMGET All Variable Information Functions
  • 29.
    Utility Macros Utility Macros  Simpledataset locking without SAS/SHARE  %lock_dataset  %unlock_dataset
  • 30.
    Utility Macros -Lock Utility Macros - Lock %macro lock_dataset(dsn); %global lock_dataset_rc; %let lock_dataset_rc = 0; %let _max_attempts = 10; %let _attempt = 0; %check_exists: %if %sysfunc(exist(&dsn)) ne 1 %then %goto missing_dsn; %lock_dataset: lock &dsn; %let rc = &syslckrc;
  • 31.
    Utility Macros -Lock Utility Macros - Lock %if &rc ne 0 %then %goto try_again; %goto finish; %try_again: lock &dsn clear; %let _attempt = %eval(&_attempt + 1); %if &_attempt eq &_max_attempts %then %goto lock_error; %if &_attempt ne &_max_attempts %then %let rc = %sysfunc(sleep(1,1)); %goto lock_dataset;
  • 32.
    Utility Macros -Lock Utility Macros - Lock %lock_error: %let lock_dataset_rc = 10; %goto finish; %missing_dsn: %let lock_dataset_rc = 20; %goto finish; %finish: %mend lock_dataset;
  • 33.
    Utility Macros -Unlock Utility Macros - Unlock %macro unlock_dataset(dsn); %global unlock_dataset_rc; %let unlock_dataset_rc = 0; %check_exists: %if %sysfunc(exist(&dsn)) ne 1 %then %goto missing_dsn; %unlock_dataset: lock &dsn clear; %let rc = &syslckrc; %if &rc ne 0 %then %goto unlock_error; %goto finish;
  • 34.
    Utility Macros -Unlock Utility Macros - Unlock %unlock_error: %let unlock_dataset_rc = 10; %goto finish; %missing_dsn: %let unlock_dataset_rc = 20; %goto finish; %finish: %mend unlock_dataset;
  • 35.
    Macro Comments Macro Comments  Don'tforget to comment your code with: /* Standard comment type 1 */ * Standard comment type 2 ; %* Macro comments look like this ;
  • 36.
    Conclusion Conclusion  The SAS MacroLanguage is a powerful tool for use with the SAS programming language  Hopefully the techniques presented will be helpful in your own application development
  • 37.
    References References  SAS OnlineDoc BASE SAS;SAS Macro Language Reference  SAS Institute Training SAS Macro Language (2 Day) Advanced SAS Macro Programming (2 Day)  http://www.sas.com
  • 38.
    Macro Magic III MacroMagic III Questions???
  • 39.
    Macro Magic III MacroMagic III Presentation available on our website: http://www.qual-it.com.au michael_matthews@qual-it.com.au