使用SAS prxmatch创建变量

时间:2018-11-13 01:46:08

标签: sas

我想使用prxmatch中匹配的条件来创建新的列变量。

The first variable 'NEW' is Y if: 
a. (A= "YES", B="NO" and C="PRESENT") or 
b. (B="NO" and C="MAYBE") or 
c. (B="NO" and C in ("NO/FL", "T2/FL ")) 
else N
The second variable 'NEXT' is Y if: 
A = 'NO' and B = 'NO' and E = 'Y'
else N 
and the last variable NEWER is Y if 
A = 'NEW' 

下面是我的代码,但我无法使prxmatch()正常工作

DATA TEST; 
SET TEST.TEST; 
if A = 'NO' and B = 'YES' and C = 'PRESENT' then NEW = 'Y';
else if B = 'NO' and C = 'MAYBE' then NEW = 'Y';
else if prxmatch('m/NO/FL|T2/FL/oi', C) > 0 and B = 'NO' then NEW = 'Y';
else NEW = 'N';
if A = 'NO' and B = 'NO' and E = 'Y' then NEXT = 'Y';
else NEXT = 'N';
if A = 'NEW' then NEWER = 'Y';
 RUN; 

PROC PRINT DATA = TEST; 
RUN; 

1 个答案:

答案 0 :(得分:1)

prxmatch的代码无法正常运行,因为您拥有/和/用于正则表达式的开始和结束,并且每当您具有NO / FL时,都会出错,因为它使您的正则表达式感觉到它结束了,并且它很少/失败后,还有更多的单词,因此您需要将NO / FL设置为NO \ / FL,如下面的代码所示,没有空格。

 data have;
 input A $ B $ C $;
  datalines;
  NO YES PRESENT
  NO NO MAYBE
  NO NO NO/FL
 YES NO T2/FL
 ;

 DATA TEST; 
  SET have; 
 if A = 'NO' and B = 'YES' and C = 'PRESENT' then NEW = 'Y';
 else if B = 'NO' and C = 'MAYBE' then NEW = 'Y';
 else if prxmatch('m/NO\/FL|T2\/FL/oi', C) > 0 and B = 'NO' then NEW = 'Y';
 else NEW = 'N';
 RUN; 

  /* i would try something like below just for testing purpose to see how your code 
  works*/

     DATA TEST; 
    SET have; 
  if A = 'NO' and B = 'YES' and C = 'PRESENT' then NEW = 'Yah';
 else if B = 'NO' and C = 'MAYBE' then NEW = 'Yoh';
  else if prxmatch('m/NO\/FL|T2\/FL/oi', C) > 0 and B = 'NO' then NEW = 'Yay';
  else NEW = 'N';
 RUN;