SAS Macro : Get variable names from a dataset

Live Online Training : SAS Programming with 50+ Case Studies

- Explain Programming Concepts in Simple English
- Live Projects
- Scenario Based Questions
- Job Placement Assistance
- Get 10% off till Oct 26, 2017
- Batch starts from October 28, 2017

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

About Author:

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has close to 7 years of experience in data science and predictive modeling. During his tenure, he has worked with global clients in various domains like retail and commercial banking, Telecom, HR and Automotive.


While I love having friends who agree, I only learn from those who don't.

Let's Get Connected: Email | LinkedIn

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