嗨我有一个包含大约300个变量的表,我需要将所有列名(变量)放到一个宏变量(%let vars = [list of names of those 300 columns]
)中。
有人知道我该怎么做?
答案 0 :(得分:1)
Boemska Macro Core library中有一个宏,其工作原理如下:
%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;
答案 1 :(得分:1)
SAS字典保存元数据,很容易用字典获取数据的列名,例如:
proc sql;
select name into: vars separated by ' ' from dictionary.columns where libname='SASHELP' and memname='CLASS';
quit;
%put &vars;
数据步骤中的或Vcolumn:
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;