<?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 based on additional criteria in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/merging-based-on-additional-criteria/m-p/8757#M459</link>
    <description>If figured this one out yesterday (surprisingly) using this:&lt;BR /&gt;
&lt;BR /&gt;
PROC SQL;&lt;BR /&gt;
CREATE TABLE alldata2 AS&lt;BR /&gt;
SELECT a.*, b.*&lt;BR /&gt;
FROM medicare_proc_first a&lt;BR /&gt;
LEFT JOIN&lt;BR /&gt;
 medicare_final_1 b&lt;BR /&gt;
ON a.beneficiary_id=b.beneficiary_id_1&lt;BR /&gt;
where b.admission_date_1 BETWEEN a.admission_date and a.admission_date+365;&lt;BR /&gt;
QUIT;&lt;BR /&gt;
RUN; &lt;BR /&gt;
&lt;BR /&gt;
This worked perfectly for me once I made sure the 2 data sets had different variable names. However, today I am trying to do a left join with medicare_proc_first with a new modified medicare_final_1. There are 13098 unique beneficiary_ids in medicare_proc and from what I understand, with a left join I should see 13098 unique beneficiary ids in my new data set alldata2. However, after creating the new table I only see 12748 unique beneficiary ids. Any idea why this would be happening?</description>
    <pubDate>Wed, 08 Jun 2011 20:36:18 GMT</pubDate>
    <dc:creator>sweetpeaindeed</dc:creator>
    <dc:date>2011-06-08T20:36:18Z</dc:date>
    <item>
      <title>merging based on additional criteria</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/merging-based-on-additional-criteria/m-p/8750#M452</link>
      <description>I'm trying to merge two data sets based on id and admission date. If you see below,  I merged medicare_final  with medicare_proc_first for beneficiary ids that are in both data sets. However, I also only want the observations from medicare_final to merge with medicare_proc_first where admission_date in medicare_final is greater than the admission date in medicare_proc_first and only within 365 days  after the admission_date in medicare_proc_first. Can I do this??&lt;BR /&gt;
Any help is greatly appreciated; I have no idea how to accomplish this.&lt;BR /&gt;
&lt;BR /&gt;
data medicare_proc_all;&lt;BR /&gt;
	merge medicare_proc_first (IN=M1) medicare_final (IN = M2);&lt;BR /&gt;
	by beneficiary_id;&lt;BR /&gt;
	if m1 and m2;&lt;BR /&gt;
run;</description>
      <pubDate>Tue, 07 Jun 2011 17:21:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/merging-based-on-additional-criteria/m-p/8750#M452</guid>
      <dc:creator>sweetpeaindeed</dc:creator>
      <dc:date>2011-06-07T17:21:31Z</dc:date>
    </item>
    <item>
      <title>Re: merging based on additional criteria</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/merging-based-on-additional-criteria/m-p/8751#M453</link>
      <description>It is straightforward using SQL.&lt;BR /&gt;
&lt;BR /&gt;
PROC SQL;&lt;BR /&gt;
CREATE TABLE medicare_proc_all AS&lt;BR /&gt;
SELECT *&lt;BR /&gt;
FROM medicare_proc_first AS p,&lt;BR /&gt;
           medicare_final AS f&lt;BR /&gt;
WHERE&lt;BR /&gt;
  p.beneficiary_id=f.beneficiary_id AND&lt;BR /&gt;
  f.admission_date BETWEEN p.admission_date+1 and P.admission_date+365&lt;BR /&gt;
;&lt;BR /&gt;
&lt;BR /&gt;
You may want to rename one of the admission dates so you keep both in the final dataset.</description>
      <pubDate>Tue, 07 Jun 2011 17:35:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/merging-based-on-additional-criteria/m-p/8751#M453</guid>
      <dc:creator>Doc_Duke</dc:creator>
      <dc:date>2011-06-07T17:35:47Z</dc:date>
    </item>
    <item>
      <title>Re: merging based on additional criteria</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/merging-based-on-additional-criteria/m-p/8752#M454</link>
      <description>Hello SweetPeaIndeed,&lt;BR /&gt;
&lt;BR /&gt;
It is better to use proc SQL in this case like this:&lt;BR /&gt;
[pre]&lt;BR /&gt;
proc SQL;&lt;BR /&gt;
  create table medicare_proc_all as &lt;BR /&gt;
  select a.*, b.* /* list fields you need from both tables */&lt;BR /&gt;
  from medicare_proc_first as a, medicare_final as b&lt;BR /&gt;
  where a.beneficiary_id=b.beneficiary_id and &lt;BR /&gt;
  b.admission_date-365 LE a.admission_date LT b.admission_date&lt;BR /&gt;
