Reputation: 189
I have following data:
data temp;
input id int1 int2 char1$ char2$;
cards;
1 2 3 AA BB
2 3 3 AB CC
3 4 5 AC DD
4 5 5 AD AD
5 6 7 AE FF
6 7 8 AF GG
;
run;
I want create a new column "difference" which is the result of comparing int1 to int2 and char1 to char2, "difference" is incremental in function of dissimilarities :
I want a result like this:
data result;
input id int1 int2 char1$ char2$ differences;
cards;
1 2 3 AA BB 2
2 3 3 AB CC 1
3 4 5 AC DD 2
4 5 5 AD AD 0
5 6 7 AE FF 2
6 7 8 AF GG 2
;
run;
In that example, there are only 4 columns but I had 36 columns in original Data. I start trying with a loop, I don't know if it's the correct way to solve the pb:
data result;
SET temp;
array id int1 int2 char1 char2;
DO i = 1 to dim(id);
DIFFERENCE = 0;
if int1 ^= int2 THEN DIFFERENCE = DIFFERENCE + 1;
if char1 ^= char2 THEN DIFFERENCE = DIFFERENCE + 1;
END;
RUN;
but it doesn't work , do you have an idea?
thanks a lot!
Upvotes: 0
Views: 348
Reputation: 51621
SAS will evaluate a boolean expression to 1 (TRUE) or 0 (FALSE).
So to get your output you just need to do:
data want;
set temp;
difference = (int1 ne int2) + (char1 ne char2);
run;
Upvotes: 0
Reputation: 3117
From your result table I speculate that the difference shall be equal to 1 also when int1 = int2 and char1 != char2
.
In that scenario, a simple use of the IFN() function should output the desired result.
data result;
set temp;
difference = ifn(int1=int2 and char1=char2, 0, ifn((int1 ne int2 and char1 = char2) or (int1 = int2 and char1 ne char2), 1, 2));
run;
EDIT: After DD Chen comment
data result;
set temp;
do i=1 to _n_;
difference = 0;
if int1 ne int2 then difference + 1;
if char1 ne char2 then difference + 1;
/* add all comparison statements ... */
end;
drop i;
run;
Upvotes: 1