BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SAS_USER_928
Obsidian | Level 7

Hi,

I have the following scenario and am stuck with creating the output. 

 

A patient is required to perform one of two labs per visit. Therefore, I have 2 observations per patient per visit, one as DONE and other as NOT DONE, in the input data set. I'm trying to flag patients' lab status per visit based on whether the lab was performed or not. If only one lab was DONE (at any visit) then flag it as DONE, else NOT DONE. Therefore, The output data set will have only 1 observation per patient per visit flagged either as DONE or NOT DONE depending on the above criteria. 

 

E.g., below is the input data set with 2 patients:

PatientIDVisitIDDateLabStatus
A11/1/2022Done
A11/1/2022Not Done
A21/5/2022Done
A21/5/2022Not Done
B11/15/2022Done
B11/15/2022Not Done
B21/20/2022Not Done
B21/20/2022Not Done

 

OUTPUT Data Set: 

PatientIDVisitIDFlag
A1Done
A2Done
B1Done
B2Not Done

 

Thank you all for the help.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Since D comes before N use the MIN() aggregate function.

 

proc sql;
  create table WANT as
    select paitentid,visitid,min(labstatus) as Flag
    from have
    group by patientid,visitid
  ;
quit;

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

Since D comes before N use the MIN() aggregate function.

 

proc sql;
  create table WANT as
    select paitentid,visitid,min(labstatus) as Flag
    from have
    group by patientid,visitid
  ;
quit;
seemiyah
Fluorite | Level 6

....and if we weren't lucky enough to have the status in the correct alphabetical order, something like the following would also work.

proc sql;
  create table want as
    select  patientid
            ,visitid
            ,ifc(sum(labstatus='Done'),'Done','Not Done') as Flag
    from have
    group by patientid
            ,visitid
  ;
quit;
SAS_USER_928
Obsidian | Level 7

Thank you so much both of you for your prompt responses. Actually both solutions worked for me and wanted to mark both as accepted solutions but the platform did not let me do so. Again, thank you so so much.

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
  • 651 views
  • 3 likes
  • 3 in conversation