<?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 with a range in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/Merging-with-a-range/m-p/599584#M18275</link>
    <description>&lt;P&gt;It sounds like you should combine the two data sets to generate this type of program:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set dataset2;
if ID &amp;lt;= 0.05 then X=37;
else if ID &amp;lt;= 0.09 then X=2;
else if ID &amp;lt;= 0.16 then X=48;
......
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here's a way to utilize your two data sets to construct and execute such a program.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
call execute('data want; set dataset2;');
do until (done);
   set dataset1 end=done;
   if_then = catx(' ', 'if ID &amp;lt;=', ID, 'then X=', X, ';');
   call execute(if_then);
   if done=0 then call execute('else');
end;
call execute('run;');&lt;BR /&gt;stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It's untested code, so see if it causes any problems vs. does the job properly.&lt;/P&gt;</description>
    <pubDate>Sat, 26 Oct 2019 23:06:12 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2019-10-26T23:06:12Z</dc:date>
    <item>
      <title>Merging with a range</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Merging-with-a-range/m-p/599234#M18266</link>
      <description>&lt;P&gt;Hi, I want to include the variable X of dataset 2 to the corresponding ID of dataset 1. The ID of dataset 2 is however a range. In the example below, the X for ID=0.56 should be 4, because the ID is below 0.73 and above 0.49, X for ID=0.22 should be 44, etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 133px;" width="128" cellspacing="0" cellpadding="0" border="0"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD style="height: 15pt; width: 49px;" height="20"&gt;Dataset 1&lt;/TD&gt;
&lt;TD style="width: 62px;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD style="height: 15pt; width: 49px;" height="20"&gt;ID&lt;/TD&gt;
&lt;TD style="width: 62px;"&gt;(expected X)&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD class="xl63" style="height: 15pt; width: 49px;" height="20" align="right"&gt;0.56&lt;/TD&gt;
&lt;TD class="xl64" style="width: 62px;" align="right"&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD class="xl63" style="height: 15pt; width: 49px;" height="20" align="right"&gt;0.22&lt;/TD&gt;
&lt;TD class="xl64" style="width: 62px;" align="right"&gt;44&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD class="xl63" style="height: 15pt; width: 49px;" height="20" align="right"&gt;0.17&lt;/TD&gt;
&lt;TD class="xl64" style="width: 62px;" align="right"&gt;44&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD class="xl63" style="height: 15pt; width: 49px;" height="20" align="right"&gt;0.09&lt;/TD&gt;
&lt;TD class="xl64" style="width: 62px;" align="right"&gt;48&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD class="xl63" style="height: 15pt; width: 49px;" height="20" align="right"&gt;0.90&lt;/TD&gt;
&lt;TD class="xl64" style="width: 62px;" align="right"&gt;44&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD class="xl63" style="height: 15pt; width: 49px;" height="20" align="right"&gt;0.37&lt;/TD&gt;
&lt;TD class="xl64" style="width: 62px;" align="right"&gt;22&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD class="xl63" style="height: 15pt; width: 49px;" height="20" align="right"&gt;0.78&lt;/TD&gt;
&lt;TD class="xl64" style="width: 62px;" align="right"&gt;44&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD class="xl63" style="height: 15pt; width: 49px;" height="20" align="right"&gt;0.22&lt;/TD&gt;
&lt;TD class="xl64" style="width: 62px;" align="right"&gt;44&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD class="xl63" style="height: 15pt; width: 49px;" height="20" align="right"&gt;0.53&lt;/TD&gt;
&lt;TD class="xl64" style="width: 62px;" align="right"&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD class="xl63" style="height: 15pt; width: 49px;" height="20" align="right"&gt;0.12&lt;/TD&gt;
&lt;TD class="xl64" style="width: 62px;" align="right"&gt;48&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD class="xl63" style="height: 15pt; width: 49px;" height="20" align="right"&gt;0.52&lt;/TD&gt;
&lt;TD class="xl64" style="width: 62px;" align="right"&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD class="xl63" style="height: 15pt; width: 49px;" height="20" align="right"&gt;0.32&lt;/TD&gt;
&lt;TD class="xl64" style="width: 62px;" align="right"&gt;22&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp; &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 96pt;" width="128" cellspacing="0" cellpadding="0" border="0"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD style="height: 15.0pt; width: 48pt;" width="64" height="20"&gt;Dataset 2&lt;/TD&gt;
&lt;TD style="width: 48pt;" width="64"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD style="height: 15.0pt;" height="20"&gt;ID&lt;/TD&gt;
&lt;TD&gt;X&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD class="xl65" style="height: 15.0pt;" height="20" align="right"&gt;0.05&lt;/TD&gt;
&lt;TD class="xl66" align="right"&gt;37&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD class="xl65" style="height: 15.0pt;" height="20" align="right"&gt;0.09&lt;/TD&gt;
&lt;TD class="xl66" align="right"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD class="xl65" style="height: 15.0pt;" height="20" align="right"&gt;0.16&lt;/TD&gt;
&lt;TD class="xl66" align="right"&gt;48&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD class="xl65" style="height: 15.0pt;" height="20" align="right"&gt;0.31&lt;/TD&gt;
&lt;TD class="xl66" align="right"&gt;44&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD class="xl65" style="height: 15.0pt;" height="20" align="right"&gt;0.38&lt;/TD&gt;
&lt;TD class="xl66" align="right"&gt;22&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD class="xl65" style="height: 15.0pt;" height="20" align="right"&gt;0.49&lt;/TD&gt;
&lt;TD class="xl66" align="right"&gt;38&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD class="xl65" style="height: 15.0pt;" height="20" align="right"&gt;0.73&lt;/TD&gt;
&lt;TD class="xl66" align="right"&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD class="xl65" style="height: 15.0pt;" height="20" align="right"&gt;0.77&lt;/TD&gt;
&lt;TD class="xl66" align="right"&gt;27&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD class="xl65" style="height: 15.0pt;" height="20" align="right"&gt;0.92&lt;/TD&gt;
&lt;TD class="xl66" align="right"&gt;44&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD class="xl65" style="height: 15.0pt;" height="20" align="right"&gt;0.96&lt;/TD&gt;
&lt;TD class="xl66" align="right"&gt;12&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD class="xl65" style="height: 15.0pt;" height="20" align="right"&gt;0.99&lt;/TD&gt;
&lt;TD class="xl66" align="right"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD class="xl65" style="height: 15.0pt;" height="20" align="right"&gt;1.00&lt;/TD&gt;
&lt;TD class="xl66" align="right"&gt;5&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Not really sure if the merge statement is suitable for this.&lt;/P&gt;</description>
      <pubDate>Fri, 25 Oct 2019 05:44:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Merging-with-a-range/m-p/599234#M18266</guid>
      <dc:creator>Demographer</dc:creator>
      <dc:date>2019-10-25T05:44:40Z</dc:date>
    </item>
    <item>
      <title>Re: Merging with a range</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Merging-with-a-range/m-p/599238#M18267</link>
      <description>&lt;P&gt;Here is one way&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data1;
input ID;
datalines;
0.56
0.22
0.17
0.09
0.90
0.37
0.78
0.22
0.53
0.12
0.52
0.32
;

data data2;
input ID X;
datalines;
0.05 37
0.09 2
0.16 48
0.31 44
0.38 22
0.49 38
0.73 4
0.77 27
0.92 44
0.96 12
0.99 1
1.00 5
;

data temp;
    set data2;
    id_from=lag(id);
    rename id=id_to;
run;

data want(keep=ID X);
    if _N_=1 then do;
        declare hash h(dataset:'temp');
        h.definekey(all:'Y');
        h.definedone();
        declare hiter hi('h');
    end;

    set data1;
    if 0 then set temp;

  	do rc=hi.first() by 0 while (rc=0);
        if id_from &amp;lt;= ID &amp;lt;= id_to then leave;
		rc=hi.next();
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;ID      X
0.56    4
0.22    44
0.17    44
0.09    48
0.9     44
0.37    22
0.78    44
0.22    44
0.53    4
0.12    48
0.52    4
0.32    22&lt;/PRE&gt;</description>
      <pubDate>Fri, 25 Oct 2019 06:11:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Merging-with-a-range/m-p/599238#M18267</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-10-25T06:11:10Z</dc:date>
    </item>
    <item>
      <title>Re: Merging with a range</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Merging-with-a-range/m-p/599256#M18269</link>
      <description>&lt;P&gt;As an alternative, create a value format with ranges from dataset 2, and use it on dataset 1.&lt;/P&gt;</description>
      <pubDate>Fri, 25 Oct 2019 07:48:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Merging-with-a-range/m-p/599256#M18269</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-10-25T07:48:15Z</dc:date>
    </item>
    <item>
      <title>Re: Merging with a range</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Merging-with-a-range/m-p/599257#M18270</link>
      <description>How does this work?</description>
      <pubDate>Fri, 25 Oct 2019 07:52:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Merging-with-a-range/m-p/599257#M18270</guid>
      <dc:creator>Demographer</dc:creator>
      <dc:date>2019-10-25T07:52:02Z</dc:date>
    </item>
    <item>
      <title>Re: Merging with a range</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Merging-with-a-range/m-p/599258#M18271</link>
      <description>&lt;P&gt;Something like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp(keep=fmtname start end label);
    merge data2(rename=id=start)
          data2(firstobs=2 keep=id X rename=(id=end X=label));
    retain fmtname 'range';
    if end ne .;
run;

proc format library=work cntlin=temp;
run;

data want;
    set data1;
    X=put(ID, range.);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 25 Oct 2019 08:14:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Merging-with-a-range/m-p/599258#M18271</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-10-25T08:14:42Z</dc:date>
    </item>
    <item>
      <title>Re: Merging with a range</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Merging-with-a-range/m-p/599584#M18275</link>
      <description>&lt;P&gt;It sounds like you should combine the two data sets to generate this type of program:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set dataset2;
if ID &amp;lt;= 0.05 then X=37;
else if ID &amp;lt;= 0.09 then X=2;
else if ID &amp;lt;= 0.16 then X=48;
......
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here's a way to utilize your two data sets to construct and execute such a program.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
call execute('data want; set dataset2;');
do until (done);
   set dataset1 end=done;
   if_then = catx(' ', 'if ID &amp;lt;=', ID, 'then X=', X, ';');
   call execute(if_then);
   if done=0 then call execute('else');
end;
call execute('run;');&lt;BR /&gt;stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It's untested code, so see if it causes any problems vs. does the job properly.&lt;/P&gt;</description>
      <pubDate>Sat, 26 Oct 2019 23:06:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Merging-with-a-range/m-p/599584#M18275</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-10-26T23:06:12Z</dc:date>
    </item>
  </channel>
</rss>

