BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
elias_2020
Fluorite | Level 6
data offenders;
   input  id   offence_DATE mmddyy10. sex$;
   format offence_DATE  mmddyy10.;
   datalines;
1 05/23/05  M 
1 08/01/09  M
2 04/18/01  F
2 06/10/12  F
3 01/01/11  M
;
run;



data HOSPITAL;
   input  id   ADMIT_date mmddyy10. ;
   format ADMIT_date mmddyy10.;
   datalines;
1 10/21/03      
1 06/29/05      
1 02/03/07 
1 09/21/08
1 08/17/09         
3 12/31/10      
3 01/02/13      
;
run;

/* I would like to know any hospital visit between the two offences (if the offenders has two) or any visit after the first offence (fot those who has only one offence such as ID=3)*/
/* I would like to keep the offender who doesn’t have any visit in the data*/ 
/*The result should be appear as below

ID  offence_DATE   ADMIT_date   sex
1   05/23/05                    M
1                   06/29/05    M
1                   02/03/07    M
1                   09/21/08    M
1  08/01/09                     M
2  04/18/01                     F 
2  06/10/12                     F
3  01/01/11                     M
3                   01/02/13    M               

/*

Could you please suggest for the best method for merging such this kind of the data.

 

 

 

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
RM6
Obsidian | Level 7 RM6
Obsidian | Level 7
here is a sample code which would do good with the example, but you will need to modify it when there are more than 2 offence_date.

proc sql;
create table admit as
select *
from (
select distinct a.id,admit_date format mmddyy10.,admit_date as offence_date format mmddyy10.,sex
from (
select id,offence_date format mmddyy10., min(offence_date) as min format mmddyy10.,
case when count(id) =1 then today() else max(offence_date) end as max format mmddyy10.,
sex
from offenders
group by id
) a
left join hospital b
on a.id= b.id
and admit_date between min and max
)
where admit_date is not null
order by id,offence_date
;
quit;

proc sort data=offenders; by id offence_date; run;

data fin;
merge offenders admit;
by id offence_date;
if offence_date= admit_date then call missing(offence_date);
run;


View solution in original post

4 REPLIES 4
RM6
Obsidian | Level 7 RM6
Obsidian | Level 7
here is a sample code which would do good with the example, but you will need to modify it when there are more than 2 offence_date.

proc sql;
create table admit as
select *
from (
select distinct a.id,admit_date format mmddyy10.,admit_date as offence_date format mmddyy10.,sex
from (
select id,offence_date format mmddyy10., min(offence_date) as min format mmddyy10.,
case when count(id) =1 then today() else max(offence_date) end as max format mmddyy10.,
sex
from offenders
group by id
) a
left join hospital b
on a.id= b.id
and admit_date between min and max
)
where admit_date is not null
order by id,offence_date
;
quit;

proc sort data=offenders; by id offence_date; run;

data fin;
merge offenders admit;
by id offence_date;
if offence_date= admit_date then call missing(offence_date);
run;


Ksharp
Super User
data offenders;
   input  id   offence_DATE mmddyy10. sex$;
   date=offence_DATE ;
   format offence_DATE  date mmddyy10.;
   datalines;
1 05/23/05  M 
1 08/01/09  M
2 04/18/01  F
2 06/10/12  F
3 01/01/11  M
;
run;



data HOSPITAL;
   input  id   ADMIT_date mmddyy10. ;
   date=ADMIT_date;
   format ADMIT_date date mmddyy10.;
   datalines;
1 10/21/03      
1 06/29/05      
1 02/03/07 
1 09/21/08
1 08/17/09         
3 12/31/10      
3 01/02/13      
;
run;

data temp;
 set offenders HOSPITAL;
 by id date;
 drop date;
run;
data want;
 merge temp(drop=sex) temp(keep=id sex where=(sex is not missing));
 by id;
run;
elias_2020
Fluorite | Level 6

Thank you so much Ksharp.

 

It gives me the correct solution as well.

 

 

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 1249 views
  • 1 like
  • 3 in conversation