<?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: Problems merging with if statement... in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Problems-merging-with-if-statement/m-p/360106#M64408</link>
    <description>&lt;P&gt;You might show what you want for output.&lt;/P&gt;
&lt;P&gt;You are doing what is called a many-to-many merge: many municipalities (from the client data) to many hospitals. The data step merge is seldom the appropriate tool for that type of merge. The good news is Proc Sql does so very well:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql;
   create table want as
   select a.*, b.hospital_id 
   from clients as a left join hospitals as b
      on a.municipality=b.municipality
   where a.u between b.lower and b.upper
   ;
quit;&lt;/PRE&gt;
&lt;P&gt;Also sql does not require the explicit sort beforehand.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 19 May 2017 20:15:44 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2017-05-19T20:15:44Z</dc:date>
    <item>
      <title>Problems merging with if statement...</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Problems-merging-with-if-statement/m-p/360098#M64407</link>
      <description>&lt;P class="p1"&gt;&lt;SPAN class="s1"&gt;HI Everyone,&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="p1"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s1"&gt;I have 2 datasets (clients and hospitals). The common variable between the two is municipality. Based on the municipality I want to randomly assign clients to hospitals. I know the ratio of the local population that goes to each hospital, that is the upper and lower variable.&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="p1"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s1"&gt;In other words, I would like to merge observations where 1. the municipality matches 2. u random number falls between lower and upper bound. &lt;/SPAN&gt;&lt;/P&gt;&lt;P class="p1"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s1"&gt;My problem is that hospitals seem to be only assigned once, and I want them to be allocated to each observation based on the above rules. Sort of like a many-to-many merge...&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="p1"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s1"&gt;What am I doing wrong?&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="p1"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s1"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/SPAN&gt; clients;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s2"&gt;input&lt;/SPAN&gt; client municipality;&lt;/P&gt;&lt;P class="p2"&gt;datalines&lt;SPAN class="s3"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="p1"&gt;502 12&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="p1"&gt;503 18&lt;/P&gt;&lt;P class="p1"&gt;505 12&lt;/P&gt;&lt;P class="p1"&gt;706 44&lt;/P&gt;&lt;P class="p1"&gt;807 51&lt;/P&gt;&lt;P class="p1"&gt;508 12&lt;/P&gt;&lt;P class="p1"&gt;709 12&lt;/P&gt;&lt;P class="p1"&gt;801 44&lt;/P&gt;&lt;P class="p1"&gt;;&lt;/P&gt;&lt;P class="p3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;SPAN class="s3"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="p4"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s1"&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt;&lt;/SPAN&gt; &lt;SPAN class="s1"&gt;&lt;STRONG&gt;sort&lt;/STRONG&gt;&lt;/SPAN&gt; &lt;SPAN class="s2"&gt;data&lt;/SPAN&gt;=clients &lt;SPAN class="s2"&gt;out&lt;/SPAN&gt;=clients;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s2"&gt;BY&lt;/SPAN&gt; client;&lt;/P&gt;&lt;P class="p3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;SPAN class="s3"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="p4"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s1"&gt;&lt;STRONG&gt;Data&lt;/STRONG&gt;&lt;/SPAN&gt; clients;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s2"&gt;set&lt;/SPAN&gt; clients;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s2"&gt;by&lt;/SPAN&gt; client;&lt;/P&gt;&lt;P class="p1"&gt;u = rand(&lt;SPAN class="s4"&gt;"Uniform"&lt;/SPAN&gt;);&lt;/P&gt;&lt;P class="p3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;SPAN class="s3"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="p4"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s1"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/SPAN&gt; hospitals;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s2"&gt;input&lt;/SPAN&gt; municipality hospital_id lower upper;&lt;/P&gt;&lt;P class="p2"&gt;datalines&lt;SPAN class="s3"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="p1"&gt;12 2 0 0.3&lt;/P&gt;&lt;P class="p1"&gt;12 3 0.3 1&lt;/P&gt;&lt;P class="p1"&gt;18 7 0 0.1&lt;/P&gt;&lt;P class="p1"&gt;18 10 0.1 1&lt;/P&gt;&lt;P class="p1"&gt;51 134 0.5 1&lt;/P&gt;&lt;P class="p1"&gt;51 99 0 0.2&lt;/P&gt;&lt;P class="p1"&gt;51 123 0.2 1&lt;/P&gt;&lt;P class="p1"&gt;44 123 0 1&lt;/P&gt;&lt;P class="p1"&gt;;&lt;/P&gt;&lt;P class="p3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;SPAN class="s3"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="p4"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s1"&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt;&lt;/SPAN&gt; &lt;SPAN class="s1"&gt;&lt;STRONG&gt;sort&lt;/STRONG&gt;&lt;/SPAN&gt; &lt;SPAN class="s2"&gt;data&lt;/SPAN&gt;=clients &lt;SPAN class="s2"&gt;out&lt;/SPAN&gt;=clients;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s2"&gt;BY&lt;/SPAN&gt; municipality;&lt;/P&gt;&lt;P class="p3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;SPAN class="s3"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="p4"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s1"&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt;&lt;/SPAN&gt; &lt;SPAN class="s1"&gt;&lt;STRONG&gt;sort&lt;/STRONG&gt;&lt;/SPAN&gt; &lt;SPAN class="s2"&gt;data&lt;/SPAN&gt;=hospitals &lt;SPAN class="s2"&gt;out&lt;/SPAN&gt;=hospitals;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s2"&gt;by&lt;/SPAN&gt; municipality;&lt;/P&gt;&lt;P class="p3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;SPAN class="s3"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="p4"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s1"&gt;&lt;STRONG&gt;DATA&lt;/STRONG&gt;&lt;/SPAN&gt; work.merge;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s2"&gt;MERGE&lt;/SPAN&gt; clients hospitals;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s2"&gt;BY&lt;/SPAN&gt; municipality;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s2"&gt;if&lt;/SPAN&gt; lower&amp;lt;u &lt;SPAN class="s2"&gt;and&lt;/SPAN&gt; u&amp;lt;upper;&lt;/P&gt;&lt;P class="p3"&gt;&lt;STRONG&gt;RUN&lt;/STRONG&gt;&lt;SPAN class="s3"&gt;;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2017 19:55:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Problems-merging-with-if-statement/m-p/360098#M64407</guid>
      <dc:creator>GKati</dc:creator>
      <dc:date>2017-05-19T19:55:45Z</dc:date>
    </item>
    <item>
      <title>Re: Problems merging with if statement...</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Problems-merging-with-if-statement/m-p/360106#M64408</link>
      <description>&lt;P&gt;You might show what you want for output.&lt;/P&gt;
