【发布时间】:2018-07-23 09:38:30
【问题描述】:
将给定数据集的所有列名放入宏变量的最简单方法是什么?
另一个问题,我应该如何将所有“数字”列的名称放入一个宏变量中,并将所有字符列的名称放入另一个宏变量中?
【问题讨论】:
标签: sas
将给定数据集的所有列名放入宏变量的最简单方法是什么?
另一个问题,我应该如何将所有“数字”列的名称放入一个宏变量中,并将所有字符列的名称放入另一个宏变量中?
【问题讨论】:
标签: sas
根据您的操作,您还可以使用_character_ 引用所有字符变量,使用_numeric_ 引用数字变量。
*doesn't do anything but illustrates how to reference all variables of a specific type;
data want;
set sashelp.class;
array _c1(*) _character_;
array _n1(*) _numeric_;
run;
【讨论】:
如果您需要为多个表执行此操作,那么您可以尝试为此目的定义一个宏,例如:
%macro get_cols(lib,mem,mvar,type);
%global &mvar;
proc sql noprint;
select
name
into
:&mvar separated by ' '
from
dictionary.columns
where
libname eq upcase("&lib")
and memname eq upcase("&mem")
%if %upcase(&type) ne ALL %then
and upcase(type) eq upcase("&type");
;
quit;
%put &mvar = &&&mvar;
%mend get_cols;
然后你可以根据需要调用它:
/* character variables */
%get_cols(sashelp,class,cvars,char);
/* numeric variables */
%get_cols(sashelp,class,nvars,num);
/* all variables */
%get_cols(sashelp,class,vars,all);
【讨论】:
使用带有 dictionary.columns 的 proc sql into 子句是为您的目的制作宏变量的最简单方法之一。下面的查询使用 sashelp.class,你可以使用你的表名和库名来代替
/* for all variables*/
Proc sql noprint;
select name into :macvar1 separated by ',' from
dictionary.columns
where upcase(memname) = 'CLASS'
and upcase(libname) = 'SASHELP';
/* for numeric variables*/
Proc sql noprint;
select name into :macvar2 separated by ',' from
dictionary.columns
where upcase(memname) = 'CLASS'
and upcase(libname) = 'SASHELP'
and upcase(type) = 'NUM';
/* for character variables*/
Proc sql noprint;
select name into :macvar3 separated by ',' from
dictionary.columns
where upcase(memname) = 'CLASS'
and upcase(libname) = 'SASHELP'
and upcase(type) = 'CHAR';
%put value of all variables is &macvar1;
%put value of numeric variables is &macvar2;
%put value of character variables is &macvar3;
【讨论】: