Hi all,
I have a dataset as below.
data have;
input col1 col2;
datalines;
1001 1.5
1001 1
1001 1.5
1001 1.5
1001 1.5
1001 5
1001 5
1002 1.5
1002 4.25
1002 2.5
1002 2.5
1002 2.5
1002 1.5
1002 1.5
1003 6.5
1003
1003 1.5
1003 1.5
1003 5
1003 7
1003 2.1
1003 2.5
1003 2.5
;
run;
I would like to flag the first record (out of last 2 records by grouping col2 if repetitive).
eg: for 1001 even though there are 1.5 is repeated, my focus is on last 2 observations and flag the 6th observation as 6th and 7th record have same value 5. for 1002, flag the 11th observation, as 11th and 12th rec has the value 1.5.
If last 2 observations are repeated, I would like to flag the previous record to the last record by each col1 group.
What gets flagged when the last two records do not have equal values?
The above question notwithstanding, this is really a simple problem when the last two records are equal, you can flag the LAST one and not the next-to-last one — unless there are more variables involved which might not be equal, you didn't say anything like that however. Is that the case?
Here's one way:
data want;
n_recs = 0;
do until (last.col1);
set have;
by col1;
n_recs + 1;
end;
next_to_last = n_recs - 1;
last_value = col2;
n_recs = 0;
do until (last.col1);
set have;
by col1;
n_recs + 1;
if n_recs = next_to_last and last_value = col2 then flag=1;
else flag = .;
output;
end;
run;
Looks like you want to flag the FIRST that is not also the LAST.
data want;
set have ;
by col1 col2 notsorted;
flag = first.col2 and not last.col2;
run;
Obs col1 col2 flag 1 1001 1.50 0 2 1001 1.00 0 3 1001 1.50 1 4 1001 1.50 0 5 1001 1.50 0 6 1001 5.00 1 7 1001 5.00 0 8 1002 1.50 0 9 1002 4.25 0 10 1002 2.50 1 11 1002 2.50 0 12 1002 2.50 0 13 1002 1.50 1 14 1002 1.50 0 15 1003 6.50 0 16 1003 1003.00 0 17 1003 1.50 0 18 1003 5.00 0 19 1003 7.00 0 20 1003 2.10 0 21 1003 2.50 1 22 1003 2.50 0
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.