宏做循环直到sas

时间:2014-03-19 19:58:17

标签: sas

我想找到O& amp;的关系。 H或H&在E1-E6中的O.那么输出是R6 = S6-S5否则R6 = S6-S4,否则R6 = S6-S3否则R6 = S6-S2直到我们找到关系,否则为0;它应该以相同的方式重复R5 = S5-S4否则R5 = S5-S3,否则R5 = S5-S2,否则R5 = S5-S1

SL  E1  E2  E3  E4  E5  E6  S1  S2  S3  S4  S5  S6
1   O   H                   1   4               
2   R   H   O   H           1   6   4   8       
3   O   O   H   O   O   H   2   4   6   7   9   8
4   H   O   R   H   O   H   4   5   4   5   8   7
5   O   O   O   H   H   O   3   5   6   7   7   5
6   O   O   H               2   4   7           
7   H   R   H   O           3   5   6   9       
8   O   H   H   R   O   H   6   7   8   8   6   9
9   O   R   H   H   R       4   7   8   9       
10  R   H   R   O   H       5   8   4   8   9   

错字错误,编辑。如果E6 = E5作为OH或HO关系,则R6 = S6-S5,否则S6-S4。为了找到直到关系,则E5 = E4为OH或HO关系,则R5 = S5-S4,否则为S5-S3 ...... ,.如果HHRO然后用S的H列中的下一个H对S列的O进行减去,则不需要为另一个H做。

Example output edited

   SL   R2  R3  R4  R5  R6
1   3               
2       -2  4       
3       2       3   -1
4   1       1   3   -1
5           1       -2
6       3           
7           3       
8   1           -2  3
9               5   
10          0   1   

1 个答案:

答案 0 :(得分:1)

无论准确的规则是什么,您都不想为此使用宏。你想使用一对数组。

data have;
infile datalines missover;
input
SL  E1  $ E2 $  E3 $ E4 $ E5  $ E6 $ S1  S2  S3  S4  S5  S6
;
datalines;
1   O   H   X   X   X   X   1   4               
2   R   H   O   H   X   X   1   6   4   8       
3   O   O   H   O   O   H   2   4   6   7   9   8
4   H   O   R   H   O   H   4   5   4   5   8   7
5   O   O   O   H   H   O   3   5   6   7   7   5
6   O   O   H   X   X   X   2   4   7           
7   H   R   H   O   X   X   3   5   6   9       
8   O   H   H   R   O   H   6   7   8   8   6   9
9   O   R   H   H   R   X   4   7   8   9       
10  R   H   R   O   H   X   5   8   4   8   9   
;;;;
run;

data want;
set have;
array e e1-e6;
array s s1-s6;
array r r2-r6;
matchfound=0;
do _t = dim(s) to 2 by -1;
  do _u = _t-1 to 1 by -1 until (matchfound); *iterate over each lower value of e[];
    *put _n_= e[_t]= e[_u]= _t= _u=;  *uncomment for debugging;
    if cats(e[_t],e[_u]) in ('HO','OH') then do; *if OH or HO;
      matchfound=1;  *set match found so we leave the inner loop;
      r[_t-1] = s[_t] - s[_u]; *calculate R;
    end;
  end;
  if _u >= 1 then _t = _u+1;   *u+1 because it is decremented one time too many automatically;
  else if matchfound=1 then leave;  *if _u =0 and matchfound=1 then we matched the final (u=1);
  matchfound=0; *reset for next lower check;
end;
run;

这可能需要调整,但应该大致为您提供工作结构。