Search notes:

SAS programming, function varname

The following example uses attrn(…, nvars) to determine the number of variables in a data set and then iterates over the data set's variables and determins their name with varname.
%macro tq84_dataSetVarNames(ds);

  %local varList;

  %let fid = %sysFunc(open(&ds));

  %if &fid %then %do;
      %do i=1 %to %sysFunc(attrn(&fid, nvars));
          %let varList = &varList %sysFunc(varName(&fid, &i));
      %end;

      %let fid = %sysFunc(close(&fid));
  %end;

  &varList;

%mend tq84_dataSetVarNames;


%put %tq84_dataSetVarNames(tq84_ds); /* foo bar baz num */
Github repository about-SAS, path: /programming/functions/varname.sas

See also

Compare with vname which returns the name of a data step variable.
functions

Index