<?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: Simplify data merge code in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/348019#M80537</link>
    <description>&lt;P&gt;Well, SQL was developed to handle relational databases, so combining data is the key functioanlity of it. &amp;nbsp;True there are times when either can be better. &amp;nbsp;As far as this question, we only see a tiny part of the problem apparently, so can only guess.&lt;/P&gt;</description>
    <pubDate>Fri, 07 Apr 2017 08:52:52 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2017-04-07T08:52:52Z</dc:date>
    <item>
      <title>Simplify data merge code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/347979#M80509</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have1;by var1;run;
data have2 (rename =(var1er=var1));set have2; run;
proc sort data=have2;by var1;run;

data want;
merge have1(in=a) have2(in=b);&lt;BR /&gt;by var1;
if a;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;Is it possible to simplify this?&lt;/P&gt;</description>
      <pubDate>Fri, 07 Apr 2017 07:00:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/347979#M80509</guid>
      <dc:creator>afiqcjohari</dc:creator>
      <dc:date>2017-04-07T07:00:26Z</dc:date>
    </item>
    <item>
      <title>Re: Simplify data merge code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/347994#M80521</link>
      <description>Not sure, in the code the statement 'if a' is doing anything. &lt;BR /&gt;&lt;BR /&gt;we could remove the same as there is no by statement.</description>
      <pubDate>Fri, 07 Apr 2017 06:49:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/347994#M80521</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2017-04-07T06:49:46Z</dc:date>
    </item>
    <item>
      <title>Re: Simplify data merge code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/347996#M80523</link>
      <description>Edited for the by statement.</description>
      <pubDate>Fri, 07 Apr 2017 07:00:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/347996#M80523</guid>
      <dc:creator>afiqcjohari</dc:creator>
      <dc:date>2017-04-07T07:00:59Z</dc:date>
    </item>
    <item>
      <title>Re: Simplify data merge code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/347998#M80524</link>
      <description>&lt;P&gt;No extra step for the rename is needed:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have1;
by var1;
run;

proc sort data=have2;
by var1er;
run;

data want;
merge
  have1 (in=a)
  have2 (
    in=b
    rename=(var1er=var1)
  )
;
by var1;
if a;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 07 Apr 2017 07:19:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/347998#M80524</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-04-07T07:19:53Z</dc:date>
    </item>
    <item>
      <title>Re: Simplify data merge code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/348003#M80526</link>
      <description>Is it possible to take out the sorting part? Could it be done within the data step merge?</description>
      <pubDate>Fri, 07 Apr 2017 08:10:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/348003#M80526</guid>
      <dc:creator>afiqcjohari</dc:creator>
      <dc:date>2017-04-07T08:10:01Z</dc:date>
    </item>
    <item>
      <title>Re: Simplify data merge code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/348011#M80532</link>
      <description>&lt;P&gt;Something like:&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table WANT as
  select A.VAR1ER as VAR1,
           B.*
  from   HAVE1 A
  left join HAVE2 B
  on      A.VAR1=B.VAR1;
