<?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: Selecting columns line by line, when joining tables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Selecting-columns-line-by-line-when-joining-tables/m-p/554583#M154285</link>
    <description>&lt;P&gt;Also, if you prefer a data step lookup approach, replace the PROC SQL step with&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   if 0 then set b_long;
   if _N_ = 1 then do;
      declare hash h(dataset:'b_long');
      h.defineKey('Company_Name', 'dealyear');
      h.defineData('stockprice');
      h.defineDone();
   end;

   set a;

   rc=h.find();
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 28 Apr 2019 13:54:15 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2019-04-28T13:54:15Z</dc:date>
    <item>
      <title>Selecting columns line by line, when joining tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-columns-line-by-line-when-joining-tables/m-p/554578#M154282</link>
      <description>&lt;P&gt;Hi friends, it's difficult to elaborate.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Let's say for Table A, I have column 'firmname' and 'dealyear' representing when a M&amp;amp;A deal was taken place.&lt;/P&gt;&lt;P&gt;and for Table B I have column 'firmname', '2015', '2016', '2017', '2018', and '2019', representing annual average stock value of the company.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now I wanna add column called 'stockprice' in table A, thus, for the respective deal info in table A there would be a stock value of the company, of the year.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone let me know how to achieve this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;example of table A:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Company_Name&lt;/TD&gt;&lt;TD&gt;dealyear&lt;/TD&gt;&lt;TD&gt;stockprice ( to be added with the value from Table B)&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Abcam plc&lt;/TD&gt;&lt;TD&gt;2000&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Abeona Therapeutics Inc&lt;/TD&gt;&lt;TD&gt;1999&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Abiomed Inc&lt;/TD&gt;&lt;TD&gt;2001&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Acadia Healthcare&lt;/TD&gt;&lt;TD&gt;2002&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;example of table B:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Company_Name&lt;/TD&gt;&lt;TD&gt;_1999&lt;/TD&gt;&lt;TD&gt;_2000&lt;/TD&gt;&lt;TD&gt;_2001&lt;/TD&gt;&lt;TD&gt;_2002&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Abcam plc&lt;/TD&gt;&lt;TD&gt;6.648748&lt;/TD&gt;&lt;TD&gt;7.526305&lt;/TD&gt;&lt;TD&gt;14.21621&lt;/TD&gt;&lt;TD&gt;17.03757&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Abeona Therapeutics Inc&lt;/TD&gt;&lt;TD&gt;602.3358&lt;/TD&gt;&lt;TD&gt;1363.622&lt;/TD&gt;&lt;TD&gt;933.9541&lt;/TD&gt;&lt;TD&gt;667.004&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Abiomed Inc&lt;/TD&gt;&lt;TD&gt;8.457992&lt;/TD&gt;&lt;TD&gt;24.72086&lt;/TD&gt;&lt;TD&gt;20.06118&lt;/TD&gt;&lt;TD&gt;7.379444&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Acadia Healthcare&lt;/TD&gt;&lt;TD&gt;4.092705&lt;/TD&gt;&lt;TD&gt;4.483808&lt;/TD&gt;&lt;TD&gt;1.457923&lt;/TD&gt;&lt;TD&gt;2.454762&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Sun, 28 Apr 2019 13:10:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-columns-line-by-line-when-joining-tables/m-p/554578#M154282</guid>
      <dc:creator>jimmychoi</dc:creator>
      <dc:date>2019-04-28T13:10:49Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting columns line by line, when joining tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-columns-line-by-line-when-joining-tables/m-p/554580#M154283</link>
      <description>&lt;P&gt;Transpose your B table from wide to long and look up from there. Makes the whole problem much simpler &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
input Company_Name:$50. dealyear;
infile datalines dlm=',';
datalines;
Abcam plc,2000
Abeona Therapeutics Inc,1999
Abiomed Inc,2001
Acadia Healthcare,2002
;

data b;
input Company_Name:$50. _1999 _2000 _2001 _2002;
infile datalines dlm=',';
datalines;
Abcam plc,6.648748,7.526305,14.21621,17.03757
Abeona Therapeutics Inc,602.3358,1363.622,933.9541,667.004
Abiomed Inc,8.457992,24.72086,20.06118,7.379444
Acadia Healthcare,4.092705,4.483808,1.457923,2.454762
;

