<?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 get values from a different table without merging ? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-values-from-a-different-table-without-merging/m-p/490891#M128593</link>
    <description>&lt;P&gt;proc transpose is one of the most useful tools to beat data into easily usable form. It allows you to have code that needs no explicit variable names where those are undetermined.&lt;/P&gt;</description>
    <pubDate>Wed, 29 Aug 2018 15:32:15 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2018-08-29T15:32:15Z</dc:date>
    <item>
      <title>how to get values from a different table without merging ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-values-from-a-different-table-without-merging/m-p/490753#M128494</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It should be pretty simple but I can't find a way to do this quickly.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have table 1 that looks like this :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;a&lt;/TD&gt;&lt;TD&gt;b&lt;/TD&gt;&lt;TD&gt;c&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;plumber&lt;/TD&gt;&lt;TD&gt;21a1&lt;/TD&gt;&lt;TD&gt;21a1&lt;/TD&gt;&lt;TD&gt;21a1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;fireman&lt;/TD&gt;&lt;TD&gt;47b1&lt;/TD&gt;&lt;TD&gt;47b2&lt;/TD&gt;&lt;TD&gt;47b3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;policeman&lt;/TD&gt;&lt;TD&gt;47b1&lt;/TD&gt;&lt;TD&gt;47b2&lt;/TD&gt;&lt;TD&gt;47b3&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and table 2 that looks like this :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;occupation&lt;/TD&gt;&lt;TD&gt;level&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;plumber&lt;/TD&gt;&lt;TD&gt;a&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;plumber&lt;/TD&gt;&lt;TD&gt;a&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;fireman&lt;/TD&gt;&lt;TD&gt;c&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;policeman&lt;/TD&gt;&lt;TD&gt;b&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;policeman&lt;/TD&gt;&lt;TD&gt;a&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And I would like to add a column to table 2, like this :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;occupation&lt;/TD&gt;&lt;TD&gt;level&lt;/TD&gt;&lt;TD&gt;code&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;plumber&lt;/TD&gt;&lt;TD&gt;a&lt;/TD&gt;&lt;TD&gt;21a1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;plumber&lt;/TD&gt;&lt;TD&gt;a&lt;/TD&gt;&lt;TD&gt;21a1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;fireman&lt;/TD&gt;&lt;TD&gt;c&lt;/TD&gt;&lt;TD&gt;47b3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;policeman&lt;/TD&gt;&lt;TD&gt;b&lt;/TD&gt;&lt;TD&gt;47b2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;policeman&lt;/TD&gt;&lt;TD&gt;a&lt;/TD&gt;&lt;TD&gt;47b1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was thinking of a complicated solution with a merge but I'm sure there is some more efficient solution. Also, in my table 1, I have way more columns than just a, b, c. So i'm looking for some function that can read columns names and put it in relation with the modalities of the "level" variable in table 2.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Aug 2018 08:03:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-get-values-from-a-different-table-without-merging/m-p/490753#M128494</guid>
      <dc:creator>alex_philby</dc:creator>
      <dc:date>2018-08-29T08:03:01Z</dc:date>
    </item>
    <item>
      <title>Re: how to get values from a different table without merging ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-values-from-a-different-table-without-merging/m-p/490756#M128497</link>
      <description>&lt;P&gt;Do something like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table1;
length occupation $20;
input occupation$ a$ b$ c$;
datalines;
plumber 21a1 21a1 21a1
fireman 47b1 47b2 47b3
policeman 47b1 47b2 47b3
;

data table2;
length occupation $20;
input occupation $ level $;
datalines;
plumber a
plumber a
fireman c
policeman b
policeman a
;

data table1_long(keep=occupation level code);
   set table1;
   array levels{*} a b c;
   do i=1 to dim(levels);
      level=vname(levels[i]);
      code=levels[i];
      output;
   end;
run;

proc sql;
   create table want as
   select a.*
         ,b.code
   from table2 as a, table1_long as b
   where a.occupation=b.occupation and a.level=b.level;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Aug 2018 08:39:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-get-values-from-a-different-table-without-merging/m-p/490756#M128497</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2018-08-29T08:39:12Z</dc:date>
    </item>
    <item>
      <title>Re: how to get values from a different table without merging ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-values-from-a-different-table-without-merging/m-p/490757#M128498</link>
      <description>&lt;P&gt;Post test data as a datastep!&amp;nbsp; I am not typing in test data.&amp;nbsp; As such this is untested:&lt;/P&gt;
