Getting a List of the Variable Names of a SAS Data Set
January 6, 2015 6 Comments
Update on 2017-04-15: I have written a new blog post that obtains the names, types, formats, lengths, and labels of variables in a SAS data set. This uses PROC SQL instead of PROC CONTENTS. I thank Robin for suggesting this topic in the comments and Jerry Leonard from SAS Technical Support for teaching me this method.
Getting a list of the variable names of a data set is a fairly common and useful task in data analysis and manipulation, but there is actually no procedure or function that will do this directly in SAS. After some diligent searching on the Internet, I found a few tricks within PROC CONTENTS do accomplish this task.
Here is an example involving the built-in data set SASHELP.CLASS. The ultimate goal is to create a new data set called “variable_names” that contains the variable names in one column.
The results of PROC CONTENTS can be exported into a new data set. I will call this data set “data_info”, and it contains just 2 variables that we need – “name” and “varnum“.
- “name” is the column of variables in alphabetical order
- “varnum” is the number that indicates the variable’s position in the data set
Once we export “data_info”, we will sort it by “varnum” to re-order the variable names in the position of appearance/creation in the data set.
* export the variable names and their position number into a data set called "data_info"; proc contents data = sashelp.class noprint out = data_info (keep = name varnum); run; * sort "data_info" by "varnum"; * export the sorted data set with the name "variable_names", and keep just the "name" column; proc sort data = data_info out = variable_names(keep = name); by varnum; run; * view the list of variables; proc print data = variable_names noobs; run;
Here is what you see in the Results Viewer: