Create macro variable array from SAS variable

Creating a macro variable array from the values of a SAS variable is a useful technique. Each element of a macro variable array can be a parameter to the invocation of a SAS macro. In addition to the macro variable array, the number of elements in the array must be defined as a macro variable.

Parameter-driven SAS macros are instrumental in developing an effective SAS macro library. This eliminates the need to generate multiple copies of the same code. Invocation of a macro for each variable in a SAS file, each file in a Unix directory, or for each user's unique identification number can extend your programming functionality. Generating these parameter lists can be a non-trivial task. Often the list of parameters is supplied in a SAS file and changes many times. Automating the creation of a parameter list is valuable.

The file created by a proc contents procedure can be used to demonstrate creating a macro variable array from a SAS variable. The contents of a SAS file contains all the variable names and whether a variable is numeric or character. The SAS Fifth Dimension macro Reduces the length of all character variables uses this technique.

The following code created by the proc contents creates a file named "work.CONTENTS" with the contents of the your SAS file. The variable NAME in the file work.CONTENTS is the list of variable names of your SAS file. The selection of your SAS file and defining the macro variable VNAME to the variable with list of parameters defines the macro variable array.

The following SAS macros show how to invoke the macro OUTPUT_DETAIL for each element of the macro array. Although the macro OUTPUT_DETAIL lists the value of the parameter to the SAS log each time it is invoked, it could be any of several functions.

The LISTMACS SAS macro first lists the number of parameters, the macro variable VNUM, in SAS log. It then invokes the macro OUTPUT_DETAIL for each parameter.

Please note: the SAS macro variable SYSLAST is a SAS system macro variable. It is defines as the last SAS file the one created most recently. See SAS macro variable definitions

PROC CONTENTS DATA=YOUR_SAS_FILE OUT=CONTENTS NOPRINT;
%let VNAME=NAME                                                    ;
data &SYSLAST                                                      ;
 set &SYSLAST end=eof                                              ;
VNAME="&VNAME                                                      ;
IF      _n_<10     THEN SUBSTR(VNAME,LENGTH(VNAME)+1,1)=PUT(_n_,1.);
ELSE IF _n_<100    THEN SUBSTR(VNAME,LENGTH(VNAME)+1,2)=PUT(_n_,2.);
ELSE IF _n_<1000   THEN SUBSTR(VNAME,LENGTH(VNAME)+1,3)=PUT(_n_,3.);
CALL SYMPUT(VNAME,SUBSTR(&VNAME,1,LENGTH(&VNAME)))                 ;
IF EOF THEN DO                                                     ;
   IF      _n_<10     THEN CALL SYMPUT('VNUM',PUT(_n_,1.))         ;
   ELSE IF _n_<100    THEN CALL SYMPUT('VNUM',PUT(_n_,2.))         ;
   ELSE IF _n_<1000   THEN CALL SYMPUT('VNUM',PUT(_n_,3.))         ;
END                                                                ;
run                                                                ;
%MACRO OUTPUT_DETAIL(NAME);
%PUT %STR(--->NAME=&NAME<); 
%MEND OUTPUT_DETAIL       ; 
%MACRO LISTMACS             ;
%PUT %STR(--->VNUM=&VNUM)   ;
%do II=1 %TO &VNUM          ;
%OUTPUT_DETAIL(&&&VNAME&II) ;
%END                        ;
%MEND LISTMACS              ;
%LISTMACS                   ;