&lt;PRE&gt;data want;
  merge a b;
  by occupation;
  array v{3} a b c;
  do i=1 to 3;
    if level=vname(v{i}) then code=v{i};
  end;
run;&lt;/PRE&gt;
&lt;P&gt;Thats using an array.&amp;nbsp; A simpler, more effective solution overall and for all your data storage needs is to store data in normalised form, i.e. more rows, less columns.&amp;nbsp; So change table 1 to look like:&lt;BR /&gt;plumber a&amp;nbsp; 21a1&lt;/P&gt;
&lt;P&gt;plumber b 21a1&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;then it is simply merging by occupation level.&amp;nbsp; You can simply transpose table 1 to get this format.&amp;nbsp; E.g&lt;/P&gt;
&lt;PRE&gt;data table1;
  input occupation $ a $ b $ c $;
datalines;
plumber   21a1   21a1   21a1
fireman   47b1   47b2   47b3
policeman   47b1   47b2   47b3
;
run;

proc sort data=table1;
  by occupation;
run;

proc transpose data=table1 out=table1;
  by occupation;
  var a b c;
run;&lt;/PRE&gt;</description>
      <pubDate>Wed, 29 Aug 2018 08:33:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-get-values-from-a-different-table-without-merging/m-p/490757#M128498</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-08-29T08:33:05Z</dc:date>
    </item>
    <item>
      <title>Re: how to get values from a different table without merging ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-values-from-a-different-table-without-merging/m-p/490758#M128499</link>
      <description>&lt;P&gt;First step: transpose table1 to a long format:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=table1;
by occupation;
run;

proc transpose
  data=table1
  out=table1_trans (
    rename=(col1=code _name_=level)
    where=(level ne 'occupation')
  )
;
by occupation notsorted;
var _all_;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;From this, you can either&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;merge with table2 by occupation and level (sort table2 by occupation and level)&lt;/LI&gt;
&lt;LI&gt;load a hash table in a straight data step (no sorting of table 2 needed)&lt;/LI&gt;
&lt;LI&gt;create a format for the combined values of occupation and level, and apply this on a combined value of occupation !! level in table2 (also no sorting needed)&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;A hash solution will look like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table1;
input occupation :$15. a $ b $ c $;
cards;
plumber 21a1 21a1 21a1
fireman 47b1 47b2 47b3
policeman 47b1 47b2 47b3
;
run;

data table2;
input occupation :$15. level $;
cards;
plumber a
plumber a
fireman c
policeman b
policeman a
;
run;

proc sort data=table1;
by occupation;
run;

proc transpose
  data=table1
  out=table1_trans (
    rename=(col1=code _name_=level)
    where=(level ne 'occupation')
  )
;
by occupation;
var _all_;
run;

data want;
set table2;
length code $4;
if _n_ = 1
then do;
  declare hash codes (dataset:'table1_trans');
  rc = codes.definekey('occupation','level');
  rc = codes.definedata('code');
  rc = codes.definedone();
end;
rc = codes.find();
drop rc;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note the datasteps for the example datasets. They make it much easier for the helpers to recreate data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Aug 2018 08:43:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-get-values-from-a-different-table-without-merging/m-p/490758#M128499</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-08-29T08:43:11Z</dc:date>
    </item>
    <item>
      <title>Re: how to get values from a different table without merging ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-values-from-a-different-table-without-merging/m-p/490850#M128566</link>
      <description>&lt;P&gt;Thank you for the explanations, I think I get the idea. Your program great because I don't have to type all the names of the columns in table 1.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Aug 2018 13:59:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-get-values-from-a-different-table-without-merging/m-p/490850#M128566</guid>
      <dc:creator>alex_philby</dc:creator>
      <dc:date>2018-08-29T13:59:40Z</dc:date>
    </item>
    <item>
      <title>Re: how to get values from a different table without merging ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-values-from-a-different-table-without-merging/m-p/490891#M128593</link>
      <description>&lt;P&gt;proc transpose is one of the most useful tools to beat data into easily usable form. It allows you to have code that needs no explicit variable names where those are undetermined.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Aug 2018 15:32:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-get-values-from-a-different-table-without-merging/m-p/490891#M128593</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-08-29T15:32:15Z</dc:date>
    </item>
  </channel>
</rss>

