<?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: Merge/left join onto certain rows in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merge-left-join-onto-certain-rows/m-p/377549#M90676</link>
    <description>&lt;P&gt;something like this hsould work for your scenario&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;&lt;BR /&gt;create table new as 
select a.hospid, a.year, case when a.hospid=b.hospid and a.ratio is missing 
                               then b.ratio *1.04 
                               else a.ratio
                               end as ratio 
from 
(select hospid, year, ratio
from old)a
left join
(select distinct hospid, ratio from old
where ratio is not missing)b
on a.hospid =b.hospid;
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 19 Jul 2017 19:42:32 GMT</pubDate>
    <dc:creator>kiranv_</dc:creator>
    <dc:date>2017-07-19T19:42:32Z</dc:date>
    <item>
      <title>Merge/left join onto certain rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-left-join-onto-certain-rows/m-p/377519#M90657</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the scenario: I have a dataset with hospital identifiers, years, and a cost to charge ratio. For the year 2003, no cost to charge ratio is available. I need to take the ratio from 2004, multipy it by 1.04, and then merge by hospital ID to get cost to charge ratios for 2003. It might make more sense if you look at the code--I want to go from old to new.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data old;&lt;/P&gt;&lt;P&gt;infile datalines;&lt;/P&gt;&lt;P&gt;input Hospid $ Year Ratio;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;A 2003 .&lt;/P&gt;&lt;P&gt;B 2003 .&lt;/P&gt;&lt;P&gt;C 2003 .&lt;/P&gt;&lt;P&gt;C 2003 .&lt;/P&gt;&lt;P&gt;A 2004 .2&lt;/P&gt;&lt;P&gt;B 2004 .24&lt;/P&gt;&lt;P&gt;C 2004 .3&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and I want to get new by taking the ratio from 2004, multiplying it by 1.04 and then matching it by hospid:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data new;&lt;/P&gt;&lt;P&gt;infile datalines;&lt;/P&gt;&lt;P&gt;input hospid $ year ratio;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;A 2003 .208&lt;/P&gt;&lt;P&gt;B 2003 .2496&lt;/P&gt;&lt;P&gt;C 2003 .312&lt;/P&gt;&lt;P&gt;C 2003 .312&lt;/P&gt;&lt;P&gt;A 2004 .2&lt;/P&gt;&lt;P&gt;B 2004 .24&lt;/P&gt;&lt;P&gt;C 2004 .3&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried two methods:&lt;/P&gt;&lt;P&gt;1. a proc sql left join, in which I made a separate dataset of 2004 observations and then tried to join by hospid. that still gave me missing values&lt;/P&gt;&lt;P&gt;2. also making a separate dataset of 2004 observations, sorting that and the original dataset, then merging by hospid. that mostly worked, except when hospital id appeared more than once in which case i only got a ratio for one of the occurances.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks for any advice!&amp;nbsp;&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;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jul 2017 18:57:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-left-join-onto-certain-rows/m-p/377519#M90657</guid>
      <dc:creator>lnicholl9</dc:creator>
      <dc:date>2017-07-19T18:57:57Z</dc:date>
    </item>
    <item>
      <title>Re: Merge/left join onto certain rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-left-join-onto-certain-rows/m-p/377549#M90676</link>
      <description>&lt;P&gt;something like this hsould work for your scenario&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;&lt;BR /&gt;create table new as 
select a.hospid, a.year, case when a.hospid=b.hospid and a.ratio is missing 
                               then b.ratio *1.04 
                               else a.ratio
                               end as ratio 
from 
(select hospid, year, ratio
from old)a
left join
(select distinct hospid, ratio from old
where ratio is not missing)b
on a.hospid =b.hospid;
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jul 2017 19:42:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-left-join-onto-certain-rows/m-p/377549#M90676</guid>
      <dc:creator>kiranv_</dc:creator>
      <dc:date>2017-07-19T19:42:32Z</dc:date>
    </item>
    <item>
      <title>Re: Merge/left join onto certain rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-left-join-onto-certain-rows/m-p/377558#M90679</link>
      <description>&lt;P&gt;Your only matching criteria is ID but ID is not unique to each observation. Am I reading your data set correctly?&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jul 2017 19:54:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-left-join-onto-certain-rows/m-p/377558#M90679</guid>
      <dc:creator>GreggB</dc:creator>
      <dc:date>2017-07-19T19:54:59Z</dc:date>
    </item>
    <item>
      <title>Re: Merge/left join onto certain rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-left-join-onto-certain-rows/m-p/377583#M90683</link>
      <description>&lt;P&gt;There are too many cases that you haven't covered here.&amp;nbsp; What should the results be for these combinations?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data old;&lt;/P&gt;
&lt;P&gt;infile datalines;&lt;/P&gt;
&lt;P&gt;input Hospid $ Year Ratio;&lt;/P&gt;
&lt;P&gt;datalines;&lt;/P&gt;
&lt;P&gt;A 2003 .&lt;/P&gt;
&lt;P&gt;A 2003 .&lt;/P&gt;
&lt;P&gt;B 2003 .&lt;/P&gt;
&lt;P&gt;C 2003 .&lt;/P&gt;
&lt;P&gt;C 2003 .&lt;/P&gt;
&lt;P&gt;A 2004 .2&lt;/P&gt;
&lt;P&gt;A 2004 .3&lt;/P&gt;
&lt;P&gt;B 2004 .24&lt;/P&gt;
&lt;P&gt;B 2004 .26&lt;/P&gt;
&lt;P&gt;C 2004 .3&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anything is possible.&amp;nbsp; But the rules are incomplete at this point.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jul 2017 20:37:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-left-join-onto-certain-rows/m-p/377583#M90683</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-07-19T20:37:17Z</dc:date>
    </item>
    <item>
      <title>Re: Merge/left join onto certain rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-left-join-onto-certain-rows/m-p/377594#M90686</link>
      <description>Sorry, should have been more clear. There would not be a case in which the ratio would be different for two instances of the same hospital.&lt;BR /&gt;&lt;BR /&gt;So this:&lt;BR /&gt;A 2004 .3&lt;BR /&gt;A 2004 .2&lt;BR /&gt;&lt;BR /&gt;Would never happen. However this could happen:&lt;BR /&gt;A 2004 .3&lt;BR /&gt;A 2005 .2&lt;BR /&gt;&lt;BR /&gt;But I don't care about years besides 2003 and 2004, I want those years to be unaffected&lt;BR /&gt;</description>
      <pubDate>Wed, 19 Jul 2017 21:04:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-left-join-onto-certain-rows/m-p/377594#M90686</guid>
      <dc:creator>lnicholl9</dc:creator>
      <dc:date>2017-07-19T21:04:49Z</dc:date>
    </item>
    <item>
      <title>Re: Merge/left join onto certain rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-left-join-onto-certain-rows/m-p/377595#M90687</link>
      <description>I think so, there can be more than one of the same hospital ID within a given year.&lt;BR /&gt;</description>
      <pubDate>Wed, 19 Jul 2017 21:06:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-left-join-onto-certain-rows/m-p/377595#M90687</guid>
      <dc:creator>lnicholl9</dc:creator>
      <dc:date>2017-07-19T21:06:39Z</dc:date>
    </item>
    <item>
      <title>Re: Merge/left join onto certain rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-left-join-onto-certain-rows/m-p/377606#M90691</link>
      <description>&lt;P&gt;I'm not sure if this would be the fastest way, but it's probably the clearest way.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data just2003 just2004 other_years;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; set old;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if year=2003 then output just2003;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; else if year = 2004 then output just2004;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; else output other_years;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=just2003;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; by HospID;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=just2004 out=ratio2004 nodupkey;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; by HospID;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data replace2003;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; set ratio2004 (in=in1)&amp;nbsp;&amp;nbsp; just2003 (in=in2);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; by HospID;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if first.HospID then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if in1 then replacement_ratio = ratio * 1.04;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else replacement_ratio = .;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if in2;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if ratio = . then ratio = replacement_ratio;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; drop replacement_ratio;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; retain replacement_ratio;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; set replace2003 just2004 other_years;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jul 2017 21:33:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-left-join-onto-certain-rows/m-p/377606#M90691</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-07-19T21:33:55Z</dc:date>
    </item>
    <item>
      <title>Re: Merge/left join onto certain rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-left-join-onto-certain-rows/m-p/377993#M90803</link>
      <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql;
   create table WANT as  
     select HOSPID, 2003 as YEAR, RATIO*1.04 as RATIO
     from HAVE
     where YEAR eq 2004
   union all
     select * 
     from HAVE
     where YEAR ne 2003;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure SQL: Query Results" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="l b header" scope="col"&gt;HOSPID&lt;/TH&gt;
