<?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: Same variable name in 2 tables in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Same-variable-name-in-2-tables/m-p/56433#M15775</link>
    <description>If you are using a SAS data step, you can't reference a varibale by dataset, like you would in i.e. SQL. (I guess you are doing a merge)&lt;BR /&gt;
[pre]&lt;BR /&gt;
data table3(drop=Table1_Var Table2_Var);&lt;BR /&gt;
  merge table1(&lt;B&gt;rename=(var=Table1_Var)&lt;/B&gt;)&lt;BR /&gt;
        table2(&lt;B&gt;rename=(var=Table2_Var)&lt;/B&gt;);&lt;BR /&gt;
  by common_var;&lt;BR /&gt;
  NewVar = &lt;B&gt;sum&lt;/B&gt;(Table1_Var,Table2_Var);&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
The rename will not change table1 and table2, the rename will only work in the current datastep.&lt;BR /&gt;
&lt;BR /&gt;
Using "+" instead of "SUM()" can/will give wrong results if you have missing values - I changed that in my example.&lt;BR /&gt;
&lt;BR /&gt;
Message was edited by: Geniz</description>
    <pubDate>Thu, 23 Oct 2008 11:30:58 GMT</pubDate>
    <dc:creator>GertNissen</dc:creator>
    <dc:date>2008-10-23T11:30:58Z</dc:date>
    <item>
      <title>Same variable name in 2 tables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Same-variable-name-in-2-tables/m-p/56432#M15774</link>
      <description>I have 2 programs that have pretty much the same sets of code in each one, but each one uses to different tables/datasets to perform the same calculations.&lt;BR /&gt;
&lt;BR /&gt;
There is a point in each set of programs where they need to pull a resulting variable from a table/dataset from the other program, but this/these variables have the same variable name.&lt;BR /&gt;
&lt;BR /&gt;
Is there a way of referencing the variables buy table/dataset name and variable name, e.g.&lt;BR /&gt;
&lt;BR /&gt;
Table1 = Table/dataset&lt;BR /&gt;
Table2 = Table/dataset&lt;BR /&gt;
Var = Variable appearing in both Table1 &amp;amp; Table2&lt;BR /&gt;
&lt;BR /&gt;
NewVar = Table1.Var + Table2.Var&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Thanks&lt;BR /&gt;
&lt;BR /&gt;
P.s I am a newbie to SAS code at the moment so as simple as possible would be great &lt;span class="lia-unicode-emoji" title=":monkey_face:"&gt;🐵&lt;/span&gt;</description>
      <pubDate>Thu, 23 Oct 2008 09:59:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Same-variable-name-in-2-tables/m-p/56432#M15774</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-10-23T09:59:20Z</dc:date>
    </item>
    <item>
      <title>Re: Same variable name in 2 tables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Same-variable-name-in-2-tables/m-p/56433#M15775</link>
      <description>If you are using a SAS data step, you can't reference a varibale by dataset, like you would in i.e. SQL. (I guess you are doing a merge)&lt;BR /&gt;
[pre]&lt;BR /&gt;
data table3(drop=Table1_Var Table2_Var);&lt;BR /&gt;
  merge table1(&lt;B&gt;rename=(var=Table1_Var)&lt;/B&gt;)&lt;BR /&gt;
        table2(&lt;B&gt;rename=(var=Table2_Var)&lt;/B&gt;);&lt;BR /&gt;
  by common_var;&lt;BR /&gt;
  NewVar = &lt;B&gt;sum&lt;/B&gt;(Table1_Var,Table2_Var);&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
The rename will not change table1 and table2, the rename will only work in the current datastep.&lt;BR /&gt;
&lt;BR /&gt;
Using "+" instead of "SUM()" can/will give wrong results if you have missing values - I changed that in my example.&lt;BR /&gt;
&lt;BR /&gt;
Message was edited by: Geniz</description>
      <pubDate>Thu, 23 Oct 2008 11:30:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Same-variable-name-in-2-tables/m-p/56433#M15775</guid>
      <dc:creator>GertNissen</dc:creator>
      <dc:date>2008-10-23T11:30:58Z</dc:date>
    </item>
    <item>
      <title>Re: Same variable name in 2 tables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Same-variable-name-in-2-tables/m-p/56434#M15776</link>
      <description>Is it possible to do this merge as well as another merge in the same program, i.e perform 2 merges within the same code?</description>
      <pubDate>Thu, 23 Oct 2008 13:54:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Same-variable-name-in-2-tables/m-p/56434#M15776</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-10-23T13:54:44Z</dc:date>
    </item>
    <item>
      <title>Re: Same variable name in 2 tables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Same-variable-name-in-2-tables/m-p/56435#M15777</link>
      <description>Yes, you can merge as many tables as you like in the same data step, as long as you merge on a common BY variable (unlike SQL, where you can join tables on different columns).&lt;BR /&gt;
&lt;BR /&gt;
/Linus</description>
      <pubDate>Thu, 23 Oct 2008 19:50:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Same-variable-name-in-2-tables/m-p/56435#M15777</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2008-10-23T19:50:33Z</dc:date>
    </item>
    <item>
      <title>Re: Same variable name in 2 tables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Same-variable-name-in-2-tables/m-p/56436#M15778</link>
      <description>you might be able to make two different merges in one data step, (assumed to be independent merges), but I don't see why you would want to do that. Can you explain the purpose ?&lt;BR /&gt;
&lt;BR /&gt;
PeterC</description>
      <pubDate>Mon, 27 Oct 2008 13:50:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Same-variable-name-in-2-tables/m-p/56436#M15778</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-10-27T13:50:01Z</dc:date>
    </item>
  </channel>
</rss>

