Reputation: 1507
This is my first day with SAS.
I use the following code to try to create a flag which identifies when a row of my data set has the property that the field a
(character) remains the same as the corresponding field in the previous row, and the field b
(character) is different than the corresponding field of the previous row:
DATA WORK.temp1;
SET WORK.temp2;
RETAIN PREV_a
PREV_b;
IF( (PREV_a = a) AND (PREV_b NE b)) THEN DO;
FLAG = "Y";
END;
ELSE
FLAG = "N";
PREV_a = a;
PREV_b = b;
RUN;
The result is that the flag always has the value "N", even in cases where it should have the value "Y". I tested the code on a data set which I constructed manually in a preceding DATA step and it worked there, so I'm sure the logic and syntax are okay (after all, this is incredibly simple).
Thus I conclude that there is some special SAS knowledge which I lack which explains why this might not work in cases where the DATA is SET from a previous node in the project flow.
Any help is greatly appreciated!
Upvotes: 0
Views: 54
Reputation: 8513
Looks fine to me. In my test data below, only row D has the flag set to 'Y'. What would you be expecting the output to be?
Some test data:
data temp2;
input row $ a b;
datalines;
a 1 2
b 2 3
c 3 4
d 3 5
e 3 5
f 4 5
g 5 6
h 7 8
i 7 8
;
run;
Your code (note - I've added an output
statement to explicitly write the contents of the variables to the dataset prior to the final assignment of prev_a and prev_b. This makes it easier to see what is happening in the if statements):
DATA WORK.temp1;
SET WORK.temp2;
RETAIN PREV_a
PREV_b;
IF( (PREV_a = a) AND (PREV_b NE b)) THEN DO;
FLAG = "Y";
END;
ELSE
FLAG = "N";
output; /* ADDED */
PREV_a = a;
PREV_b = b;
RUN;
Upvotes: 4