Search code examples
sassas-macro

Parse all column names from table and put into macro variable (SAS 9.4)


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?


Solution

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