quit;&lt;/PRE&gt;</description>
      <pubDate>Fri, 07 Apr 2017 08:42:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/348011#M80532</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-04-07T08:42:26Z</dc:date>
    </item>
    <item>
      <title>Re: Simplify data merge code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/348015#M80535</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/130031"&gt;@afiqcjohari&lt;/a&gt; wrote:&lt;BR /&gt;Is it possible to take out the sorting part? Could it be done within the data step merge?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;To merge, you always have to sort. Even if you use proc sql and you do not have to sort explicitly, the sql procedure will do it automatically, which basically uses the same resources and needs the same time. If you need the same order repeatedly, an explicit sort is the way to go.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Apr 2017 08:47:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/348015#M80535</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-04-07T08:47:35Z</dc:date>
    </item>
    <item>
      <title>Re: Simplify data merge code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/348016#M80536</link>
      <description>Thank you. But one of the feature I like about SAS is the data merge step. It's actually pretty elegant minus the need to sort step. :). Actually the real code outputs to many tables which data merge is very good at. Writing in proc sql would mean 2 different proc sql for 2 different tables.</description>
      <pubDate>Fri, 07 Apr 2017 08:48:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/348016#M80536</guid>
      <dc:creator>afiqcjohari</dc:creator>
      <dc:date>2017-04-07T08:48:26Z</dc:date>
    </item>
    <item>
      <title>Re: Simplify data merge code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/348019#M80537</link>
      <description>&lt;P&gt;Well, SQL was developed to handle relational databases, so combining data is the key functioanlity of it. &amp;nbsp;True there are times when either can be better. &amp;nbsp;As far as this question, we only see a tiny part of the problem apparently, so can only guess.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Apr 2017 08:52:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/348019#M80537</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-04-07T08:52:52Z</dc:date>
    </item>
    <item>
      <title>Re: Simplify data merge code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/348020#M80538</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; wrote:&lt;BR /&gt;&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/130031"&gt;@afiqcjohari&lt;/a&gt; wrote:&lt;BR /&gt;Is it possible to take out the sorting part? Could it be done within the data step merge?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;To merge, you always have to sort. Even if you use proc sql and you do not have to sort explicitly, the sql procedure will do it automatically, which basically uses the same resources and needs the same time. If you need the same order repeatedly, an explicit sort is the way to go.&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;The initial code was in proc sql, but I prefer data step merge because it can create multiple tables. Had it been in proc sql, I need to write separate proc sql for each different table. But I really want to avoid writing the 2 proc sorts, if possible. But it seems that it's not. What I don't get is, why if data merge necessitates the 2 tables to be sorted by the common variable, shouldn't data merge internally 'know' that the 2 tables will need to be sorted? Meaning, data merge should by default sort these 2 variables implicitly.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a case where data merge could be called without the tables being sorted in the first place?&lt;/P&gt;</description>
      <pubDate>Fri, 07 Apr 2017 08:54:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/348020#M80538</guid>
      <dc:creator>afiqcjohari</dc:creator>
      <dc:date>2017-04-07T08:54:07Z</dc:date>
    </item>
    <item>
      <title>Re: Simplify data merge code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/348023#M80539</link>
      <description>&lt;P&gt;I really appreciate your answer. What I don't understand is why does SAS require the programmer to explicitly write the 2 proc sorts on the 2 tables? Why can't data merge does it at one go? Because as per my understanding, data merge requires the tables to be sorted. It could&amp;nbsp;have been more concise if data merge can implicity sort the tables by itself.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Apr 2017 08:57:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/348023#M80539</guid>
      <dc:creator>afiqcjohari</dc:creator>
      <dc:date>2017-04-07T08:57:40Z</dc:date>
    </item>
    <item>
      <title>Re: Simplify data merge code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/348030#M80543</link>
      <description>&lt;P&gt;The merge statement in a data step is very simple and relies on the given order of the datasets. This is its natural behaviour. Therefore the sort is necessary if you use by with a merge.&lt;/P&gt;
&lt;P&gt;This simplicity is one of the reasons why sort &amp;amp; merge often outperforms SQL (which sorts implicitly).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A proc sort is usually three lines of code. I type that without really thinking in &amp;lt; 10 seconds. And I'm no touch-typist. To make your work easier, you can define a keyboard macro in EG that writes the sort step for you, so you only have to fill in the dataset and the by variables.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Apr 2017 09:03:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/348030#M80543</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-04-07T09:03:44Z</dc:date>
    </item>
    <item>
      <title>Re: Simplify data merge code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/348032#M80545</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/130031"&gt;@afiqcjohari&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;I really appreciate your answer. What I don't understand is why does SAS require the programmer to explicitly write the 2 proc sorts on the 2 tables? Why can't data merge does it at one go? Because as per my understanding, data merge requires the tables to be sorted. It could&amp;nbsp;have been more concise if data merge can implicity sort the tables by itself.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Misconception. Merge on its own does NOT require the datasets to be sorted. Merge with a by does need sorting.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Apr 2017 09:06:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/348032#M80545</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-04-07T09:06:02Z</dc:date>
    </item>
    <item>
      <title>Re: Simplify data merge code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/348049#M80551</link>
      <description>&lt;P&gt;Perhaps an important point .... MERGE with a BY statement requires that your data set is already in order before the DATA step begins. &amp;nbsp;If it is already in order without running PROC SORT, that is sufficient. &amp;nbsp;You only need to run PROC SORT when the data is not in the right order.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Apr 2017 11:27:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/348049#M80551</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-04-07T11:27:28Z</dc:date>
    </item>
    <item>
      <title>Re: Simplify data merge code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/348167#M80590</link>
      <description>&lt;P&gt;Also the BY statement in a data step is used with other things than just MERGE, UPDATE or MODIFY statements&amp;nbsp;in a data step.&lt;/P&gt;
