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;
Related Posts
About Author:

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he has worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and Human Resource.

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

Post a comment

Next → ← Prev
Love this Post? Spread the Word!
Share