Getting a List of the Variable Names of a SAS Data Set

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:

NAME
Age
Height
Name
Sex
Weight

2 Responses to Getting a List of the Variable Names of a SAS Data Set

  1. Yossi Levy says:

    You can use the following PROC SQL code to put the list of variables into the macro variable &varlist :
    proc SQL noprint;
    select distinct name into:varlist separated by ‘ ‘
    from sashelp.vcolumn
    where libname = ‘SASHELP’ and memname = ‘CLASS’
    order by name
    ;
    quit;

Your thoughtful comments are much appreciated!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: