SAS tips & tricks #2 – Retrieving variable metadata within DATA Steps

In our last tips & tricks blogpost we looked at how the VARNUM function could be used to within a DATA Step to determine whether a variable was present within dataset and how this function could be used as an alternative to referencing  the dictionary tables or a PROC CONTENTS. Today we look at how the VARxx functions can be used in combination with the VARNUM function to determine other attributes about variables in a dataset.

The additional functions are shown in the table below and each can be used to retrieve a specific piece of metadata:

VARFMT Returns the format that is assigned to a SAS data set variable.
VARINFMT Returns the informat that is assigned to a SAS data set variable.
VARLABEL Returns the label that is assigned to a SAS data set variable.
VARLEN Returns the label that is assigned to a SAS data set variable.
VARNAME Returns the length of a SAS data set variable.
VARTYPE Returns the length of a SAS data set variable.

The example below shows how to use VARTYPE and VARLEN functions to retrieve the type and length of variables.

DATA  _null_;
  dsid=OPEN("sashelp.class");
  CALL SYMPUT("name_length",VARLEN(dsid,VARNUM(dsid,1));
  CALL SYMPUTX("height_type",VARTYPE(dsid,VARNUM(dsid,"height")));
RUN;

Note that in the VARLEN statement we specify the column number of the variable we are interested in. In the VARTYPE example we retrieve the column number by first using the VARNUM function.

All of these functions appear under the SAS SAS File I/O Category. More information about these functions is available within the SAS (R) Language Reference: Dictionary

Leave your comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>