BookmarkSubscribeRSS Feed
raja777pharma
Fluorite | Level 6

Hi,

 

I have two dataset and need to be assign value based on date ranges .

 

subjectSiteidTestdate
1001201LDH12-Aug-24
1001201LDH22-Aug-24
1001201LDH31-Sep-24
1001201ALT12-Aug-24
1001201ALT12-Aug-24
1002202LDH18-Mar-24
1002202LDH18-Mar-24
1002202ALT18-Mar-24
1002202ALT18-Mar-24
1002202ALT25-Jun-24

 

 

  

Below Value need to add above dataset where same siteid and test and the date value between stdt and endt.

if endt is missing then use only date and stdt 

 

for example subject 1001 and siteid = 201 ,test = LDH , date = 31Sep24 , value need be assign 27 and rest of two records will get value 24 .

 

siteidteststdtendtvalue
201LDH10-Aug-2415-Aug-2424
201LDH30-Sep-24 27
201ALT10-Aug-2415-Aug-241002
202LDH15-Mar-2428-Mar-2435
202ALT15-Mar-2428-Mar-241021
202ALT20-Jun-24 1031
1 REPLY 1
himself
Pyrite | Level 9
/* Kindly Note that September has dates upto 30th*/
data dataset1;
    input subject Siteid Test $ date :date9.;
    format date date9.;
    datalines;
1001 201 LDH 12-Aug-24
1001 201 LDH 22-Aug-24
1001 201 LDH 31-Sep-24
1001 201 ALT 12-Aug-24
1001 201 ALT 12-Aug-24
1002 202 LDH 18-Mar-24
1002 202 LDH 18-Mar-24
1002 202 ALT 18-Mar-24
1002 202 ALT 18-Mar-24
1002 202 ALT 25-Jun-24
;
run;

data dataset2;
    input Siteid Test $ stdt :date9. endt :date9. value;
    format stdt endt date9.;
    datalines;
201 LDH 10-Aug-24 15-Aug-24 24
201 LDH 30-Sep-24 . 27
201 ALT 10-Aug-24 15-Aug-24 1002
202 LDH 15-Mar-24 28-Mar-24 35
202 ALT 15-Mar-24 28-Mar-24 1021
202 ALT 20-Jun-24 . 1031
;
run;

proc sql;
    create table merged as
    select a.*, b.value
    from dataset1 as a
    left join dataset2 as b
    on a.Siteid = b.Siteid and a.Test = b.Test
    and (a.date between b.stdt and coalesce(b.endt, a.date));
quit;

proc print data=merged;
run;

The date “31-Sep-24” is missing because September only has 30 days. Therefore, “31-Sep-24” is not a valid date.

To correct this, you should use a valid date within September, such as “30-Sep-24”. Here’s the updated dataset with the corrected date:

data dataset1;
    input subject Siteid Test $ date :date9.;
    format date date9.;
    datalines;
1001 201 LDH 12-Aug-24
1001 201 LDH 22-Aug-24
1001 201 LDH 30-Sep-24
1001 201 ALT 12-Aug-24
1001 201 ALT 12-Aug-24
1002 202 LDH 18-Mar-24
1002 202 LDH 18-Mar-24
1002 202 ALT 18-Mar-24
1002 202 ALT 18-Mar-24
1002 202 ALT 25-Jun-24
;
run;

data dataset2;
    input Siteid Test $ stdt :date9. endt :date9. value;
    format stdt endt date9.;
    datalines;
201 LDH 10-Aug-24 15-Aug-24 24
201 LDH 30-Sep-24 . 27
201 ALT 10-Aug-24 15-Aug-24 1002
202 LDH 15-Mar-24 28-Mar-24 35
202 ALT 15-Mar-24 28-Mar-24 1021
202 ALT 20-Jun-24 . 1031
;
run;

proc sql;
    create table merged as
    select a.*, b.value
    from dataset1 as a
    left join dataset2 as b
    on a.Siteid = b.Siteid and a.Test = b.Test
    and (a.date between b.stdt and coalesce(b.endt, a.date));
quit;

proc print data=merged;
run;

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 1 reply
  • 581 views
  • 0 likes
  • 2 in conversation