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

Dear All,

 

I have a dataset as below:

 

ID         DATE            EVENT

101    20Apr2021     Headache

101    21Apr2021     Nausea

101    20Apr2021     Headache

102    01May2021    Vomiting

102    02May2021    Vomiting

103    01Aug2021    Fatigue

104    01Aug2021    Fatigue

 

I need to flag the records where the observations in date and event column are same per each ID.

 

The output should be like the below:

 

ID         DATE            EVENT         FLAG

101    20Apr2021     Headache         Y

101    21Apr2021     Nausea      

101    20Apr2021     Headache         Y

102    01May2021    Vomiting

102    02May2021    Vomiting

103    01Aug2021    Fatigue

104    01Aug2021    Fatigue

 

Please help

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

One way

 

data have;
input ID DATE :date9. EVENT $;
format DATE date9.;
datalines;
101 20Apr2021 Headache
101 21Apr2021 Nausea
101 20Apr2021 Headache
102 01May2021 Vomiting
102 02May2021 Vomiting
103 01Aug2021 Fatigue
104 01Aug2021 Fatigue
;

data want(drop = c);
   if _N_ = 1 then do;
      dcl hash h();
      h.definekey("ID", "DATE", "EVENT");
      h.definedata("c");
      h.definedone();
      
      do until (z);
         set have end = z;
         if h.find() ne 0 then c = 1;
         else                  c + 1;
         h.replace();
      end;
   end;
   
   set have;
   
   if h.find() = 0 and c > 1 then flag = "Y";
run;

 

Result:

 

Obs  ID   DATE       EVENT     flag
1    101  20APR2021  Headache  Y
2    101  21APR2021  Nausea  
3    101  20APR2021  Headache  Y
4    102  01MAY2021  Vomiting  
5    102  02MAY2021  Vomiting  
6    103  01AUG2021  Fatigue  
7    104  01AUG2021  Fatigue  

View solution in original post

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

One way

 

data have;
input ID DATE :date9. EVENT $;
format DATE date9.;
datalines;
101 20Apr2021 Headache
101 21Apr2021 Nausea
101 20Apr2021 Headache
102 01May2021 Vomiting
102 02May2021 Vomiting
103 01Aug2021 Fatigue
104 01Aug2021 Fatigue
;

data want(drop = c);
   if _N_ = 1 then do;
      dcl hash h();
      h.definekey("ID", "DATE", "EVENT");
      h.definedata("c");
      h.definedone();
      
      do until (z);
         set have end = z;
         if h.find() ne 0 then c = 1;
         else                  c + 1;
         h.replace();
      end;
   end;
   
   set have;
   
   if h.find() = 0 and c > 1 then flag = "Y";
run;

 

Result:

 

Obs  ID   DATE       EVENT     flag
1    101  20APR2021  Headache  Y
2    101  21APR2021  Nausea  
3    101  20APR2021  Headache  Y
4    102  01MAY2021  Vomiting  
5    102  02MAY2021  Vomiting  
6    103  01AUG2021  Fatigue  
7    104  01AUG2021  Fatigue  
r3570
Obsidian | Level 7

This looks somewhat complex program. Hard for me to understand. Could you please help me with a simple easy to understand program ?

FreelanceReinh
Jade | Level 19

Hello @r3570,


@r3570 wrote:

This looks somewhat complex program. Hard for me to understand. Could you please help me with a simple easy to understand program ?


Maybe it's easier to understand if you split the task into several short steps:

/* Create a temporary unique sort key _SEQNO */

data _tmp / view=_tmp;
set have;
_seqno=_n_;
run;

/* Separate duplicate and unique ID-DATE-EVENT combinations */

proc sort data=_tmp out=dup nouniquekey uniqueout=uni;
by id date event;
run;

proc sort data=dup; by _seqno; run;
proc sort data=uni; by _seqno; run;

/* Reunite the two parts, flagging the duplicates */

data want(drop=_seqno);
set dup(in=d) uni;
by _seqno;
if d then flag='Y';
run;
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1381 views
  • 0 likes
  • 3 in conversation