Process Metadata: Numbered (from Fraktal SAS Programming)
The numbered approach starts by storing the unique values in an intermediate table. The lines of this table are then assigned with the SYMPUT call routine to numbered MACRO variables using the record number read from the automatic data step variable "_N_".
/* generate the list itself */ proc sql noprint; create table age_list as select distinct age from sashelp.class ; quit;
data _null_; set age_list; call symput("age_grp"||left(put(_N_,8.)),trim(left(put(age,8.)))); run;
There is probably no less elegant way to determine the number of values but we will use it here for demonstration purposes.
Nevertheless, this method is definitely indicated when all lines read are used as does the code presented in step 2 before.
/* obtain the number of elements */ data _null_; set age_list end = eof; if eof then do; call symput("age_grps",trim(left(put(_N_,8.)))); end; run;
First, utilization seems pretty similar to the list approach, since the DO loop remains unchanged. However, the re-construction of parameter names from the loop variable AGE_INDX requires multiple ampersands.
The most simple, and yet correct, explanation says, that the ampersand is parameter name and value as well, i.e. the MACRO variable with name "&" resolves to "&.
Thus, the double ampersand use "&&" produces all required parameter references starting with AGE_GRP1.
/* utilize list elements */ %DO age_indx = 1 %TO &AGE_GRPS.; proc print noobs data = sashelp.class ; where age = &&AGE_GRP&AGE_INDX.; run; %END;