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

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:

NAME
Age
Height
Name
Sex
Weight
Advertisements

6 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;

  2. Signe S.K. says:

    Hello

    Thank you for this. Is it possible to make a list of variable name and corresponding label name?

    • Yes, you can.

      Use PROC PRINT to look at the data set “data_info”. You’ll see that it has many columns, including “label”. Just add this to the “KEEP” option in “PROC CONTENTS”. Here is an example:

      * add labels to the variables;
      data class2;
      set sashelp.class;
      label
      name = ‘First Name’
      sex = ‘Gender’
      age = ‘Age (Years)’
      height = ‘Height (cm)’
      weight = ‘Weight (lb)’;
      run;

      * export the variable names and their position number into a data set called “data_info”;
      proc contents
      data = class2
      out = data_info (keep = name varnum label);
      run;

      * sort “data_info” by “varnum”;
      * export the sorted data set with the name “variable_names”, and keep the “name” and “label” columns;
      proc sort
      data = data_info
      out = variable_names(keep = name label);
      by varnum;
      run;

      * view the list of variables;
      proc print
      data = variable_names
      noobs;
      run;

      • Robin says:

        Is there a way to also keep the format of the variables? I tried using the keep statement in the same manner as the name and label, but it doesn’t seem to be working.

        proc contents data=qol_n1_all noprint out = data_info (keep=name format label varnum);
        proc sort data=data_info out = variable_names;
        by varnum;

        data allcontent;
        set variable_names;
        file “allcont”;
        put (_all_)( +0 );

        proc print data=allcontent;
        proc contents data=allcontent varnum;

        When I open the output file allcont, it only includes the name, label and varnum.

        And here’s an output of the contents for the allconent file:

        Variables in Creation Order

        # Variable Type Len Label

        1 NAME Char 32 Variable Name
        2 VARNUM Num 8 Variable Number
        3 LABEL Char 256 Variable Label
        4 FORMAT Char 32 Variable Format

      • Hi Robin,

        I have published a new blog post to address your question. Thank you for your patience!

        http://wp.me/p3bFTa-UA

        Eric

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: