解析表中的所有列名并放入宏变量 (SAS 9.4) 中

  • 本文关键字:变量 SAS sas sas-macro
  • 更新时间 :
  • 英文 :


嗨,我有包含大约 300 个变量的表,我需要将所有列名(变量(放入一个宏变量 ( %let vars = [list of names of those 300 columns] (。

有人知道我该怎么做吗?

SAS字典保留元数据,很容易用字典获取数据的列名,例如:

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

或数据步骤中的 V 列:

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;

在 MacroCore 库中有一个宏,其工作原理如下:

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

转载如下:

/**
  @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;

最新更新