&lt;P&gt;And the BY statement supports the NOTSORTED and DESCENDING options and trying to "guess" all the time which combination a user "intended" might be very problematical.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And since a basic sort statement is 3 lines of code including the "run;" I am surprised this is much of an issue at all.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Apr 2017 15:37:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/348167#M80590</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-04-07T15:37:21Z</dc:date>
    </item>
    <item>
      <title>Re: Simplify data merge code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/348605#M80741</link>
      <description>How is it possible to merge without a by statement? Otherwise how would merge know what is the variable(s) to use to merge the different tables?</description>
      <pubDate>Mon, 10 Apr 2017 07:02:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/348605#M80741</guid>
      <dc:creator>afiqcjohari</dc:creator>
      <dc:date>2017-04-10T07:02:14Z</dc:date>
    </item>
    <item>
      <title>Re: Simplify data merge code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/348610#M80745</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/130031"&gt;@afiqcjohari&lt;/a&gt; wrote:&lt;BR /&gt;How is it possible to merge without a by statement? Otherwise how would merge know what is the variable(s) to use to merge the different tables?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That is because the merge statement can simply put two tables side-by-side where the records have no logical relation to each other. The "by" in a data step is used for "by-group-processing", with a merge it also names the key variables.&lt;/P&gt;</description>
      <pubDate>Mon, 10 Apr 2017 07:40:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/348610#M80745</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-04-10T07:40:35Z</dc:date>
    </item>
    <item>
      <title>Re: Simplify data merge code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/348623#M80750</link>
      <description>It will be a nice feature to have if data merge is intelligent enough to call proc sort if 'by' statement is provided.</description>
      <pubDate>Mon, 10 Apr 2017 08:51:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/348623#M80750</guid>
      <dc:creator>afiqcjohari</dc:creator>
      <dc:date>2017-04-10T08:51:24Z</dc:date>
    </item>
    <item>
      <title>Re: Simplify data merge code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/348659#M80756</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/130031"&gt;@afiqcjohari&lt;/a&gt; wrote:&lt;BR /&gt;It will be a nice feature to have if data merge is intelligent enough to call proc sort if 'by' statement is provided.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That would be nuts. Making an implicit structural change (order) to a dataset will break other programs/apps that expect a specific order.&lt;/P&gt;
&lt;P&gt;And sorting implicitly to temporary files everytime a dataset does not have the "ordered by" attribute correctly set would destroy the performance of the data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want automated sorting, use proc sql.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Apr 2017 12:05:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/348659#M80756</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-04-10T12:05:43Z</dc:date>
    </item>
    <item>
      <title>Re: Simplify data merge code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/348948#M80861</link>
      <description>It's nuts if you bind yourself to the limitation of data merge of course. Hopefully SAS can take inspiration of other modern languages for this part. I still prefer data merge for the multiple outputs it can give as oppose to proc sql. Just that it's quite ugly to see proc sort prior to data merge. What to do...</description>
      <pubDate>Tue, 11 Apr 2017 02:03:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simplify-data-merge-code/m-p/348948#M80861</guid>
      <dc:creator>afiqcjohari</dc:creator>
      <dc:date>2017-04-11T02:03:10Z</dc:date>
    </item>
  </channel>
</rss>

