SAS Macro : Get variable names from a dataset

Suppose you want to create a macro variable that puts all the variable names from a data set.

1. Get all the variable names from a data set
*Selecting all the variables;
proc sql noprint;
select name into : vars separated by " "
from dictionary.columns
where LIBNAME = upcase("work")
and MEMNAME = upcase("predata");
quit;
LIBNAME : Library Name
MEMNAME : Dataset Name
Note : Make sure library and dataset names in CAPS. Or you can use UPCASE function to make it in caps.

To see the variable names, use the following code :
%put variables = &vars.;
2. Get all the numeric variable names from a data set
*Selecting numeric variables;
proc sql noprint;
select name into : numvar separated by " "
from dictionary.columns
where LIBNAME = "WORK"
and MEMNAME = "PREDATA"
and type = 'num';
quit;
3. Get all the character variable  names from a data set
*Selecting character variables;
proc sql noprint;
select name into : charvar separated by " "
from dictionary.columns
where LIBNAME = "WORK"
and MEMNAME = "PREDATA"
and type = 'char';
quit;
4. Get all the variable names except ID variable
proc sql noprint;
select name into : vars separated by " "
from dictionary.columns
where LIBNAME = upcase("work")
and MEMNAME = upcase("predata")
and upcase(name) ne upcase("id");
quit;

SAS Tutorials : 100 Free SAS Tutorials

Get Free Email Updates :
*Please confirm your email address by clicking on the link sent to your Email*

Related Posts:

0 Response to "SAS Macro : Get variable names from a dataset"

Post a Comment

Next → ← Prev