SAS Macro : Get variable names from a dataset

Deepanshu Bhalla 2 Comments ,
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
Spread the Word!
Share
About Author:
Deepanshu Bhalla

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 worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

Post Comment 2 Responses to "SAS Macro : Get variable names from a dataset"
  1. I always liked the way you simplify the syntax with examples throughout in your website. Great work!!
    However, examples are missing here making it a little hard for me to understand.

    ReplyDelete
Next → ← Prev