执行循环以在SAS中找到匹配项

时间:2014-04-09 17:28:04

标签: sas

我想创建RT1到RT(最后)的列,以及TSN之间的差异。 EV应该是HIS并且先前的EV应该是HIS或OH然后RT2 = TS2-TS1 ELSE IF EV1 ne OH然后EV2 = TS1 ELSE O.  EV(有HIS)与减量EV匹配以找到匹配(OH或HIS),如果找到匹配则TS的其他差异转到下一个减量以找到匹配。

SlN EV1 EV2 EV3 EV4 EV5 EV6 EV7 EV8 TS1 TS2 TS3 TS4 TS5 TS6 TS7 TS8
1   OH  HIS HIS OH  OH  HIS OH  HIS 2   3   5   8   10  15  17  19
2   HIS HIS OH                      5   6   9                   
3   HIS OH                          1   3                       
4   HIS                             2                           
5   OH                              3                           
6   OH  HIS                         2   5                       
7   HIS HIS                         2   4                       
8   OH  OH                          3   4   6   9   9           
9   HIS HIS OH  HIS HIS             2   5   7                   
10  OH  OH  HIS                             

EX:SL:1 R1 = 0(becoz EV1 ne HIS),R2 = TS2-TS1(EV2 = HIS,EV1 = HIS或OH),R3 = TS3-TS2(EV3 = HIS,EV2 = HIS), R4 = TS6-TS5(EV4 ne HIS,EV5 ne HIS So,EV6 = HIS(匹配))

输出样本:

SLN R1  R2  R3  R4  R5
1   2   1   2   5   2
2   5   1           
3   1               
4   2               
5   0               
6   0   3           
7   2   3           
8   0               
9   3   1   3   0   
10  0   2           

1 个答案:

答案 0 :(得分:0)

你在想这样的事情......

DATA dataout;
SET datain;

ARRAY Rvar[8] r1-r8
        Evar[8] EV1-EV8
        TsVar[8] TS1-TS8;

DO i=1 TO 8;
    IF i=1 AND EV1="HIS" THEN Rvar[i]=TS1; 
    ELSE IF Evar[i]="HIS" AND Evar[i-1] IN ("HIS", "OH") THEN Rvar[i] = TsVar[i] - TsVar[i-1];
    ELSE Rvar[i]=.;
END;
RUN;