;quit;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
Since both datasets contain the same variables like beneficiary_id, admission_date and may be some others it is necessary to list all necessary variables in the select clause.&lt;BR /&gt;
&lt;BR /&gt;
Sincerely,&lt;BR /&gt;
SPR</description>
      <pubDate>Tue, 07 Jun 2011 17:38:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/merging-based-on-additional-criteria/m-p/8752#M454</guid>
      <dc:creator>SPR</dc:creator>
      <dc:date>2011-06-07T17:38:47Z</dc:date>
    </item>
    <item>
      <title>Re: merging based on additional criteria</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/merging-based-on-additional-criteria/m-p/8753#M455</link>
      <description>Thank you both! However, it doesn't seem to be giving me what I am looking for. When I use either of the sql statements it only gives me the first observation for each beneficiary_id, which is not what I am trying to get. This makes me think I am explaining myself poorly. The merge in a one-to-many where beneficiary_ids match. So, in medicare_proc, I have one observation for a beneficiary_id but that beneficiary_id has many matches in medicare_final. I want those additional observations in medicare_final but ONLY if the beneficiary_ids match and the admission_date in medicare_final is greater than within 365 days of the admission_date in medicare_proc.&lt;BR /&gt;
&lt;BR /&gt;
For example:&lt;BR /&gt;
&lt;BR /&gt;
medicare_proc               medicare_final                    medicare_all&lt;BR /&gt;
&lt;BR /&gt;
id   admission_date        id     admission_date           id      admission_date&lt;BR /&gt;
1     03MAR2003            1     03FEB2003                  1       03MAR2003&lt;BR /&gt;
                                     1    03MAR2003                  1       03MAY2003&lt;BR /&gt;
                                     1    03MAY2003&lt;BR /&gt;
&lt;BR /&gt;
Merging medicare_proc and medicare_final would give me medicare_all</description>
      <pubDate>Tue, 07 Jun 2011 18:11:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/merging-based-on-additional-criteria/m-p/8753#M455</guid>
      <dc:creator>sweetpeaindeed</dc:creator>
      <dc:date>2011-06-07T18:11:05Z</dc:date>
    </item>
    <item>
      <title>Re: merging based on additional criteria</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/merging-based-on-additional-criteria/m-p/8754#M456</link>
      <description>Thank you! However, it doesn't seem to be giving me what I am looking for. When I use either of the sql statements it only gives me the first observation for each beneficiary_id, which is not what I am trying to get. This makes me think I am explaining myself poorly. The merge in a one-to-many where beneficiary_ids match. So, in medicare_proc, I have one observation for a beneficiary_id but that beneficiary_id has many matches in medicare_final. I want those additional observations in medicare_final but ONLY if the beneficiary_ids match and the admission_date in medicare_final is greater than within 365 days of the admission_date in medicare_proc.&lt;BR /&gt;
&lt;BR /&gt;
For example:&lt;BR /&gt;
&lt;BR /&gt;
medicare_proc               medicare_final                    medicare_all&lt;BR /&gt;
&lt;BR /&gt;
id   admission_date        id     admission_date           id      admission_date&lt;BR /&gt;
1     03MAR2003            1     03FEB2003                  1       03MAR2003&lt;BR /&gt;
                                     1    03MAR2003                  1       03MAY2003&lt;BR /&gt;
                                     1    03MAY2003&lt;BR /&gt;
&lt;BR /&gt;
Merging medicare_proc and medicare_final would give me medicare_all.&lt;BR /&gt;
Hopefully that makes more sense. Clearly neither of my sas or sql skills are great so I appreciate the help!</description>
      <pubDate>Tue, 07 Jun 2011 18:13:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/merging-based-on-additional-criteria/m-p/8754#M456</guid>
      <dc:creator>sweetpeaindeed</dc:creator>
      <dc:date>2011-06-07T18:13:14Z</dc:date>
    </item>
    <item>
      <title>Re: merging based on additional criteria</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/merging-based-on-additional-criteria/m-p/8755#M457</link>
      <description>I am completely confused with the information you submit. In your last post you say:&lt;BR /&gt;
