Pages

SyntaxHighlighter

Tuesday, December 5, 2017

DOSUBL() - execute macro code in a data step

The DOSUBL() function, new in SAS 9.4, requires us to rethink the way we look at passing values to a SAS macro. Previously I would create a series of macro variables using PROC SQL with the INTO clause such as :macvar1 - :macvar999 or by using CALL SYMPUTX().

If you try to pass data set variables into a macro they are rendered as the column names, not the column values. The way I used to do this required a %DO loop inside a macro. Now I can use the CATS() function to concatenate strings inside the DOSUBL function. As you can see in the below code, I am passing in a data set variable directly into a macro and using the RESOLVE() function to render its value. The return value of DOSUBL is zero if it was successful.

%macro createfile( filename =, text = ) ;
   filename temp "&filename." ;
   data _null_ ;
     file temp ;
     if not missing( "&text." ) then put "&text." ;
   run ;
   filename temp clear ;
%mend ;

%let path = %sysget(HOME) ;
data files ;
   input filename $64. ;
   datalines ;
&path./a.txt
&path./b.txt
;
run ;

/* Old technique */

%macro sql1() ; proc sql noprint ; select filename into :file1 - :file999 from files ; quit ; %do I = 1 %to &sqlobs. ; %createfile( filename = &&file&i ) ; %end ; %mend ; %sql1()

/*New technique */

data _null_ ;   set files ;   rc = dosubl(cats('%createfile(filename=',resolve(filename),')')); run ;
 

1 comment: