%range is a genetic list creator. To apply data driven programming technique, we need to fetch metadata from source data dynamically, for example, to get all variables from a input dataset.
You can easily make it by
1. PROC SQL, from a SAS dictionary table, or macro variable by SELECT INTO; or
2. Proc Contents; or
3. even a smart data step with RESOLVE function and with CALL SYMPUT, like
%let namelist=; data _null_; set sashelp.class; call symput('namelist', trim(resolve('&namelist'))||' '||trim(name)); run; %put &namelist;
In my repository, there is a elegant function-like macro where SAS file processing functions like open(), close() are used, %getVar. Below follows examples to fetch variables from a dataset, based on variable type, numeric or character:
filename list url “https://raw.github.com/Jiangtang/SAS_ListProcessing/master/_ListProcessing”;
%inc list;
%put %getVar(%str(sashelp.class));
%put %getVar(%str(sashelp.class),n);
%put %getVar(%str(sashelp.class),C);
Outputs:
Name Sex Age Height Weight
<p> Age Height Weight </span> </p> <p> Name Sex </p> <p> </p></blockquote> <p> <span style="font-size: xx-small;">Another example to list all the elements from a directory using </span><a href="https://github.com/Jiangtang/SAS_ListProcessing/blob/master/dir.sas"><span style="font-size: xx-small;">%dir</span></a><span style="font-size: xx-small;"> by </span><a href="http://www.datasavantconsulting.com/roland/"><span style="font-size: xx-small;">Roland Rashleigh-Berry</span></a><span style="font-size: xx-small;">:</span> </p> <blockquote> <p> <span style="font-family: 'Courier New'; font-size: xx-small;">%put %dir(d:\test);</span> </p> </blockquote> <p> <span style="font-size: xx-small;">For more, check out the List Creating part of the portal:</span> </p> <p> <a href="https://github.com/Jiangtang/SAS_ListProcessing"><span style="font-size: xx-small;">https://github.com/Jiangtang/SAS_ListProcessing</span></a> </p>