<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Merging data sets with more than one condition using proc sql in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merging-data-sets-with-more-than-one-condition-using-proc-sql/m-p/355818#M83352</link>
    <description>&lt;P&gt;Please try the sql approach&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ae1;
set ae;
if length(aedate) lt 10 then do;
aedate=strip(aedate)||'-'||'01';
adtf='D';
end;
aedt=input(aedate,yymmdd10.);
format aedt date9.;
row=_n_;
run;

proc sql;
create table want as select a.*,b.dose,b.exdt from ae1 as a left join ex as b on a.id=b.id and b.exdt&amp;lt;=a.aedt order by a.id,a.term,a.aedt,b.exdt;
create table want_ as select a.id,a.term,a.aedt,b.dose as dose2,b.exdt as exdt2 from ae1(where=(adtf='D')) as a left join ex as b on a.id=b.id order by a.id,a.term,a.aedt,b.exdt;
quit;

data want_;
set want_;
by id term aedt;
if first.aedt;
run;

data want2;
set want;
by id term aedt;
if last.aedt;
run;

data want3;
merge want2 want_;
by id term aedt;
if dose eq . then dose=dose2;
keep id term aedate dose;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 04 May 2017 01:04:18 GMT</pubDate>
    <dc:creator>Jagadishkatam</dc:creator>
    <dc:date>2017-05-04T01:04:18Z</dc:date>
    <item>
      <title>Merging data sets with more than one condition using proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-data-sets-with-more-than-one-condition-using-proc-sql/m-p/355445#M83243</link>
      <description>&lt;P&gt;Dear,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am merging ae and ex data sets &amp;nbsp;to get last dose on or before AE event. I need help in my SQL code to get the output. Please help . Thank you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data ae;
input id term$ aedate $10.;
datalines;
1 ache 2015-10-05
1 ache 2015-10-08
2 pain 2015-11-10
2 ache 2015-11-07
3 ache 2015-10
4 ache 2015-11
5 pain 2015-12-07
;
data ex;
input id dose exdate $10.;
datalines;
1 10 2015-10-04
1 20 2015-10-06
1 30 2015-10-08
2 15 2015-11-05
2 30 2015-11-05
2 25 2015-11-09
3 40 2015-10-05
3 50 2015-10-09
3 60 2015-10-15
4 22 2015-10-29
4 26 2015-11-09
4 29 2015-11-19
5 50 2015-12-09
5 50 2015-12-15
5 50 2015-12-20
;&lt;BR /&gt;
data ae1;
set ae;
if length(aedate) lt 10 then do;
aedate=strip(aedate)||'-'||'01';
adtf='D';
end;
run;&lt;BR /&gt;
proc sql;
create table one as
select a.*,b.dose as DOAEON 
 from ae1 as a left join ex as b 
  on a.id=b.id and a.Aedate-b.exdate &amp;gt;=0
    group by a.id,a.aedate 
     having a.Aedate-b.exdate=min(a.Aedate-b.exdate);
quit;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;output needed;&lt;/P&gt;
&lt;P&gt;id &amp;nbsp; term &amp;nbsp; &amp;nbsp; &amp;nbsp;aedate &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;dose&lt;BR /&gt;1 &amp;nbsp; ache &amp;nbsp; &amp;nbsp;2015-10-05 &amp;nbsp; &amp;nbsp; &amp;nbsp;10 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;/* Dose taken on or before AE event.*/&lt;/SPAN&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;BR /&gt;1 &amp;nbsp; ache &amp;nbsp; &amp;nbsp;2015-10-08 &amp;nbsp; &amp;nbsp; &amp;nbsp;30 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;/*Dose taken on or before AE event.*/&lt;/SPAN&gt;&lt;BR /&gt;2 &amp;nbsp; &amp;nbsp;pain &amp;nbsp; &amp;nbsp; 2015-11-10 &amp;nbsp; &amp;nbsp; 25 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;/*Dose taken on or before AE event.*/&lt;/SPAN&gt;&lt;BR /&gt;2 &amp;nbsp; &amp;nbsp;ache &amp;nbsp; &amp;nbsp; 2015-11-07 &amp;nbsp; &amp;nbsp;15 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;SPAN&gt;/*if subject has taken two doses on same day, consider small dose*/&lt;/SPAN&gt;&lt;BR /&gt;3 &amp;nbsp; &amp;nbsp;ache &amp;nbsp; &amp;nbsp; 2015-10-01 &amp;nbsp; &amp;nbsp;40 &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;SPAN&gt;/ partial date(adtf='D') and no dose value present prior to aedate then first exdose consider*/&lt;/SPAN&gt;&lt;BR /&gt;4 &amp;nbsp; &amp;nbsp;ache &amp;nbsp; &amp;nbsp; &amp;nbsp;2015-11-01 &amp;nbsp; &amp;nbsp;22 &amp;nbsp;&amp;nbsp;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;/ partial date(adtf='D') and dose value present prior to aedate then on or before dose consider*/&lt;/SPAN&gt;&lt;BR /&gt;5 &amp;nbsp; &amp;nbsp; pain &amp;nbsp; &amp;nbsp; &amp;nbsp;2015-12-07 &amp;nbsp; &amp;nbsp; . &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;SPAN&gt;/*complete date with no prior dose taken before aedate, then no dose populated*/&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 May 2017 00:03:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-data-sets-with-more-than-one-condition-using-proc-sql/m-p/355445#M83243</guid>
      <dc:creator>knveraraju91</dc:creator>
      <dc:date>2017-05-03T00:03:05Z</dc:date>
    </item>
    <item>
      <title>Re: Merging data sets with more than one condition using proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-data-sets-with-more-than-one-condition-using-proc-sql/m-p/355598#M83281</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ae;
