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

i have two data sets data1 has one date for one ID and data2 has all the encounters dates for the ID's. What I want is to check if IDs in data1 had any encounters dates within 7 days in data2 and if they did create a new colunm that states 1 for yes and 0 for no. 

 

data Data1;
  input id date :date9.;
  format date date9.;
  datalines;1 07JAN2018
2 28MAR2018
3 14FEB2018;
data Data2; 
input id date :date9.;
format date date9.;
datalines;
1 07JAN2018
1 17JAN2018
1 14FEB2018
2 28MAR2018
2 04APR2018
2 17APR2018
3 14FEB2018
3 18FEB2018
3 27MAR2018
;

output : 

ID  Date     SEEN
1 07JAN2018 0 2 28MAR2018 1 3 14FEB2018 1

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
data Data1;
  input id date :date9.;
  format date date9.;
  datalines;
1 07JAN2018
2 28MAR2018
3 14FEB2018
;

data Data2; 
input id date :date9.; format date date9.; 
datalines; 1 07JAN2018 1 17JAN2018 1 14FEB2018 2 28MAR2018 2 04APR2018 2 17APR2018 3 14FEB2018 3 18FEB2018 3 27MAR2018 ; proc sql; create table want as select a.*,max(0<b.date-a.date<=7) as seen from Data1 a left join Data2 b on a.id=b.id group by a.id,a.date; quit;

View solution in original post

6 REPLIES 6
novinosrin
Tourmaline | Level 20
data Data1;
  input id date :date9.;
  format date date9.;
  datalines;
1 07JAN2018
2 28MAR2018
3 14FEB2018
;

data Data2; 
input id date :date9.; format date date9.; 
datalines; 1 07JAN2018 1 17JAN2018 1 14FEB2018 2 28MAR2018 2 04APR2018 2 17APR2018 3 14FEB2018 3 18FEB2018 3 27MAR2018 ; proc sql; create table want as select a.*,max(0<b.date-a.date<=7) as seen from Data1 a left join Data2 b on a.id=b.id group by a.id,a.date; quit;
HB
Barite | Level 11 HB
Barite | Level 11
The question can be reworded as is there another encounter within 7 days as noted in data2 for the ids and dates in data 1.

I see that max(0<b.date-a.date<=7) is the magic, but I have no idea what is going on there and how that goes to a binary 0/1,
novinosrin
Tourmaline | Level 20

@HB  Good catch mate (I see that max(0<b.date-a.date<=7) is the magic) 

 

Basically any expression results to true of false in most if not all programming languages which in numbers is 1 or 0. True positive is <=7  in this case and False positive is anything otherwise. My python  prof at my college loves binary processing big time and encourages to effectively use it all the time when appropriate. Hope that helps

HB
Barite | Level 11 HB
Barite | Level 11

@novinosrin

 

I get it now. So if there is a future date that is within 7 days, b.date- a.date will be between 0 and 7 and that expression will evaluate to true (1). The criteria has to be >0 to not match to itself. Slick.

hk2013
Fluorite | Level 6

would it be possible to tell if the encounter happened on the same day but with a different reps

for example in data1 ID 1 has encounter on 7jan2018 with sam but in data2 ID1 has encounter on 7jan2018 with sam and bob 

and i want that to equal to 1 as seen?

 

 

 

data Data1;
  input id date :date9. reps;
  format date date9.;
  datalines;1 07JAN2018 sam
2 28MAR2018 bob
3 14FEB2018 sally;

data Data2; 
input id date :date9.reps; format date date9.;  datalines; 
1 07JAN2018  sam
1 07JAN2018  bob
1 14FEB2018  sally
2 28MAR2018  sam 
2 04APR2018   bob
2 17APR2018    sally
3 14FEB2018   sam
3 18FEB2018   bob
3 27MAR2018   sally
;

  

novinosrin
Tourmaline | Level 20

Do you mean this?

 

data Data1;
  input id date :date9. reps $;
  format date date9.;
  datalines;
1 07JAN2018 sam
2 28MAR2018 bob
3 14FEB2018 sally
;

data Data2; 
input id date :date9. reps $; 
format date date9.; 
datalines; 
1 07JAN2018  sam
1 07JAN2018  bob
1 14FEB2018  sally
2 28MAR2018  sam 
2 04APR2018   bob
2 17APR2018    sally
3 14FEB2018   sam
3 18FEB2018   bob
3 27MAR2018   sally
;

proc sql;
create table want as
select a.*,max(a.reps ne b.reps and a.date=b.date) as seen
from Data1 a left join Data2 b
on a.id=b.id 
group by a.id,a.date,a.reps;
quit;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 6 replies
  • 1485 views
  • 2 likes
  • 3 in conversation