Coding in the fast lane with data-driven macro calls

0

The simple PRINT macro below prints a selected dataset:

%macro print(data=&syslast,obs=5);
   proc print data=&data(obs=&obs); 
      title "%upcase(&data)"; 
   run;
%mend print;

Suppose you want to print every dataset in the library.  Would you enjoy typing a macro call for every dataset in the library?

Only if you enjoy coding in the slow lane!

%print(data=orion.country)  
%print(data=orion.club_members)  
%print(data=orion.bizlist)  ...

I don't know about y'all, but, whether I'm driving my Corvette or coding in SAS, I hate the slow lane!

The PRINTLIB macro below puts you in the middle lane by issuing automated data-driven calls to the PRINT macro:

%macro printlib(libname);
 
   proc sql noprint;
	select "&libname.." || memname into :dsn1-
   		from dictionary.tables
			where libname="%upcase(&libname)";
   quit;
 
   %do i=1 %to &sqlobs;
      %print(data=&&dsn&i)   
   %end;
 
%mend printlib;

While the above macro has power, it suffers from the weight of excessive code; poor power-to-weight ratio!

Would you believe me if I said you could issue identical data-driven macro calls with a 3-line datastep (5 lines if you count the DATA and RUN statements) and no %DO loop? Well, you can! The magic of the EXECUTE routine will put you in the fast lane with a lean mean power-to-weight ratio. Learn all the details, as well as a dangerous pothole to avoid, by attending our tire-squealing new course, SAS Macro Language 2: Advanced Techniques.

photo credit: FastLane_YiLiang, SlowLane_LarsPlougmann // attribution: creative commons

Share

About Author

Jim Simon

Principal Technical Training Consultant

Jim Simon is a principal instructor and course developer for SAS Education. Jim has a bachelor's degree from UCLA and a master's degree from California State University at Northridge. Prior to joining the SAS Irvine office in 1988, Jim was an instructor at Ventura College and a SAS programmer at The Medstat Group in Santa Barbara. Jim's areas of specialization include the DATA step, application development, web enablement, and the SAS macro language. A native of Southern California, Jim enjoys anything in the warm California sun. On weekends, Jim loves jumping in his Corvette, turning up the stereo, and cruising Pacific Coast Highway, top down, South to Laguna Beach or North to his old home town, Santa Barbara.

Comments are closed.

Back to Top