<?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: Pull in a new field from different tables based on table name in a field from the original table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Pull-in-a-new-field-from-different-tables-based-on-table-name-in/m-p/917969#M361606</link>
    <description>&lt;P&gt;All data tables are sas data sets and the key is unique.&lt;/P&gt;</description>
    <pubDate>Mon, 26 Feb 2024 22:16:59 GMT</pubDate>
    <dc:creator>ChickenLittle</dc:creator>
    <dc:date>2024-02-26T22:16:59Z</dc:date>
    <item>
      <title>Pull in a new field from different tables based on table name in a field from the original table.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Pull-in-a-new-field-from-different-tables-based-on-table-name-in/m-p/917966#M361603</link>
      <description>&lt;P&gt;I have a table that contains a field named TableName, which stores the name of the specific table I need to pull from for each observation (re: each observation requires me to hit against different tables to grab NeededField). For each observation, is there a way to code it so that depending on the table name in the TableName field, I can pull the field I need just from that table for that particular observation?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;P&gt;libname abc 'path/of/my/file';&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;OriginalTable&lt;/P&gt;
&lt;P&gt;obs&amp;nbsp; &amp;nbsp; key&amp;nbsp; &amp;nbsp; &amp;nbsp;TableName&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cat&amp;nbsp; &amp;nbsp; &amp;nbsp;abc.202201&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;dog&amp;nbsp; &amp;nbsp; abc.202202&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;frog&amp;nbsp; &amp;nbsp; abc.202303&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Table at abc.202201&lt;/P&gt;
&lt;P&gt;key&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;NeededField&lt;/P&gt;
&lt;P&gt;cat&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1234&lt;/P&gt;
&lt;P&gt;dog&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 12345&lt;/P&gt;
&lt;P&gt;frog&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 123456&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Table at abc.202202&lt;/P&gt;
&lt;P&gt;key&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;NeededField&lt;/P&gt;
&lt;P&gt;cat&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;345&lt;/P&gt;
&lt;P&gt;dog&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3456&lt;/P&gt;
&lt;P&gt;frog&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 34567&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Table at abc.202202&lt;/P&gt;
&lt;P&gt;key&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;NeededField&lt;/P&gt;
&lt;P&gt;cat&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5&lt;/P&gt;
&lt;P&gt;dog&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 56&lt;/P&gt;
&lt;P&gt;frog&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;567&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to see:&lt;/P&gt;
&lt;P&gt;OriginalTable&lt;/P&gt;
&lt;P&gt;obs&amp;nbsp; &amp;nbsp; key&amp;nbsp; &amp;nbsp; &amp;nbsp;TableName&amp;nbsp; &amp;nbsp; &amp;nbsp; NeededField&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cat&amp;nbsp; &amp;nbsp; &amp;nbsp;abc.202201&amp;nbsp; &amp;nbsp; &amp;nbsp; 1234&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;dog&amp;nbsp; &amp;nbsp; abc.202202&amp;nbsp; &amp;nbsp; &amp;nbsp; 3456&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;frog&amp;nbsp; &amp;nbsp; abc.202303&amp;nbsp; &amp;nbsp; &amp;nbsp; 567&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is this at all possible? Thank you in advance.&lt;/P&gt;</description>
      <pubDate>Mon, 26 Feb 2024 22:03:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Pull-in-a-new-field-from-different-tables-based-on-table-name-in/m-p/917966#M361603</guid>
      <dc:creator>ChickenLittle</dc:creator>
      <dc:date>2024-02-26T22:03:37Z</dc:date>
    </item>
    <item>
      <title>Re: Pull in a new field from different tables based on table name in a field from the original table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Pull-in-a-new-field-from-different-tables-based-on-table-name-in/m-p/917967#M361604</link>
      <description>&lt;P&gt;The table names you show are not normally valid SAS data set names. So are those "tables" SAS data sets or something in an external data system?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Basically you are going to&amp;nbsp; use the information you have write additional syntax. Possible? yes. Short and sweet? Maybe not.&lt;/P&gt;
&lt;P&gt;Is the Key that you show going to be unique in each of the tables? If the key is not unique there will need to be additional rules provided on how to select the specific observation from each external data set.&lt;/P&gt;</description>
      <pubDate>Mon, 26 Feb 2024 22:14:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Pull-in-a-new-field-from-different-tables-based-on-table-name-in/m-p/917967#M361604</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-02-26T22:14:06Z</dc:date>
    </item>
    <item>
      <title>Re: Pull in a new field from different tables based on table name in a field from the original table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Pull-in-a-new-field-from-different-tables-based-on-table-name-in/m-p/917969#M361606</link>
      <description>&lt;P&gt;All data tables are sas data sets and the key is unique.&lt;/P&gt;</description>
      <pubDate>Mon, 26 Feb 2024 22:16:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Pull-in-a-new-field-from-different-tables-based-on-table-name-in/m-p/917969#M361606</guid>
      <dc:creator>ChickenLittle</dc:creator>
      <dc:date>2024-02-26T22:16:59Z</dc:date>
    </item>
    <item>
      <title>Re: Pull in a new field from different tables based on table name in a field from the original table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Pull-in-a-new-field-from-different-tables-based-on-table-name-in/m-p/917970#M361607</link>
      <description>&lt;P&gt;How many tables, columns and rows are in your real use case? This is a really inefficient methodology for locating and reading your required data and is unlikely to scale well to large numbers of tables, columns and rows. If you explained your problem statement, there may be better approaches.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Feb 2024 22:34:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Pull-in-a-new-field-from-different-tables-based-on-table-name-in/m-p/917970#M361607</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2024-02-26T22:34:11Z</dc:date>
    </item>
    <item>
      <title>Re: Pull in a new field from different tables based on table name in a field from the original table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Pull-in-a-new-field-from-different-tables-based-on-table-name-in/m-p/917972#M361608</link>
      <description>&lt;P&gt;Doable, yes, efficient, probably not.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One option: combine all into one table with the source table identified using INDSNAME option. Then merge for desired results on the two key variables. You could limit to just key values needed if size might be an issue.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data combined;
