<?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: how to merge two dataset more efficiently in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-merge-two-dataset-more-efficiently/m-p/446650#M112108</link>
    <description>&lt;P&gt;Thanks for your help. I will go to learn hash.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Have a good night&lt;/P&gt;</description>
    <pubDate>Mon, 19 Mar 2018 00:23:06 GMT</pubDate>
    <dc:creator>xiangpang</dc:creator>
    <dc:date>2018-03-19T00:23:06Z</dc:date>
    <item>
      <title>how to merge two dataset more efficiently</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-merge-two-dataset-more-efficiently/m-p/446622#M112092</link>
      <description>&lt;P&gt;I want to merge eq2 and eq3 with final look like eq.&amp;nbsp;&lt;/P&gt;&lt;P&gt;my code works. but I want to know a more efficient way by sql or data step to do it.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data eq;
input ID y v x z w;
cards;
1 1 1 27 40 8
1 2 3 . 29 37
1 3 5 30 . 25
1 4 7 38 38 23
2 1 1 23 45 19
2 2 3 32 20 .
2 3 5 67 . .
2 4 7 . 27 .
3 1 1 33 23 46
3 2 3 21 12 56
3 3 5 78 . 34
3 4 7 13 45 . 
4 1 1 56 45 23
4 2 3 67 13 67  
4 3 5 . 35 13
4 4 7 48 35 56 
;
run;
data eq2;
input ID y x z w;
cards;
1 1 27 40 8
1 2 . 29 37
1 3 30 . 25
1 4 38 38 23
2 1 23 45 19
2 2 32 20 .
2 3 67 . .
2 4 . 27 .
3 1 33 23 46
3 2 21 12 56
3 3 78 . 34
3 4 13 45 . 
4 1 56 45 23
4 2 67 13 67  
4 3 . 35 13
4 4 48 35 56 
;
run;

data eq3;
input y v ;
cards;
1 1
2 3
3 5
4 7
;
run;

proc sql;
 create table m as 
select * from eq2 as a
full join 
eq3 as b
on a.y=b.y;
quit;

proc sort data=m ;
by id;
run;


data n;
format id y v x z w;
set m;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 18 Mar 2018 21:50:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-merge-two-dataset-more-efficiently/m-p/446622#M112092</guid>
      <dc:creator>xiangpang</dc:creator>
      <dc:date>2018-03-18T21:50:08Z</dc:date>
    </item>
    <item>
      <title>Re: how to merge two dataset more efficiently</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-merge-two-dataset-more-efficiently/m-p/446623#M112093</link>
      <description>&lt;P&gt;What's the first table for, EQ, since your query only uses the last 2 tables, eq2 and eq3?&lt;/P&gt;
&lt;P&gt;Since it's a many to one I would recommend a format as the fastest approach.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/194581"&gt;@xiangpang&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;I want to merge eq2 and eq3 with final look like eq.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;my code works. but I want to know a more efficient way by sql or data step to do it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data eq;
input ID y v x z w;
cards;
1 1 1 27 40 8
1 2 3 . 29 37
1 3 5 30 . 25
1 4 7 38 38 23
2 1 1 23 45 19
2 2 3 32 20 .
2 3 5 67 . .
2 4 7 . 27 .
3 1 1 33 23 46
3 2 3 21 12 56
3 3 5 78 . 34
3 4 7 13 45 . 
4 1 1 56 45 23
4 2 3 67 13 67  
4 3 5 . 35 13
4 4 7 48 35 56 
;
run;
data eq2;
input ID y x z w;
cards;
1 1 27 40 8
1 2 . 29 37
1 3 30 . 25
1 4 38 38 23
2 1 23 45 19
2 2 32 20 .
2 3 67 . .
2 4 . 27 .
3 1 33 23 46
3 2 21 12 56
3 3 78 . 34
3 4 13 45 . 
4 1 56 45 23
4 2 67 13 67  
4 3 . 35 13
4 4 48 35 56 
;
run;

data eq3;
input y v ;
cards;
1 1
2 3
3 5
4 7
;
run;

proc sql;
 create table m as 
select * from eq2 as a
full join 
eq3 as b
on a.y=b.y;
quit;

proc sort data=m ;
by id;
run;