input id term$ aedate $10.;
datalines;
1 ache 2015-10-05
1 ache 2015-10-08
2 pain 2015-11-10
2 ache 2015-11-07
3 ache 2015-10
4 ache 2015-11
5 pain 2015-12-07
;
data ex;
input id dose exdate : yymmdd10.;
format exdate yymmdd10.;
datalines;
1 10 2015-10-04
1 20 2015-10-06
1 30 2015-10-08
2 15 2015-11-05
2 30 2015-11-05
2 25 2015-11-09
3 40 2015-10-05
3 50 2015-10-09
3 60 2015-10-15
4 22 2015-10-29
4 26 2015-11-09
4 29 2015-11-19
5 50 2015-12-09
5 50 2015-12-15
5 50 2015-12-20
;
proc sort data=ex;
 by id exdate dose;
run;
data new_ex;
 set ex;
 by id exdate ;
 if first.exdate;
 rename exdate=date;
run;

data ae;
 set ae;
 date=input(aedate,?? yymmdd10.);
 if missing(date) then do;date=input(cats(aedate,'-01'), yymmdd10.);flag=1;end;
 format date yymmdd10.;
run;
proc sort data=ae;
 by id date;
run;
data temp;
 set new_ex ae(in=inb);
 by id date;
 retain _dose;
 if first.id then call missing(_dose);
 if not missing(dose) then _dose=dose;
 want=inb;
 drop dose;
run;
proc sort data=temp;
 by id descending date;
run;
data want;
 set temp;
 by id;
 retain dose;
 if first.id then call missing(dose);
 if not missing(_dose) then dose=_dose;
 if flag and missing(_dose) then _dose=dose;
 if want;
 keep id date term _dose;
run;
proc sort data=want;
 by id date;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 May 2017 13:51:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-data-sets-with-more-than-one-condition-using-proc-sql/m-p/355598#M83281</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-05-03T13:51:05Z</dc:date>
    </item>
    <item>
      <title>Re: Merging data sets with more than one condition using proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-data-sets-with-more-than-one-condition-using-proc-sql/m-p/355818#M83352</link>
      <description>&lt;P&gt;Please try the sql approach&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ae1;
set ae;
if length(aedate) lt 10 then do;
aedate=strip(aedate)||'-'||'01';
adtf='D';
end;
aedt=input(aedate,yymmdd10.);
format aedt date9.;
row=_n_;
run;

proc sql;
create table want as select a.*,b.dose,b.exdt from ae1 as a left join ex as b on a.id=b.id and b.exdt&amp;lt;=a.aedt order by a.id,a.term,a.aedt,b.exdt;
create table want_ as select a.id,a.term,a.aedt,b.dose as dose2,b.exdt as exdt2 from ae1(where=(adtf='D')) as a left join ex as b on a.id=b.id order by a.id,a.term,a.aedt,b.exdt;
quit;

data want_;
set want_;
by id term aedt;
if first.aedt;
run;

data want2;
set want;
by id term aedt;
if last.aedt;
run;

data want3;
merge want2 want_;
by id term aedt;
if dose eq . then dose=dose2;
keep id term aedate dose;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 04 May 2017 01:04:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-data-sets-with-more-than-one-condition-using-proc-sql/m-p/355818#M83352</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2017-05-04T01:04:18Z</dc:date>
    </item>
  </channel>
</rss>