/* transpose b from wide to long */
data b_long(keep=Company_Name dealyear stockprice);
  set b;
  array y{*} _1999-_2002;
  do i=1 to dim(y);
     dealyear=input(compress(vname(y[i]), '_'), best.);
     stockprice=y[i];
     output;
  end;
run;

proc sql;
   create table want as
   select a.*,
          b_long.stockprice
   from a, b_long
   where a.Company_Name=b_long.Company_Name
   and   a.dealyear=b_long.dealyear;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 28 Apr 2019 13:53:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-columns-line-by-line-when-joining-tables/m-p/554580#M154283</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-04-28T13:53:26Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting columns line by line, when joining tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-columns-line-by-line-when-joining-tables/m-p/554583#M154285</link>
      <description>&lt;P&gt;Also, if you prefer a data step lookup approach, replace the PROC SQL step with&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   if 0 then set b_long;
   if _N_ = 1 then do;
      declare hash h(dataset:'b_long');
      h.defineKey('Company_Name', 'dealyear');
      h.defineData('stockprice');
      h.defineDone();
   end;

   set a;

   rc=h.find();
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 28 Apr 2019 13:54:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-columns-line-by-line-when-joining-tables/m-p/554583#M154285</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-04-28T13:54:15Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting columns line by line, when joining tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-columns-line-by-line-when-joining-tables/m-p/554584#M154286</link>
      <description>&lt;P&gt;Or simply merge by &lt;FONT face="courier new,courier"&gt;Company_Name&lt;/FONT&gt;:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have_a;
input Company_Name $25. dealyear;
cards;
Abcam plc                2000  
Abeona Therapeutics Inc  1999  
Abiomed Inc              2001  
Acadia Healthcare        2002  
;

data have_b;
input Company_Name $25. _1999-_2002;
cards;
Abcam plc                6.648748 7.526305 14.21621 17.03757
Abeona Therapeutics Inc  602.3358 1363.622 933.9541 667.004
Abiomed Inc              8.457992 24.72086 20.06118 7.379444
Acadia Healthcare        4.092705 4.483808 1.457923 2.454762
;

data want(drop=_:);
merge have_a(in=a) have_b;
by Company_Name;
if a;
array _[1999:2002] _:;
stockprice=_[dealyear];
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Alternatively, you can omit the ARRAY statement and define &lt;FONT face="courier new,courier"&gt;stockprice&lt;/FONT&gt; by means of the VVALUEX function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;stockprice=input(vvaluex(cats('_',dealyear)),12.);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Of course, merging by &lt;FONT face="courier new,courier"&gt;Company_Name&lt;/FONT&gt; requires that there are no spelling differences (upper/lower case, puctuation, etc.) between the two datasets and that they are sorted by this variable.&lt;/P&gt;</description>
      <pubDate>Sun, 28 Apr 2019 13:58:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-columns-line-by-line-when-joining-tables/m-p/554584#M154286</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-04-28T13:58:22Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting columns line by line, when joining tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-columns-line-by-line-when-joining-tables/m-p/554594#M154291</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/215463"&gt;@jimmychoi&lt;/a&gt;&amp;nbsp; &amp;nbsp;Your table B names with _ prefix gives me the impression, that's actually&amp;nbsp; result of a transposed dataset?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 28 Apr 2019 15:19:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-columns-line-by-line-when-joining-tables/m-p/554594#M154291</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-04-28T15:19:36Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting columns line by line, when joining tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-columns-line-by-line-when-joining-tables/m-p/556797#M155132</link>
      <description>Hi Draycut, your code works just perfect. and for the transposing part, can I do similar thing using SQL?</description>
      <pubDate>Tue, 07 May 2019 14:53:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-columns-line-by-line-when-joining-tables/m-p/556797#M155132</guid>
      <dc:creator>jimmychoi</dc:creator>
      <dc:date>2019-05-07T14:53:35Z</dc:date>
    </item>
  </channel>
</rss>