data n;
format id y v x z w;
set m;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 18 Mar 2018 21:58:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-merge-two-dataset-more-efficiently/m-p/446623#M112093</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-03-18T21:58:47Z</dc:date>
    </item>
    <item>
      <title>Re: how to merge two dataset more efficiently</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-merge-two-dataset-more-efficiently/m-p/446624#M112094</link>
      <description>&lt;P&gt;I want to create a table same as EQ&lt;/P&gt;</description>
      <pubDate>Sun, 18 Mar 2018 22:00:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-merge-two-dataset-more-efficiently/m-p/446624#M112094</guid>
      <dc:creator>xiangpang</dc:creator>
      <dc:date>2018-03-18T22:00:17Z</dc:date>
    </item>
    <item>
      <title>Re: how to merge two dataset more efficiently</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-merge-two-dataset-more-efficiently/m-p/446625#M112095</link>
      <description>&lt;P&gt;Ok, well, PROC FORMAT is likely the fastest method. You can find a comparison of the different approaches, proc format, hash, sql, data step or DS2 on lexjansen.com.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 18 Mar 2018 22:05:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-merge-two-dataset-more-efficiently/m-p/446625#M112095</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-03-18T22:05:56Z</dc:date>
    </item>
    <item>
      <title>Re: how to merge two dataset more efficiently</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-merge-two-dataset-more-efficiently/m-p/446628#M112096</link>
      <description>&lt;P&gt;how to do merge in proc format? could you provide the code?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Sun, 18 Mar 2018 22:16:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-merge-two-dataset-more-efficiently/m-p/446628#M112096</guid>
      <dc:creator>xiangpang</dc:creator>
      <dc:date>2018-03-18T22:16:51Z</dc:date>
    </item>
    <item>
      <title>Re: how to merge two dataset more efficiently</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-merge-two-dataset-more-efficiently/m-p/446636#M112099</link>
      <description>&lt;P&gt;&lt;SPAN&gt;&amp;gt; I want to create a table same as EQ&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;That's not at all obvious when looking at your post.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Good questions bring good answers.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Using formats will be faster if table EQ3 is not too large, as this conserves the table order and avoids sorting the table&amp;nbsp;again. Thousands of values is fine. If you have millions&amp;nbsp;of values don't bother.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;To avoid re-sorting, a hash table is the next choice if you have many values.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;It is a matter of volume.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Otherwise, your existing code should be faster if you remove the &lt;FONT face="courier new,courier"&gt;proc sort&lt;/FONT&gt; and add &lt;FONT face="courier new,courier"&gt;order by&lt;/FONT&gt; instead.&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 18 Mar 2018 23:02:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-merge-two-dataset-more-efficiently/m-p/446636#M112099</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-03-18T23:02:52Z</dc:date>
    </item>
    <item>
      <title>Re: how to merge two dataset more efficiently</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-merge-two-dataset-more-efficiently/m-p/446637#M112100</link>
      <description>&lt;P&gt;Here is an example of code and benchmark result.&lt;/P&gt;
&lt;P&gt;The results will vary depending on your data and your hardware.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data F;
  retain FMTNAME 'QE';
  do START=1 to 1e7;
    LABEL=put(START,z10.);
    output;
  end;
run;


proc format cntlin=F;
run;    
%*    real time         26.92 seconds;
data W_FMT;
 do START=1 to 1e7;
   LABEL=put(START,qe.);
   output;
  end; 
run;
%*  real time           10.07 seconds ;
%*  total               37 seconds ;


data W_HASH;
  dcl hash H(dataset:'F');
  H.definekey('START');
  H.definedata('LABEL');
  H.definedone();
  LABEL='          ';
  drop RC;
  do START=1 to 1e7;
    RC=H.find(); 
    output;
  end;
run;
%*  real time           18.39 seconds ;
  

proc sql;
  create table W_SQL as
  select a.START, b.LABEL from F a, F b where a.START=b.START order by 1;
quit;
%*  real time           22.96 seconds ;
&lt;/CODE&gt;&lt;/PRE&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>Sun, 18 Mar 2018 23:05:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-merge-two-dataset-more-efficiently/m-p/446637#M112100</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-03-18T23:05:11Z</dc:date>
    </item>
    <item>
      <title>Re: how to merge two dataset more efficiently</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-merge-two-dataset-more-efficiently/m-p/446639#M112101</link>
      <description>&lt;P&gt;Relevant questions for some methods (some of which are much simpler):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is Y always an integer?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How many different values do you have for Y?&lt;/P&gt;</description>
      <pubDate>Sun, 18 Mar 2018 23:19:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-merge-two-dataset-more-efficiently/m-p/446639#M112101</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-03-18T23:19:30Z</dc:date>
    </item>
    <item>
      <title>Re: how to merge two dataset more efficiently</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-merge-two-dataset-more-efficiently/m-p/446641#M112102</link>
      <description>&lt;P&gt;thanks for your reply&lt;/P&gt;</description>
      <pubDate>Sun, 18 Mar 2018 23:30:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-merge-two-dataset-more-efficiently/m-p/446641#M112102</guid>
      <dc:creator>xiangpang</dc:creator>
      <dc:date>2018-03-18T23:30:32Z</dc:date>
    </item>
    <item>
      <title>Re: how to merge two dataset more efficiently</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-merge-two-dataset-more-efficiently/m-p/446643#M112103</link>
      <description>&lt;P&gt;Thanks for your reply.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID Y V is always an integer.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For Y, it has about 500 different value.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 18 Mar 2018 23:34:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-merge-two-dataset-more-efficiently/m-p/446643#M112103</guid>
      <dc:creator>xiangpang</dc:creator>
      <dc:date>2018-03-18T23:34:31Z</dc:date>
    </item>
    <item>
      <title>Re: how to merge two dataset more efficiently</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-merge-two-dataset-more-efficiently/m-p/446644#M112104</link>
      <description>&lt;P&gt;Thanks for the reply. I am happy to learn from you. Thanks again&lt;/P&gt;</description>
      <pubDate>Sun, 18 Mar 2018 23:36:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-merge-two-dataset-more-efficiently/m-p/446644#M112104</guid>
      <dc:creator>xiangpang</dc:creator>
      <dc:date>2018-03-18T23:36:30Z</dc:date>
    </item>
    <item>
      <title>Re: how to merge two dataset more efficiently</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-merge-two-dataset-more-efficiently/m-p/446645#M112105</link>
      <description>&lt;P&gt;Perfect.&amp;nbsp; Here's an easy way:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data eq;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;array v_values {600} _temporary_;&lt;/P&gt;
