<?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 datasets with the same variable names? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-two-datasets-with-the-same-variable-names/m-p/628463#M185711</link>
    <description>&lt;P&gt;There is no AUTOMATIC renaming.&amp;nbsp; You could write some code to compare the variable names from the two datasets and then use that to generate code.&amp;nbsp; But for two datasets it is not worth the effort.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You second error is probably because you tried to run a IF statement outside of any DATA step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For what your describe the easiest way is to use the RENAME= dataset option on INCOMING datasets so the name conflict is fixed BEFORE the data step tries to combine the data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So for example if both datasets had variables E3 and F1 you might do something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge
    sasuser.datA (rename=(  e3 = A_e3 f1=A_f1 ))
    sasuser.datB (rename=(  e3 = B_e3 f1=B_f1 ))
  ;
  by id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I rarely use the RENAME= dataset option on an outgoing dataset from a DATA step. For that you can just use the RENAME statement instead.&amp;nbsp; The RENAME= dataset option is more useful on the outgoing dataset from a PROC step where you cannot use the RENAME statement.&lt;/P&gt;</description>
    <pubDate>Sat, 29 Feb 2020 17:39:19 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2020-02-29T17:39:19Z</dc:date>
    <item>
      <title>How to merge two datasets with the same variable names?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-two-datasets-with-the-same-variable-names/m-p/628449#M185703</link>
      <description>&lt;P&gt;Hi, I have two datasets saved in sasuser library. Both are mostly different, however some of variables have the same names but they mean something different. Please, let me know how to merge them with automatic new variables names in one of datasets (for repeated variables names).&lt;/P&gt;&lt;P&gt;My code at the moment is:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data a(rename=(E3=E33)); /*(here is the try for renaming one of known repeated names of variable)&lt;/P&gt;&lt;P&gt;set sasuser.datA(rename=(E3=E3num E3num=E33 F1=F111 UW1=UW11));&lt;/P&gt;&lt;P&gt;E3 = put(E3num, 7.);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;data datBA;&lt;BR /&gt;merge sasuser.datB datA;&lt;BR /&gt;by id;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;if age_group = '55 - 59 y' then delete;&lt;/P&gt;&lt;P&gt;Moreover, the log says as well that an error occur when i try to delete some observations "ERROR 180-322: Statement is not valid or it is used out of proper order".&lt;/P&gt;&lt;P&gt;Can you see any issues in my code?&lt;/P&gt;&lt;P&gt;Thank you in advance for any response.&lt;/P&gt;</description>
      <pubDate>Sat, 29 Feb 2020 15:30:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-two-datasets-with-the-same-variable-names/m-p/628449#M185703</guid>
      <dc:creator>Skillside</dc:creator>
      <dc:date>2020-02-29T15:30:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to merge two datasets with the same variable names?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-two-datasets-with-the-same-variable-names/m-p/628452#M185706</link>
      <description>&lt;P&gt;For the renaming part the code could look as below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data datBA;
  merge 
    sasuser.datB (rename=(e3=datb_e3 e4=datb_e4))
    datA
    ;
  by id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can now write code like:&lt;/P&gt;
&lt;P&gt;e3=put(datab_e3, 7.)&amp;nbsp;&lt;/P&gt;
&lt;P&gt;...but make sure that you don't have a variable e3 coming from source table datA.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you just want to change the variable e3 then you could also use code as below:&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;For the error you observe:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hard to say without the full SAS log.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 29 Feb 2020 15:44:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-two-datasets-with-the-same-variable-names/m-p/628452#M185706</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-02-29T15:44:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to merge two datasets with the same variable names?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-two-datasets-with-the-same-variable-names/m-p/628463#M185711</link>
      <description>&lt;P&gt;There is no AUTOMATIC renaming.&amp;nbsp; You could write some code to compare the variable names from the two datasets and then use that to generate code.&amp;nbsp; But for two datasets it is not worth the effort.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You second error is probably because you tried to run a IF statement outside of any DATA step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For what your describe the easiest way is to use the RENAME= dataset option on INCOMING datasets so the name conflict is fixed BEFORE the data step tries to combine the data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So for example if both datasets had variables E3 and F1 you might do something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge
    sasuser.datA (rename=(  e3 = A_e3 f1=A_f1 ))
    sasuser.datB (rename=(  e3 = B_e3 f1=B_f1 ))
  ;
  by id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I rarely use the RENAME= dataset option on an outgoing dataset from a DATA step. For that you can just use the RENAME statement instead.&amp;nbsp; The RENAME= dataset option is more useful on the outgoing dataset from a PROC step where you cannot use the RENAME statement.&lt;/P&gt;</description>
      <pubDate>Sat, 29 Feb 2020 17:39:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-two-datasets-with-the-same-variable-names/m-p/628463#M185711</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-02-29T17:39:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to merge two datasets with the same variable names?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-two-datasets-with-the-same-variable-names/m-p/628486#M185725</link>
      <description>Thank You guys for the answers. Well, isn't it a limitation of SAS then? Because overwriting the databases because of same variables names is kinda not comfortable? Once again, thank you.</description>
      <pubDate>Sat, 29 Feb 2020 21:54:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-two-datasets-with-the-same-variable-names/m-p/628486#M185725</guid>
      <dc:creator>Skillside</dc:creator>
      <dc:date>2020-02-29T21:54:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to merge two datasets with the same variable names?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-two-datasets-with-the-same-variable-names/m-p/628489#M185728</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/279880"&gt;@Skillside&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thank You guys for the answers. Well, isn't it a limitation of SAS then? Because overwriting the databases because of same variables names is kinda not comfortable? Once again, thank you.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;No database will let you have 2 variables/columns with the same name in the same table.&lt;/P&gt;</description>
      <pubDate>Sat, 29 Feb 2020 22:05:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-two-datasets-with-the-same-variable-names/m-p/628489#M185728</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-02-29T22:05:44Z</dc:date>
    </item>
  </channel>
</rss>

