data vv;
set v;
length var $32.;
i=1;
var=scan(s,i,'-~=>< ');
do while ( compress(var)~="");
s1=upcase(substr(var,1,1));
s2=upcase(substr(var,2,1));
if ('A'<=s1<='Z' or (s1="_" and 'A'<=s2<='Z') ) and lowcase(var) not in ('and' 'or') then output;
i=i+1;
var=scan(s,i,'-~=> ');
end;
drop s1 s2 i;
run;[/code:xv7tjums]作者: shiyiming 时间: 2010-4-8 17:47 标题: Re: 如何从表达式中提取变量 那我就来个正则表达式版本的吧..
[code:1ih262qw]data v;
length s $50.;
s="age2~=";
output;
s='_age>60 ';
output;
s='age>60 And sex=1';
output;
s='time1-time0>time24';
output;
run;
data prx_v;
set v;
length v $50;
RE = prxparse("/(\W*)(\w+)(\W*)/");
do while (^missing(s));
position = prxmatch(RE, s);
if position ^= 0 then
do;
call prxposn(RE, 2, start, length);
if start ^= 0 then v = substr(s, start, length);
call prxposn(RE, 3, start, length);
s = substr(s, start + length);
if lowcase(v) not in ('not', 'and', 'or', 'eq', 'ne', 'gt', 'ge', 'lt', 'le', 'in')
and anydigit(v) ~= 1
then output;
end;
end;
prxfree(RE);
keep v;
run;
[/code:1ih262qw]作者: shiyiming 时间: 2010-4-8 18:26 标题: Re: 如何从表达式中提取变量 %let aaa=age~=. and age>60; 从这个表达式中得到age
%let bbb=age>60 and sex=1 从这个表达式得到age sex
%let ccc=time1-time0>0 等
如何通过macro实现?作者: shiyiming 时间: 2010-4-21 08:11 标题: Re: 如何从表达式中提取变量 有什么好的方法通过macro实现呢