"I want those additional observations in medicare_final but ONLY if the beneficiary_ids match and the admission_date in medicare_final is greater than within 365 days of the admission_date in medicare_proc" &lt;BR /&gt;
&lt;BR /&gt;
1) It means that admission_date(final) &amp;gt; admission_date(proc) + 365.&lt;BR /&gt;
&lt;BR /&gt;
In you example you wrote:&lt;BR /&gt;
[pre]&lt;BR /&gt;
medicare_proc     medicare_final    medicare_all&lt;BR /&gt;
id admission_date id admission_date id admission_date&lt;BR /&gt;
1  03MAR2003      1  03FEB2003      1  03MAR2003&lt;BR /&gt;
[/pre]&lt;BR /&gt;
According to 1) it should be &lt;BR /&gt;
admission date(final)=03Feb2003 &amp;gt; admission_date(proc)=03Mar2003 + 365=03Mar2003&lt;BR /&gt;
&lt;BR /&gt;
which is an obvious error. Please, explain.&lt;BR /&gt;
SPR</description>
      <pubDate>Tue, 07 Jun 2011 21:14:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/merging-based-on-additional-criteria/m-p/8755#M457</guid>
      <dc:creator>SPR</dc:creator>
      <dc:date>2011-06-07T21:14:15Z</dc:date>
    </item>
    <item>
      <title>Re: merging based on additional criteria</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/merging-based-on-additional-criteria/m-p/8756#M458</link>
      <description>Or Maybe you can add another condition to filter some observations.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
&lt;BR /&gt;
data medicare_proc_all;&lt;BR /&gt;
merge medicare_proc_first (IN=M1) medicare_final (IN = M2 rename=(admission_date=_admission_date));&lt;BR /&gt;
by beneficiary_id;&lt;BR /&gt;
if m1 and m2 and _admission_date le admission_date+365;&lt;BR /&gt;
run; &lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Wed, 08 Jun 2011 03:31:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/merging-based-on-additional-criteria/m-p/8756#M458</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-06-08T03:31:51Z</dc:date>
    </item>
    <item>
      <title>Re: merging based on additional criteria</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/merging-based-on-additional-criteria/m-p/8757#M459</link>
      <description>If figured this one out yesterday (surprisingly) using this:&lt;BR /&gt;
&lt;BR /&gt;
PROC SQL;&lt;BR /&gt;
CREATE TABLE alldata2 AS&lt;BR /&gt;
SELECT a.*, b.*&lt;BR /&gt;
FROM medicare_proc_first a&lt;BR /&gt;
LEFT JOIN&lt;BR /&gt;
 medicare_final_1 b&lt;BR /&gt;
ON a.beneficiary_id=b.beneficiary_id_1&lt;BR /&gt;
where b.admission_date_1 BETWEEN a.admission_date and a.admission_date+365;&lt;BR /&gt;
QUIT;&lt;BR /&gt;
RUN; &lt;BR /&gt;
&lt;BR /&gt;
This worked perfectly for me once I made sure the 2 data sets had different variable names. However, today I am trying to do a left join with medicare_proc_first with a new modified medicare_final_1. There are 13098 unique beneficiary_ids in medicare_proc and from what I understand, with a left join I should see 13098 unique beneficiary ids in my new data set alldata2. However, after creating the new table I only see 12748 unique beneficiary ids. Any idea why this would be happening?</description>
      <pubDate>Wed, 08 Jun 2011 20:36:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/merging-based-on-additional-criteria/m-p/8757#M459</guid>
      <dc:creator>sweetpeaindeed</dc:creator>
      <dc:date>2011-06-08T20:36:18Z</dc:date>
    </item>
    <item>
      <title>Re: merging based on additional criteria</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/merging-based-on-additional-criteria/m-p/8758#M460</link>
      <description>Yes.&lt;BR /&gt;
I think that is because you add a "where" clause.&lt;BR /&gt;
So it will filter some obs.&lt;BR /&gt;
You can test it without this "where" clause, to see whether has 13098 unique .&lt;BR /&gt;
&lt;BR /&gt;
But the best way to find answer is to post some dummy data and code you used,then can check it further.&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Thu, 09 Jun 2011 01:32:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/merging-based-on-additional-criteria/m-p/8758#M460</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-06-09T01:32:43Z</dc:date>
    </item>
    <item>
      <title>Re: merging based on additional criteria</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/merging-based-on-additional-criteria/m-p/8759#M461</link>
      <description>Eliminate the WHERE clause, but move the condition into the ON clause (connected with AND). That's probably what's desired.&lt;BR /&gt;