&lt;P&gt;if _n_=1 then do until (done);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;set eq3 end=done;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;v_values{y} = v;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;set eq2;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;v=.;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;if (1 &amp;lt;= y &amp;lt;= 600) then v = v_values{y};&lt;/P&gt;
&lt;P&gt;format id y v x z w;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=eq;&lt;/P&gt;
&lt;P&gt;by id;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;***** EDITED to add a &lt;FONT color="#FF0000"&gt;small correction&lt;/FONT&gt; for safety's sake.&lt;/P&gt;</description>
      <pubDate>Mon, 19 Mar 2018 00:06:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-merge-two-dataset-more-efficiently/m-p/446645#M112105</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-03-19T00:06:32Z</dc:date>
    </item>
    <item>
      <title>Re: how to merge two dataset more efficiently</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-merge-two-dataset-more-efficiently/m-p/446646#M112106</link>
      <description>&lt;P&gt;Thanks. But I have two more questions.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Although "format id y v x z w;" is used, why the column sequence is "y v id x z w;"?&lt;/P&gt;&lt;P&gt;when y is character (letter+number), how will you change the code?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 18 Mar 2018 23:48:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-merge-two-dataset-more-efficiently/m-p/446646#M112106</guid>
      <dc:creator>xiangpang</dc:creator>
      <dc:date>2018-03-18T23:48:17Z</dc:date>
    </item>
    <item>
      <title>Re: how to merge two dataset more efficiently</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-merge-two-dataset-more-efficiently/m-p/446649#M112107</link>
      <description>&lt;P&gt;It's possible that the FORMAT statement is intended to reorder the columns.&amp;nbsp; If so, getting the right order would require a small change to the program.&amp;nbsp; Also notice the &lt;FONT color="#FF0000"&gt;small correction&lt;/FONT&gt; I added.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If Y is character, a lot depends on the range of values.&amp;nbsp; In some cases, a wider array could be used, with a formula to translate from letter+number to a unique integer value.&amp;nbsp; But that's really what a hash table is for.&amp;nbsp; An informat based on eq3 would also be possible, to be created using a CNTLIN= data set to PROC FORMAT.&amp;nbsp; Sorry to say, you would need to look up some of the details there.&amp;nbsp; But the DATA step would be easy:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data eq;&lt;/P&gt;
&lt;P&gt;set eq2;&lt;/P&gt;
&lt;P&gt;v = input(y, myform.);&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this case, MYFORM. is the name of the informat that your program creates using a CNTLIN= data set to PROC FORMAT.&lt;/P&gt;</description>
      <pubDate>Mon, 19 Mar 2018 00:19:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-merge-two-dataset-more-efficiently/m-p/446649#M112107</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-03-19T00:19:29Z</dc:date>
    </item>
    <item>
      <title>Re: how to merge two dataset more efficiently</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-merge-two-dataset-more-efficiently/m-p/446650#M112108</link>
      <description>&lt;P&gt;Thanks for your help. I will go to learn hash.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Have a good night&lt;/P&gt;</description>
      <pubDate>Mon, 19 Mar 2018 00:23:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-merge-two-dataset-more-efficiently/m-p/446650#M112108</guid>
      <dc:creator>xiangpang</dc:creator>
      <dc:date>2018-03-19T00:23:06Z</dc:date>
    </item>
    <item>
      <title>Re: how to merge two dataset more efficiently</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-merge-two-dataset-more-efficiently/m-p/446651#M112109</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/194581"&gt;@xiangpang&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;how to do merge in proc format? could you provide the code?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;A href="http://www2.sas.com/proceedings/sugi30/001-30.pdf" target="_blank"&gt;http://www2.sas.com/proceedings/sugi30/001-30.pdf&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Mar 2018 00:45:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-merge-two-dataset-more-efficiently/m-p/446651#M112109</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-03-19T00:45:36Z</dc:date>
    </item>
  </channel>
</rss>