&lt;P&gt;You are doing what is called a many-to-many merge: many municipalities (from the client data) to many hospitals. The data step merge is seldom the appropriate tool for that type of merge. The good news is Proc Sql does so very well:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql;
   create table want as
   select a.*, b.hospital_id 
   from clients as a left join hospitals as b
      on a.municipality=b.municipality
   where a.u between b.lower and b.upper
   ;
quit;&lt;/PRE&gt;
&lt;P&gt;Also sql does not require the explicit sort beforehand.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2017 20:15:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Problems-merging-with-if-statement/m-p/360106#M64408</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-05-19T20:15:44Z</dc:date>
    </item>
    <item>
      <title>Re: Problems merging with if statement...</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Problems-merging-with-if-statement/m-p/360107#M64409</link>
      <description>&lt;P&gt;I'd suggest code similar to what&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;suggested, but I'd use a left join so that you will still output clients who don't have a matching municipality. Also, in your specs you said that you didn't want to include the two cutoffs. If that is true, you can't use the BETWEEN operator:&lt;/P&gt;
&lt;PRE&gt;data clients;
  input client municipality;
  datalines;
502 12   
503 18
505 12
706 44
807 51
508 12
709 12
801 44
;
run;
 
Data clients;
  set clients;
  u = rand("Uniform");
run;
 
data hospitals;
  input municipality hospital_id lower upper;
  datalines;
12 2 0 0.3
12 3 0.3 1
18 7 0 0.1
18 10 0.1 1
51 134 0.5 1
51 99 0 0.2
51 123 0.2 1
44 123 0 1
;
run;
 
proc sql noprint;
  create table work.merge as
    select a.*, b.hospital_id
      from clients a
        left join hospitals b
          on a.municipality=b.municipality
            where lower&amp;lt;u&amp;lt;upper
  ;
quit;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2017 20:23:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Problems-merging-with-if-statement/m-p/360107#M64409</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-05-19T20:23:48Z</dc:date>
    </item>
    <item>
      <title>Re: Problems merging with if statement...</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Problems-merging-with-if-statement/m-p/360117#M64410</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13711"&gt;@art297&lt;/a&gt;&amp;nbsp;when I tested my code with between it seemed to work okay and did not require the bounds in the output. Could this be difference between SAS and some other SQL or maybe the older version of SAS (9.2) I have here?&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2017 20:48:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Problems-merging-with-if-statement/m-p/360117#M64410</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-05-19T20:48:29Z</dc:date>
    </item>
    <item>
      <title>Re: Problems merging with if statement...</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Problems-merging-with-if-statement/m-p/360124#M64411</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;:&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/123185"&gt;@GKati&lt;/a&gt;'s specs stated:&amp;nbsp;&lt;SPAN class="s2"&gt;if&lt;/SPAN&gt;&lt;SPAN&gt; lower&amp;lt;u &lt;/SPAN&gt;&lt;SPAN class="s2"&gt;and&lt;/SPAN&gt;&lt;SPAN&gt; u&amp;lt;upper;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;I was simply pointing out that the between operator is the same as lower&amp;lt;=u&amp;lt;=upper.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think that has been true ever since SAS incorporated the between operator&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2017 21:18:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Problems-merging-with-if-statement/m-p/360124#M64411</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-05-19T21:18:33Z</dc:date>
    </item>
    <item>
      <title>Re: Problems merging with if statement...</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Problems-merging-with-if-statement/m-p/360130#M64412</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13711"&gt;@art297&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;:&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/123185"&gt;@GKati&lt;/a&gt;'s specs stated:&amp;nbsp;&lt;SPAN class="s2"&gt;if&lt;/SPAN&gt;&lt;SPAN&gt; lower&amp;lt;u &lt;/SPAN&gt;&lt;SPAN class="s2"&gt;and&lt;/SPAN&gt;&lt;SPAN&gt; u&amp;lt;upper;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;I was simply pointing out that the between operator is the same as lower&amp;lt;=u&amp;lt;=upper.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think that has been true ever since SAS incorporated the between operator&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Gotcha.&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2017 21:43:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Problems-merging-with-if-statement/m-p/360130#M64412</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-05-19T21:43:37Z</dc:date>
    </item>
    <item>
      <title>Re: Problems merging with if statement...</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Problems-merging-with-if-statement/m-p/360308#M64417</link>
      <description>&lt;P&gt;I tried both solutions and they both seem to work well. I accepted ballardw&lt;SPAN class="login-bold"&gt;'s solution just because he was first.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="login-bold"&gt;Thanks for your help.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="login-bold"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 21 May 2017 13:10:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Problems-merging-with-if-statement/m-p/360308#M64417</guid>
      <dc:creator>GKati</dc:creator>
      <dc:date>2017-05-21T13:10:31Z</dc:date>
    </item>
  </channel>
</rss>

