SAS中文论坛

标题: 请教如何判断某个变量是否存在的问题 [打印本页]

作者: shiyiming    时间: 2011-10-7 21:22
标题: 请教如何判断某个变量是否存在的问题
我在写个MACRO,其中要首先判断某个变量(e.g.: y)是不是存在.
%macro ss(add=);
data a;
ptn="y/i";
ptnid=prxparse(ptn);
call prxsubstr(ptnid,&add.,position);
if position^=0 then do;
put y is existting;
end;
else do;
put y is missing;
end;
run;
%mend;
%ss(add=x y z)


可是总出错,请教高手指点如何改正,或,是不是可以用prxsubstr之外其他的function,或其他更好的方法.谢谢!
作者: shiyiming    时间: 2011-10-7 23:51
标题: Re: 请教如何判断某个变量是否存在的问题
*ptn的表达式没有定义正确
*put后输出的字符要加引号,否则会默认为是变量
*add后边的值中加上单引号或双引号
[code:u2djbiv6]%macro ss(add=);
data a;
ptnid=prxparse("/y/i");
if missing(ptnid) then do ;put "定义出错";stop ;end; *判断定义是否正确;
if prxmatch(ptnid,&add.) gt 0 then do;
put "y is existting";
end;
else do;
put "y is missing";
end;
run;
%mend;
%ss(add='x y z')[/code:u2djbiv6]
作者: shiyiming    时间: 2011-10-8 03:02
标题: Re: 请教如何判断某个变量是否存在的问题
thanks a lot and prxmatch is better.
Actually, I wanna run following code, and it gave me wrong result(y allways ), but I cannot figure out what the problem is. Thanks again.

%macro ss(add=);
%if %sysfunc(prxmatch('/y/i',"&add.")) gt 0 %then %do;
%put y is existting;
data c1; set c; run;
%end;
%else %do;
%put y is missing;
data c2(drop=x1);set c;run;
%end;
%mend;
%ss(add=x y z)
%ss(add=x  z)

ps: data c has x1 x2 x3 variables with rows. e.g.
data c;
x1=1;x2=2;x3=3;
run;
作者: shiyiming    时间: 2011-10-8 04:34
标题: Re: 请教如何判断某个变量是否存在的问题
I fixed the code and go the answer.
Thanks a lot.
作者: shiyiming    时间: 2011-10-10 23:09
标题: Re: 请教如何判断某个变量是否存在的问题
判断y是否存在的问题:
data a;input x1 x2;cards;
1 2
2 3
;
run;

%macro ss2(dataout=,add=);
%if %sysfunc(prxmatch('/y/i',"&add."))  %then %do;
data &dataout.;set a;
x=10;y=2;z=30;
put "y is existing";
run;
%end;

%else %do;
data &dataout.(drop=x);set a;
x=1;z=3;
put "y is missing";
run;
%end;
%mend;

%ss2(dataout=b1,add=x z)
%ss2(dataout=c2,add=x y z)

for the code above, I always got the same result no matter %ss2(dataout=b,add=x z) or %ss2(dataout=c,add=x y z). Could anyone help?

Thanks.
作者: shiyiming    时间: 2011-10-15 14:48
标题: 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]




欢迎光临 SAS中文论坛 (http://www.mysas.net/forum/) Powered by Discuz! X3.2