|
6#

楼主 |
发表于 2011-10-15 14:48:35
|
只看该作者
Re: 请教如何判断某个变量是否存在的问题
分享一个宏,不仅能判别变量是否存在,更主要的是判别变量是字符型还是数值型变量。
[code:11hfkbjt]%macro getVarType( _sasdsn_=, _varname_=,);
%let dsid=%sysfunc(open(&_sasdsn_,i));
%global &_varname_._Type;
%let &_varname_._Type = ;
%do i=1 %to %sysfunc(attrn(&dsid,nvars));
%if %upcase(&_varname_) = %upcase(%sysfunc(varname(&dsid,&i))) %then %do;
%let &_varname_._Type = %sysfunc(vartype(&dsid,&i));
%goto _alldone_;
%end;
%end;
%_alldone_: %let rc=%sysfunc(close(&dsid));
%mend getVarType;
/*** example use of macro ***/
%getVartype( _sasdsn_=sashelp.class, _varname_=salary);
data test;
set sashelp.class;
*** determine if salary is Numeric, Character, or Does Not Exist at all;
salary_type = "&salary_Type" ;
if salary_Type = "N" then flag = "Numeric ";
else if salary_type = "C" then flag = "Character ";
else flag = "DNE ";
run;
[/code:11hfkbjt] |
|