set abc.202201-abc.202203 indsname =src;
tableName=src;
keep key NeededField tableName;
run;

proc sql;
create table want as
select a.*, b.needed_field
from have as a
left join combined as b
on upcase(a.TableName)=upcase(b.TableName)
order by 1;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/436300"&gt;@ChickenLittle&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have a table that contains a field named TableName, which stores the name of the specific table I need to pull from for each observation (re: each observation requires me to hit against different tables to grab NeededField). For each observation, is there a way to code it so that depending on the table name in the TableName field, I can pull the field I need just from that table for that particular observation?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;P&gt;libname abc 'path/of/my/file';&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;OriginalTable&lt;/P&gt;
&lt;P&gt;obs&amp;nbsp; &amp;nbsp; key&amp;nbsp; &amp;nbsp; &amp;nbsp;TableName&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cat&amp;nbsp; &amp;nbsp; &amp;nbsp;abc.202201&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;dog&amp;nbsp; &amp;nbsp; abc.202202&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;frog&amp;nbsp; &amp;nbsp; abc.202303&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Table at abc.202201&lt;/P&gt;
&lt;P&gt;key&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;NeededField&lt;/P&gt;
&lt;P&gt;cat&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1234&lt;/P&gt;
&lt;P&gt;dog&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 12345&lt;/P&gt;
&lt;P&gt;frog&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 123456&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Table at abc.202202&lt;/P&gt;
&lt;P&gt;key&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;NeededField&lt;/P&gt;
&lt;P&gt;cat&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;345&lt;/P&gt;
&lt;P&gt;dog&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3456&lt;/P&gt;
&lt;P&gt;frog&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 34567&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Table at abc.202202&lt;/P&gt;
&lt;P&gt;key&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;NeededField&lt;/P&gt;
&lt;P&gt;cat&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5&lt;/P&gt;
&lt;P&gt;dog&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 56&lt;/P&gt;
&lt;P&gt;frog&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;567&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to see:&lt;/P&gt;
&lt;P&gt;OriginalTable&lt;/P&gt;
&lt;P&gt;obs&amp;nbsp; &amp;nbsp; key&amp;nbsp; &amp;nbsp; &amp;nbsp;TableName&amp;nbsp; &amp;nbsp; &amp;nbsp; NeededField&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cat&amp;nbsp; &amp;nbsp; &amp;nbsp;abc.202201&amp;nbsp; &amp;nbsp; &amp;nbsp; 1234&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;dog&amp;nbsp; &amp;nbsp; abc.202202&amp;nbsp; &amp;nbsp; &amp;nbsp; 3456&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;frog&amp;nbsp; &amp;nbsp; abc.202303&amp;nbsp; &amp;nbsp; &amp;nbsp; 567&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is this at all possible? Thank you in advance.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Feb 2024 22:36:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Pull-in-a-new-field-from-different-tables-based-on-table-name-in/m-p/917972#M361608</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2024-02-26T22:36:36Z</dc:date>
    </item>
    <item>
      <title>Re: Pull in a new field from different tables based on table name in a field from the original table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Pull-in-a-new-field-from-different-tables-based-on-table-name-in/m-p/917973#M361609</link>
      <description>&lt;P&gt;You could just make a single lookup table and do a KEYed set.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
   length tableName $16;
   tableName = 'abc.202201';
   input key $ field;
   cards;
cat         1234
dog        12345
frog        123456
;;;;
data b;
   length tableName $16;
   TableName = 'abc.202202';
   input key $ field;
   cards;
cat         345
dog        3456
frog        34567
;;;;
data c;
   length tableName $16;
   TableName = 'abc.202303';
   input key $ Field;
   cards;
cat         5
dog        56
frog       567
;;;;
   run;
data lookuptable(index=(idx1=(tableName key)/unique));
   set a b c;
   run;
data want;
   set have;
   set lookuptable key=idx1/unique;
   run;
proc print;
   run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 202px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/94141i51154AD46CD19BDC/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Feb 2024 22:36:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Pull-in-a-new-field-from-different-tables-based-on-table-name-in/m-p/917973#M361609</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2024-02-26T22:36:43Z</dc:date>
    </item>
    <item>
      <title>Re: Pull in a new field from different tables based on table name in a field from the original table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Pull-in-a-new-field-from-different-tables-based-on-table-name-in/m-p/917980#M361612</link>
      <description>Thank you so much for your response! This works too, but I used the other responder's suggestion first. This was very helpful!</description>
      <pubDate>Mon, 26 Feb 2024 23:34:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Pull-in-a-new-field-from-different-tables-based-on-table-name-in/m-p/917980#M361612</guid>
      <dc:creator>ChickenLittle</dc:creator>
      <dc:date>2024-02-26T23:34:07Z</dc:date>
    </item>
  </channel>
</rss>

