Reputation: 371
I want to find the relationship of O & H or H & O in E1-E6. THEN the output is R6=S6-S5 else R6=S6-S4, else R6=S6-S3 else R6=S6-S2 until we find the relationship, else 0;It should repeat the same way for R5=S5-S4 else R5=S5-S3, else R5=S5-S2 else 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
Typo mistake, Edited. If E6=E5 as OH or HO relation then R6=S6-S5 else S6-S4. To find untill relationship then E5=E4 as OH or HO relation then R5=S5-S4 else S5-S3...,. If HHRO then substract O of S column with next of H column of S, Then there is no need of doing for another 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
Upvotes: 2
Views: 145
Reputation: 63424
Whatever the precise rules are, you don't want to use a macro for this. You want to use a pair of arrays.
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;
That probably needs tweaking but should roughly give you the structure to work from.
Upvotes: 1