![]() | ![]() | ![]() | ![]() | ![]() |
This sample uses macro logic to create one global macro variable that contains a list of all variables that exist within a SAS data set. This can be useful if a variable list is needed within an IN operator on a WHERE clause. %SYSFUNC is used with various SAS functions to retrieve the number of variables that exist within the data set and to pull out each variable name.
There are other ways to accomplish this task, for example using PROC SQL and DICTIONARY.COLUMNS, but that introduces non-macro code within the macro. As this macro is written it can be called anywhere (since it only contains macro code), but if the macro contained PROC SQL then it is limited as to where it can be called.
If an SQL way is preferred here is an example:
proc sql noprint;
select distinct name
into : varlist separated by ' '
from dictionary.columns
where libname='WORK' and memname='ONE';
quit;
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
/* Sample data */
data one;
input x y;
datalines;
1 2
;
%macro lst(dsn);
%local dsid cnt rc;
%global x;
%let x=;
/* Open the data set */
%let dsid=%sysfunc(open(&dsn));
/* The variable CNT will contain the number of variables that are in the */
/* data set that is passed in. */
%let cnt=%sysfunc(attrn(&dsid,nvars));
/* Create a macro variable that contains all dataset variables */
%do i = 1 %to &cnt;
%let x=&x %sysfunc(varname(&dsid,&i));
%end;
/* Close the data set */
%let rc=%sysfunc(close(&dsid));
%mend lst;
/* Pass in the name of the data set */
%lst(one)
%put macro variable x = &x;
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
Output to log macro variable x = x y
| Type: | Sample |
| Topic: | SAS Reference ==> Macro |
| Date Modified: | 2009-09-09 10:18:50 |
| Date Created: | 2005-01-26 16:17:39 |
| Product Family | Product | Host | SAS Release | |
| Starting | Ending | |||
| SAS System | Base SAS | All | 8 TS M0 | n/a |





