When it comes to writing wrappers around data steps and procedures in SAS, SAS macros might still be the primary choice for most SASors. In the example below, I am going to show other alternatives to accomplish such task.

First of all, let’s generate a toy SAS dataset as below. Based on different categories in the variable X, we are going to calculate different statistics for the variable Y. For instance, we will want the minimum with X = “A”, the median with X = “B”, the maximum with Y = “C”.

data one (keep = x y); array a{3} $ _temporary_ ("A" "B" "C"); do i = 1 to dim(a); x = a[i]; do j = 1 to 10; y = rannor(1); output; end; end; run; /* Expected Output: x y stat A -1.08332 MIN B -0.51915 MEDIAN C 1.61438 MAX */

Writing a SAS macro to get the job done is straightforward with lots of “&” and “%” that could be a little confusing for new SASors, as shown below.

%macro wrap; %local list stat i x s; %let list = A B C; %let stat = MIN MEDIAN MAX; %let i = 1; %do %while (%scan(&list, &i) ne %str()); %let x = %scan(&list, &i); %let s = %scan(&stat, &i); proc summary data = one nway; where x = "&x"; class x; output out = tmp(drop = _freq_ _type_) &s.(y) = ; run; %if &i = 1 %then %do; data two1; set tmp; format stat $6.; stat = "&s"; run; %end; %else %do; data two1; set two1 tmp (in = tmp); if tmp then stat = "&s."; run; %end; %let i = %eval(&i + 1); %end; %mend wrap; %wrap;

Other than using SAS macro, Data _Null_ might be considered another old-fashion way for the same task by utilizing the generic data flow embedded in the data step and the Call Execute routine. The most challenging piece might be to parse the script for data steps and procedures. The benefit over using SAS macro is that the code runs instantaneously without the need to compile the macro.

data _null_; array list{3} $ _temporary_ ("A" "B" "C"); array stat{3} $ _temporary_ ("MIN" "MEDIAN" "MAX"); do i = 1 to dim(list); call execute(cats( 'proc summary data = one nway; class x; where x = "', list[i], cat('"; output out = tmp(drop = _type_ _freq_) ', stat[i]), '(y) = ; run;' )); if i = 1 then do; call execute(cats( 'data two2; set tmp; format stat $6.; stat = "', stat[i], '"; run;' )); end; else do; call execute(cats( 'data two2; set two2 tmp (in = tmp); if tmp then stat = "', stat[i], '"; run;' )); end; end; run;

If we’d like to look for something more inspiring, the IML Procedure might be another option that can be considered by SASors who feel more comfortable about other programming languages, such as R or Python. The only caveat is that we need to convert values in IML into macro variables that can be consumed by SAS codes within the SUBMIT block.

proc iml; list = {'A', 'B', 'C'}; stat = {'MIN', 'MEDIAN', 'MAX'}; do i = 1 to nrow(list); call symputx("x", list[i]); call symputx("s", stat[i]); submit; proc summary data = one nway; class x; where x = "&x."; output out = tmp(drop = _type_ _freq_) &s.(y) = ; run; endsubmit; if i = 1 then do; submit; data two3; set tmp; format stat $6.; stat = "&s."; run; endsubmit; end; else do; submit; data two3; set two3 tmp (in = tmp); if tmp then stat = "&s."; run; endsubmit; end; end; quit;

The last option that I’d like to demonstrate is based on the LUA Procedure that is relatively new in SAS/Base. The logic flow of Proc LUA implementation looks similar to the one of Proc IML implementation shown above. However, passing values and tables in and out of generic SAS data steps and procedures is much more intuitive, making Proc LUA a perfect wrapper to bind other SAS functionalities together.

proc lua; submit; local list = {'A', 'B', 'C'} local stat = {'MIN', 'MEDIAN', 'MAX'} for i, item in ipairs(list) do local x = list[i] local s = stat[i] sas.submit[[ proc summary data = one nway; class x; where x = "@x@"; output out = tmp(drop = _type_ _freq_) @s@(y) = ; run; ]] if i == 1 then sas.submit[[ data two4; set tmp; format stat $6.; stat = "@s@"; run; ]] else sas.submit[[ data two4; set two4 tmp (in = tmp); if tmp then stat = "@s@"; run; ]] end end endsubmit; run;