<?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: Create new variable based on separate table. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-based-on-separate-table/m-p/262183#M51129</link>
    <description>&lt;P&gt;You're welcome.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To avoid the overwriting in the data step, you could add a KEEP= or DROP= dataset option to TABLE2.&lt;BR /&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
merge table1(in=a)
      table2(keep=cm_num ref_nbr action action_dt
             where=(action='OCCN') rename=(action_dt=open_dt));
by cm_num ref_nbr;
if a;
drop action;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 07 Apr 2016 19:50:46 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2016-04-07T19:50:46Z</dc:date>
    <item>
      <title>Create new variable based on separate table.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-based-on-separate-table/m-p/262167#M51118</link>
      <description>&lt;P&gt;I currently have two tables. &amp;nbsp;One has highlevel portoflio information, the 2nd has the specific details for each portfolio.&lt;/P&gt;
&lt;P&gt;Table 1:&lt;/P&gt;
&lt;P&gt;cm_num &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ref_nbr &amp;nbsp; &amp;nbsp; trans_id&lt;/P&gt;
&lt;P&gt;12345 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;AB456 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;12345 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;AB456 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;23456 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;BC258 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;23456 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;BC258 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;34567 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;CD987 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;34567 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;CD987 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Table 2:&lt;/P&gt;
&lt;P&gt;cm_num &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ref_nbr &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; action &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; action_dt&lt;/P&gt;
&lt;P&gt;12345 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;AB456 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;OCCN &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3/1/2016&lt;/P&gt;
&lt;P&gt;12345 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;AB456 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;OCCD &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4/6/2016&amp;nbsp;&lt;/P&gt;
&lt;P&gt;23456 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; BC258 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;PCCD &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3/15/2016&lt;/P&gt;
&lt;P&gt;23456 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; BC258 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;OCCN &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3/12/2016&lt;/P&gt;
&lt;P&gt;34567 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; CD987 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; OCCN &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4/6/2016&amp;nbsp;&lt;/P&gt;
&lt;P&gt;34567 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; CD987 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; PCCD &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3/15/2016&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As you can see Table A can have multiple entires from the same portfolio with different trans_id I need to look up the ref_nbr from Table 1 in table 2 and if it finds an OCCN for that ref_nbr I need it to take the action_dt and create a new variable called open_dt for every trans_nbr within that ref_nbr. &amp;nbsp; The results should look like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;cm_num &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ref_nbr &amp;nbsp; &amp;nbsp; trans_id &amp;nbsp; &amp;nbsp;open_dt&lt;/P&gt;
&lt;P&gt;12345 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;AB456 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3/1/2016&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;12345 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;AB456 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3/1/2016&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;23456 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;BC258 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3/12/2016&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;23456 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;BC258 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3/12/2016&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;34567 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;CD987 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4/6/2016&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;34567 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;CD987 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4/6/2016&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I am currently using SAS EG 9.4&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2016 18:50:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-based-on-separate-table/m-p/262167#M51118</guid>
      <dc:creator>Sotarkadin</dc:creator>
      <dc:date>2016-04-07T18:50:03Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variable based on separate table.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-based-on-separate-table/m-p/262172#M51123</link>
      <description>&lt;P&gt;Assuming that the combination of CM_NUM and REF_NBR can be used as a key, you could try:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
merge table1(in=a)
      table2(where=(action='OCCN') rename=(action_dt=open_dt));
by cm_num ref_nbr;
if a;
drop action;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If this doesn't work because either of the tables is not sorted by CM_NUM REF_NBR, try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select a.*, b.action_dt as open_dt
from table1 a left join table2(where=(action='OCCN')) b
on a.cm_num=b.cm_num &amp;amp; a.ref_nbr=b.ref_nbr
order by cm_num, ref_nbr, trans_id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Both solutions would create missing values for OPEN_DT if no matching record with action='OCCN' was found in table 2. In case of multiple&amp;nbsp;&lt;SPAN&gt;matching records with action='OCCN' in table 2, you will need to provide a rule for this situation.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;(Edit: Just improved code indentation.)&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2016 19:51:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-based-on-separate-table/m-p/262172#M51123</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-04-07T19:51:48Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variable based on separate table.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-based-on-separate-table/m-p/262178#M51127</link>
      <description>&lt;P&gt;Based on the way some of the extra columns in the table worked I had to use the proc sql; code as the merge code kept overwriting data in table A becuase both tables have some of the columns named the same but were not pertinant to this issue.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you so much as at first glance(I have not checked row for row on my sample data to ensure the data integrity) it seems to have worked perfectly.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2016 19:42:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-based-on-separate-table/m-p/262178#M51127</guid>
      <dc:creator>Sotarkadin</dc:creator>
      <dc:date>2016-04-07T19:42:15Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variable based on separate table.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-based-on-separate-table/m-p/262183#M51129</link>
      <description>&lt;P&gt;You're welcome.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To avoid the overwriting in the data step, you could add a KEEP= or DROP= dataset option to TABLE2.&lt;BR /&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
merge table1(in=a)
      table2(keep=cm_num ref_nbr action action_dt
             where=(action='OCCN') rename=(action_dt=open_dt));
by cm_num ref_nbr;
if a;
drop action;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2016 19:50:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-based-on-separate-table/m-p/262183#M51129</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-04-07T19:50:46Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variable based on separate table.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-based-on-separate-table/m-p/262187#M51130</link>
      <description>&lt;P&gt;Ok that makes sense, i dropped the columns I didn't need and that were named the same just with different data and that fixed it thank you. &amp;nbsp;One question I have is if both tables have a column named for example; card_num. &amp;nbsp;When it merges them which table is it coming from if for example it had different data, would it be the 2nd table?&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2016 19:57:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-based-on-separate-table/m-p/262187#M51130</guid>
      <dc:creator>Sotarkadin</dc:creator>
      <dc:date>2016-04-07T19:57:38Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variable based on separate table.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-based-on-separate-table/m-p/262190#M51132</link>
      <description>&lt;P&gt;Yes, the value from TABLE2 would overwrite that from TABLE1. If you set the following SAS system option, SAS will inform you in the log about which variables are overwritten and&amp;nbsp;in which "direction":&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options msglevel=I;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here's an example of the type of log messages you will get with this option setting:&lt;/P&gt;
&lt;PRE&gt;INFO: The variable cm_num on data set WORK.TABLE1 will be overwritten by data set WORK.TABLE2.&lt;/PRE&gt;
&lt;P&gt;Edit: The above message occurred when I used &lt;FONT face="courier new,courier"&gt;ref_nbr&lt;/FONT&gt; as the only BY variable. BY variables will not occur in these messages, because they &lt;EM&gt;have&lt;/EM&gt; to be in both datasets and by nature of match merging it's impossible that one of them is overwritten by a &lt;EM&gt;different&lt;/EM&gt; value.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2016 20:18:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-based-on-separate-table/m-p/262190#M51132</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-04-07T20:18:33Z</dc:date>
    </item>
  </channel>
</rss>

