2
votes

Hi I have table which contains around 300 variable and I need to get all the column names (variables) into one macro variable (%let vars = [list of names of those 300 columns] ).

Anybody knows how could I do that?

2

2 Answers

2
votes

SAS dictionary keep metadata, it is easy to get columns name of data with dictionary, such as:

proc sql;
   select name into: vars separated by ' ' from dictionary.columns where libname='SASHELP' and memname='CLASS';
quit;

%put &vars;

Or Vcolumn in data step:

data _null_;
   set sashelp.vcolumn(where=(libname='SASHELP' and memname='CLASS')) end=last;
   length vars $100;
   retain vars;
   vars=catx(' ', vars,name);
   if last then call symputx('vars',vars);
   run;
%put &vars;
1
votes

There is a macro for this in the MacroCore library which works as follows:

%put List of Variables=%mf_getvarlist(sashelp.class);

Reproduced below:

/**
  @file
  @brief Returns dataset variable list direct from header
  @details WAY faster than dictionary tables or sas views, and can
    also be called in macro logic (is pure macro). Can be used in open code,
    eg as follows:

        %put List of Variables=%mf_getvarlist(sashelp.class);

  returns:
  > List of Variables=Name Sex Age Height Weight

  @param libds Two part dataset (or view) reference.
  @param dlm= provide a delimiter (eg comma or space) to separate the vars

  @version 9.2
  @author Allan Bowe
  @copyright GNU GENERAL PUBLIC LICENSE v3
**/

%macro mf_getvarlist(libds
      ,dlm=%str( )
)/*/STORE SOURCE*/;
  /* declare local vars */
  %local outvar dsid nvars x rc dlm;
  /* open dataset in macro */
  %let dsid=%sysfunc(open(&libds));

  %if &dsid %then %do;
    %let nvars=%sysfunc(attrn(&dsid,NVARS));
    %if &nvars>0 %then %do;
      /* add first dataset variable to global macro variable */
      %let outvar=%sysfunc(varname(&dsid,1));
      /* add remaining variables with supplied delimeter */
      %do x=2 %to &nvars;
        %let outvar=&outvar.&dlm%sysfunc(varname(&dsid,&x));
      %end;
    %End;
    %let rc=%sysfunc(close(&dsid));
  %end;
  %else %do;
    %put unable to open &libds (rc=&dsid);
    %let rc=%sysfunc(close(&dsid));
  %end;
  &outvar
%mend;