BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
heraldic2
Calcite | Level 5

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; 

 

 
What I need to do is update the flag to be equal to 1 where it is the records max(date)
 
 

 

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; 

 

 
I have tried creating a temp table and hold the max date and tool id, but I keep hitting a wall on how to update the flag.  Nothing I have tried has been successful at doing the update, but that makes sense as I am only so so when it comes to SAS programming and I tend to use the PROC SQL more then anything else.

There has to be a simple way to do this!  HELP!!
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
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;
--
Paige Miller

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

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;
PaigeMiller
Diamond | Level 26
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;
--
Paige Miller
heraldic2
Calcite | Level 5

Thank you very much.  I was able to get this to work quite easily and learned about the MAX() function.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 329 views
  • 3 likes
  • 3 in conversation