DATA Step, Macro, Functions and more

Assigning values by comparing dates in two datasets

Reply
Super Contributor
Posts: 272

Assigning values by comparing dates in two datasets

Dear,

 

In my datasets the following values present. my code works for all obs except these obs. Please help thanks.

 

ex

 

id    exdate                      exdose

1      09MAY2016                50

1      09MAY2016                100

 

ae

id             aedate                 

1          09MAY2016

 

code;

proc sql;
create table dose as
select a.*,b.exdose
from ae as a left join EX as b
on a.id=b.id and a.Aedate-b.exdate >=0
group by a.id,a.aedate
having a.Aedate-b.exdate1=min(a.Aedate-b.exdate);
quit;

 

output getting ;

 

id             aedate                   exdose

1               09MAY2016              50

 

output need;

id             aedate                   exdose

1               09MAY2016              100

Super Contributor
Posts: 266

Re: Assigning values by comparing dates in two datasets

Posted in reply to knveraraju91

Can you provide one liner how the data to be picked for the output. Is it maximum of of exdose or minimum of exdose Or is there any other logic. what condition are applied on dates?

Super Contributor
Posts: 272

Re: Assigning values by comparing dates in two datasets

Thanks for help.

 

I need to get the last dose taken on or before AE start date. My code worked for all obs except this obs because there are two doses taken on the day AE started.  I need to get the maximum dose taken on that day. Thanks

Trusted Advisor
Posts: 1,019

Re: Assigning values by comparing dates in two datasets

Posted in reply to knveraraju91

You need a way to retain the maximum dose when there are multiple EX records on a date.  In addition you have to retain the most recent EXDOSE in cas you encounter a date with an AE record but no EX record:

 

data want (rename=(date=aedate) drop=prior_dose);
  merge ae (rename=(aedate=date)) 
        ex (in=inex keep=id exdate exdose rename=(exdate=date));
  by id date;

  retain prior_dose;

  if first.id then prior_dose=.;
  if inex then do;
    if first.date then prior_dose=exdose;
    else exdose=max(exdose,prior_dose);
  end;
  else exdose=prior_dose;
  if inae;
run;

 

 

Super User
Posts: 10,023

Re: Assigning values by comparing dates in two datasets

Posted in reply to knveraraju91
You have multiple obs for the same date and same id. Keep the last one you want.

proc sort data=ex;by id exdate;run;
data new_ex;
 set ex;
 by id exdate;
 if last.exdate;
run;

then PROC SQL.

Ask a Question
Discussion stats
  • 4 replies
  • 204 views
  • 2 likes
  • 4 in conversation