&lt;BR /&gt;
Outer unions are guided by their ON clauses. Any accompanying WHERE clause operates as a simple filter and does not generate any "outer" rows.&lt;BR /&gt;
&lt;BR /&gt;
With inner unions, it doesn't matter if some of the conditions are coded in a WHERE clause. With outer unions it does.&lt;BR /&gt;
&lt;BR /&gt;
Here's an example.&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data table_L ;&lt;BR /&gt;
input id_L $ date_L ;&lt;BR /&gt;
cards ;&lt;BR /&gt;
x 1&lt;BR /&gt;
y 2&lt;BR /&gt;
z 3&lt;BR /&gt;
;&lt;BR /&gt;
&lt;BR /&gt;
data table_R ;&lt;BR /&gt;
input id_R $ date_R amount ;&lt;BR /&gt;
cards ;&lt;BR /&gt;
x  1 1.1&lt;BR /&gt;
y  2 2.2&lt;BR /&gt;
;&lt;BR /&gt;
&lt;BR /&gt;
PROC SQL ;&lt;BR /&gt;
TITLE 'All Conditions in ON Clause' ;&lt;BR /&gt;
SELECT *&lt;BR /&gt;
 FROM table_L LEFT JOIN table_R&lt;BR /&gt;
  ON     id_L EQ   id_R&lt;BR /&gt;
   AND date_L EQ date_R&lt;BR /&gt;
;&lt;BR /&gt;
TITLE 'Some Conditions in WHERE Clause' ;&lt;BR /&gt;
SELECT *&lt;BR /&gt;
 FROM table_L LEFT JOIN table_R&lt;BR /&gt;
  ON     id_L EQ   id_R&lt;BR /&gt;
 WHERE date_L EQ date_R&lt;BR /&gt;
;&lt;BR /&gt;
TITLE ;&lt;BR /&gt;
QUIT ;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
The first query returns&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
id_L        date_L  id_R        date_R    amount&lt;BR /&gt;
------------------------------------------------&lt;BR /&gt;
x                1  x                1       1.1&lt;BR /&gt;
y                2  y                2       2.2&lt;BR /&gt;
z                3                   .         .&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
but the second returns&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
id_L        date_L  id_R        date_R    amount&lt;BR /&gt;
------------------------------------------------&lt;BR /&gt;
x                1  x                1       1.1&lt;BR /&gt;
y                2  y                2       2.2&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
&amp;gt; Yes.&lt;BR /&gt;
&amp;gt; I think that is because you add a "where" clause.&lt;BR /&gt;
&amp;gt; So it will filter some obs.&lt;BR /&gt;
&amp;gt; You can test it without this "where" clause, to see&lt;BR /&gt;
&amp;gt; whether has 13098 unique .&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; But the best way to find answer is to post some dummy&lt;BR /&gt;
&amp;gt; data and code you used,then can check it further.&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; Ksharp</description>
      <pubDate>Fri, 10 Jun 2011 15:51:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/merging-based-on-additional-criteria/m-p/8759#M461</guid>
      <dc:creator>Howles</dc:creator>
      <dc:date>2011-06-10T15:51:06Z</dc:date>
    </item>
    <item>
      <title>Re: merging based on additional criteria</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/merging-based-on-additional-criteria/m-p/8760#M462</link>
      <description>Hi.Howles &lt;BR /&gt;
Thanks. let me know there is different between &lt;BR /&gt;
&lt;BR /&gt;
ON     id_L EQ   id_R&lt;BR /&gt;
   AND date_L EQ date_R&lt;BR /&gt;
&lt;BR /&gt;
and&lt;BR /&gt;
&lt;BR /&gt;
 ON     id_L EQ   id_R&lt;BR /&gt;
 WHERE date_L EQ date_R&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
I think they are the same before.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Mon, 13 Jun 2011 05:47:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/merging-based-on-additional-criteria/m-p/8760#M462</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-06-13T05:47:53Z</dc:date>
    </item>
    <item>
      <title>Re: merging based on additional criteria</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/merging-based-on-additional-criteria/m-p/8761#M463</link>
      <description>And "union" you mentioned would be join (I think), union in sql is an other different operator from join .&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Mon, 13 Jun 2011 06:30:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/merging-based-on-additional-criteria/m-p/8761#M463</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-06-13T06:30:09Z</dc:date>
    </item>
  </channel>
</rss>

