Greetings,
I am trying to update an existing record set so I can update a flag for the most recent entry that can be used for reporting purposes and I have hit a wall and need help.
I have, and cannot alter, data in this format although it is a bit more random in the order:
data work.records;
input ToolID Date $ Outcome $ Flag;
datalines;
12345 01/01/2022 Pass 0
12345 04/01/2022 Fail 0
12345 02/01/2022 Fail 0
12345 12/01/2021 Pass 0
12345 03/01/2022 Pass 0
98765 03/01/2022 Fail 0
98765 02/01/2022 Pass 0
65471 02/01/2022 Fail 0
;
run;
data work.records;
input ToolID Date $ Outcome $ Flag;
datalines;
12345 01/01/2022 Pass 0
12345 04/01/2022 Fail 1
12345 02/01/2022 Fail 0
12345 12/01/2021 Pass 0
12345 03/01/2022 Pass 0
98765 03/01/2022 Fail 1
98765 02/01/2022 Pass 0
65471 02/01/2022 Fail 1
;
run;
data work.records;
input ToolID Date :mmddyy10. Outcome $;
datalines;
12345 01/01/2022 Pass 0
12345 04/01/2022 Fail 0
12345 02/01/2022 Fail 0
12345 12/01/2021 Pass 0
12345 03/01/2022 Pass 0
98765 03/01/2022 Fail 0
98765 02/01/2022 Pass 0
65471 02/01/2022 Fail 0
;
run;
proc sql;
create table want as select *,date=max(date) as flag
from records
group by toolid;
quit;
Use a double DO loop:
data want;
do until (last.toolid);
set have;
by toolid;
maxdate = max(maxdate,date);
end;
do until (last.toolid);
set have;
by toolid;
if date = maxdate then flag = 1;
output;
end;
drop maxdate;
run;
data work.records;
input ToolID Date :mmddyy10. Outcome $;
datalines;
12345 01/01/2022 Pass 0
12345 04/01/2022 Fail 0
12345 02/01/2022 Fail 0
12345 12/01/2021 Pass 0
12345 03/01/2022 Pass 0
98765 03/01/2022 Fail 0
98765 02/01/2022 Pass 0
65471 02/01/2022 Fail 0
;
run;
proc sql;
create table want as select *,date=max(date) as flag
from records
group by toolid;
quit;
Thank you very much. I was able to get this to work quite easily and learned about the MAX() function.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.