&lt;TH class="r b header" scope="col"&gt;YEAR&lt;/TH&gt;
&lt;TH class="r b header" scope="col"&gt;RATIO&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;A&lt;/TD&gt;
&lt;TD class="r data"&gt;2003&lt;/TD&gt;
&lt;TD class="r data"&gt;0.208&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;B&lt;/TD&gt;
&lt;TD class="r data"&gt;2003&lt;/TD&gt;
&lt;TD class="r data"&gt;0.2496&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;C&lt;/TD&gt;
&lt;TD class="r data"&gt;2003&lt;/TD&gt;
&lt;TD class="r data"&gt;0.312&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;A&lt;/TD&gt;
&lt;TD class="r data"&gt;2004&lt;/TD&gt;
&lt;TD class="r data"&gt;0.2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;B&lt;/TD&gt;
&lt;TD class="r data"&gt;2004&lt;/TD&gt;
&lt;TD class="r data"&gt;0.24&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;C&lt;/TD&gt;
&lt;TD class="r data"&gt;2004&lt;/TD&gt;
&lt;TD class="r data"&gt;0.3&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
      <pubDate>Fri, 21 Jul 2017 04:10:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-left-join-onto-certain-rows/m-p/377993#M90803</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-07-21T04:10:06Z</dc:date>
    </item>
    <item>
      <title>Re: Merge/left join onto certain rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-left-join-onto-certain-rows/m-p/378314#M90875</link>
      <description>&lt;P&gt;When I tried this I ended up with more observations than in the original dataset&lt;/P&gt;</description>
      <pubDate>Fri, 21 Jul 2017 20:40:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-left-join-onto-certain-rows/m-p/378314#M90875</guid>
      <dc:creator>lnicholl9</dc:creator>
      <dc:date>2017-07-21T20:40:41Z</dc:date>
    </item>
    <item>
      <title>Re: Merge/left join onto certain rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-left-join-onto-certain-rows/m-p/378319#M90877</link>
      <description>&lt;P&gt;I tried this code but if there is a hospital in 2003 which is not in the other years, it gets overwritten. see this dataset;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data old;&lt;/P&gt;&lt;P&gt;infile datalines;&lt;/P&gt;&lt;P&gt;input hospid $ year ratio;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;A 2003 .&lt;/P&gt;&lt;P&gt;A 2003 .&lt;/P&gt;&lt;P&gt;B 2003 .&lt;/P&gt;&lt;P&gt;C 2003 .&lt;/P&gt;&lt;P&gt;A 2004 .2&lt;/P&gt;&lt;P&gt;B 2004 .3&lt;/P&gt;&lt;P&gt;B 2004 .3&lt;/P&gt;&lt;P&gt;D 2004 .4&lt;/P&gt;&lt;P&gt;A 2005 .3&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;output should be this but instead C gets overwriten:&lt;/P&gt;&lt;P&gt;A 2003 .208&lt;/P&gt;&lt;P&gt;A 2003 .208&lt;/P&gt;&lt;P&gt;B 2003 .312&lt;/P&gt;&lt;P&gt;C 2003 .&lt;/P&gt;&lt;P&gt;A 2004 .2&lt;/P&gt;&lt;P&gt;B 2004 .3&lt;/P&gt;&lt;P&gt;B 2004 .3&lt;/P&gt;&lt;P&gt;D 2004 .4&lt;/P&gt;&lt;P&gt;A 2005 .3&lt;/P&gt;</description>
      <pubDate>Fri, 21 Jul 2017 20:49:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-left-join-onto-certain-rows/m-p/378319#M90877</guid>
      <dc:creator>lnicholl9</dc:creator>
      <dc:date>2017-07-21T20:49:12Z</dc:date>
    </item>
  </channel>
</rss>

