<?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: Macro to extract table name from main table and query that table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-table-name-from-main-table-and-query-that-table/m-p/722935#M224257</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/226241"&gt;@AMSAS&lt;/a&gt;&amp;nbsp;&lt;SPAN&gt;Thank you for the program, but it's not producing the desired results.&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;I've corrected the initial post to have better view. I want the value of macro variable to hold data of data_name.&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 02 Mar 2021 17:50:23 GMT</pubDate>
    <dc:creator>David_Billa</dc:creator>
    <dc:date>2021-03-02T17:50:23Z</dc:date>
    <item>
      <title>Macro to extract table name from main table and query that table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-table-name-from-main-table-and-query-that-table/m-p/722875#M224219</link>
      <description>&lt;P&gt;I'm trying to create a macro variable called TABLE&amp;nbsp;which holds the value of the variable 'Data_name'&amp;nbsp;from the table Meta. Once the macro variable is created I want to use that macro variable as one of the table name to query.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Ideally I want to accomplish something like this but the value of TABLE should come from META table from the field 'Data_name'. Any help?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;/*Macro variable Table is value is from the field Data_name in Meta table*/
Proc sql;
create table aa as select t1.*,t2.Company
from &amp;amp;table. t1 inner join 
meta t2
on (t1.ID=t2.ID)
;
quit; &lt;/PRE&gt;
&lt;P&gt;I've data like this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data Meta;
  input Id Company :$40. Data_name :$32. ;
cards;
1234 XXX  Customer
5678 YYY  Insurance
3456 ZZZZ Health
;

data Insurance;
  input Id Type $ Segment $;
cards;
5678 NLG AS2
;

data Customer;
  input Id Flag $ Report $ ;
cards;
1234 Yes Test
;&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Mar 2021 02:45:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-table-name-from-main-table-and-query-that-table/m-p/722875#M224219</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2021-03-03T02:45:38Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to extract table name from main table and query that table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-table-name-from-main-table-and-query-that-table/m-p/722880#M224223</link>
      <description>&lt;P&gt;Your code won't run, because reporting_purpose is not present in dataset meta.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Rule #1 of making code dynamic: &lt;U&gt;&lt;STRONG&gt;start with working non-dynamic code&lt;/STRONG&gt;&lt;/U&gt;.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Mar 2021 15:35:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-table-name-from-main-table-and-query-that-table/m-p/722880#M224223</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-03-02T15:35:11Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to extract table name from main table and query that table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-table-name-from-main-table-and-query-that-table/m-p/722897#M224230</link>
      <description>&lt;P&gt;Hi, it is not clear to me what you want to achieve. Still here's some code that puts the data_name into macro variables.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create Sample Data */
data meta ;
	infile cards ;
	input id:$12. company:$12. data_name:$12.;
cards;
1234	XXX	Customer
5678	YYY	Insurance
3456	ZZZZ Health
;

/* Create Macro Variables */
data _null_ ;
	/* Read meta dataset */
	set meta ;
	/* create macro variable called table&amp;lt;n&amp;gt; assign it the value from the data_name variable */
	call symput("table"||left(putn(_n_,"8.")),data_name) ;
	/* create macro variable tableCnt that is a count of all the observations in meta dataset */
	call symput("tableCnt",putn(_n_,"8.")) ;
run ;

/* Simple macro to print out the values of the macro variables created in step above */
%macro check() ;
	%do i=1 %to &amp;amp;tableCnt ;
		%put table&amp;amp;i = &amp;amp;&amp;amp;table&amp;amp;i ;
	%end ;
%mend ;

/* Call check macro */
%check ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 Mar 2021 16:27:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-table-name-from-main-table-and-query-that-table/m-p/722897#M224230</guid>
      <dc:creator>AMSAS</dc:creator>
      <dc:date>2021-03-02T16:27:05Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to extract table name from main table and query that table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-table-name-from-main-table-and-query-that-table/m-p/722926#M224251</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp; I've corrected the initial post.&amp;nbsp;&lt;SPAN&gt;reporting_purpose is not required and I've removed it in my query and also in the data&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Mar 2021 17:34:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-table-name-from-main-table-and-query-that-table/m-p/722926#M224251</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2021-03-02T17:34:39Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to extract table name from main table and query that table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-table-name-from-main-table-and-query-that-table/m-p/722935#M224257</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/226241"&gt;@AMSAS&lt;/a&gt;&amp;nbsp;&lt;SPAN&gt;Thank you for the program, but it's not producing the desired results.&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;I've corrected the initial post to have better view. I want the value of macro variable to hold data of data_name.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Mar 2021 17:50:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-table-name-from-main-table-and-query-that-table/m-p/722935#M224257</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2021-03-02T17:50:23Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to extract table name from main table and query that table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-table-name-from-main-table-and-query-that-table/m-p/722938#M224258</link>
      <description>&lt;P&gt;Supply the data in usable form. I'm tired of writing datalines steps for others who seem to be too lazy for that.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Mar 2021 18:02:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-table-name-from-main-table-and-query-that-table/m-p/722938#M224258</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-03-02T18:02:06Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to extract table name from main table and query that table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-table-name-from-main-table-and-query-that-table/m-p/722941#M224259</link>
      <description>I'm happy to receive untested code. I just looking for logic&lt;BR /&gt;</description>
      <pubDate>Tue, 02 Mar 2021 18:36:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-table-name-from-main-table-and-query-that-table/m-p/722941#M224259</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2021-03-02T18:36:47Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to extract table name from main table and query that table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-table-name-from-main-table-and-query-that-table/m-p/722955#M224265</link>
      <description>&lt;P&gt;Here is how you can post your data in a usable form.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Meta;
  input Id Company :$40. Data_name :$32. ;
cards;
1234 XXX  Customer
5678 YYY  Insurance
3456 ZZZZ Health
;

data Insurance;
  input Id Type $ Segment $;
cards;
5678 NLG AS2
;

data Customer;
  input Id Type $ Segment $ ;
cards;
1234 NL AS1
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It is really no harder to make this than the version you posted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So for your example query looks like you want&amp;nbsp; to run these two queries.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select t1.*,t2.Company
from Insurance t1
inner join meta t2
  on t1.ID=t2.ID
  and t2.data_name ="Insurance"
;
select t1.*,t2.Company
from Customer t1
inner join meta t2
  on t1.ID=t2.ID
  and t2.data_name ="Customer"
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What do you want to do with the results?&amp;nbsp; Do you want to create two separate datasets?&amp;nbsp; Do you want to create one dataset with the results from both queries?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Perhaps you just want to put all of the little tables together and join the result with the META table?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data all;
  length dsname $41 Data_name $32 ;
  set Insurance Customer indsname=dsname;
  Data_name = scan(dsname,-1,'.');
run;

proc sql;
create table want as 
select t1.*,t2.Company
from all t1
inner join meta t2
  on t1.ID=t2.ID
  and upcase(t2.data_name) = upcase(t1.data_name)
;
quit; 
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 Mar 2021 19:31:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-table-name-from-main-table-and-query-that-table/m-p/722955#M224265</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-03-02T19:31:07Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to extract table name from main table and query that table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-table-name-from-main-table-and-query-that-table/m-p/723060#M224319</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;has provided us the datastep. I've updated the intial post with the data step code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;Thank you for the code. But&amp;nbsp;I want the value of TABLE to come from the field 'Data_name' which is available in META table.&amp;nbsp; Assume ID is the common field between the tables Insurance and Customer. Check my initial post.&lt;/P&gt;
&lt;P&gt;So I'm looking for code like this,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql;
create table want as 
select t1.*,t2.Company
from &amp;lt;macro_variable&amp;gt; t1
inner join meta t2
  on t1.ID=t2.ID 
;
quit; &lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Mar 2021 02:46:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-table-name-from-main-table-and-query-that-table/m-p/723060#M224319</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2021-03-03T02:46:50Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to extract table name from main table and query that table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-table-name-from-main-table-and-query-that-table/m-p/723086#M224332</link>
      <description>2 step answer (cannot do it in one)&lt;BR /&gt;SQL can provide both&lt;BR /&gt;&lt;BR /&gt;Select data_name into : myTable trimmed&lt;BR /&gt;   From your.metadata&lt;BR /&gt;   Where whatever&lt;BR /&gt;;&lt;BR /&gt;Select * from libname.&amp;amp;myTable &lt;BR /&gt; Where &amp;lt;other conditions&amp;gt;&lt;BR /&gt;;&lt;BR /&gt;I think you cannot get the final table/select with a join to metadata because the required table is unknown when the query is compiled. It is only known after run time &lt;BR /&gt;</description>
      <pubDate>Wed, 03 Mar 2021 07:47:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-table-name-from-main-table-and-query-that-table/m-p/723086#M224332</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2021-03-03T07:47:27Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to extract table name from main table and query that table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-table-name-from-main-table-and-query-that-table/m-p/723093#M224334</link>
      <description>&lt;P&gt;Since you want to run the query multiple times, you also need to use a variable name for the output dataset, otherwise only the result of the final query will be present.&lt;/P&gt;
&lt;P&gt;The easiest way to do this is to pack the SQL query into a CALL EXECUTE:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Meta;
  input Id Company :$40. Data_name :$32. ;
cards;
1234 XXX  Customer
5678 YYY  Insurance
3456 ZZZZ Health
;

data Insurance;
  input Id Type $ Segment $;
cards;
5678 NLG AS2
;

data Customer;
  input Id Flag $ Report $ ;
cards;
1234 Yes Test
;

data _null_;
set meta;
if exist(data_name)
then call execute("
Proc sql;
create table aa" !! data_name !! " as select t1.*,t2.Company 
from " !! data_name !! " t1 inner join 
meta t2 
on (t1.ID=t2.ID)
;
quit;
");
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Mar 2021 08:25:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-extract-table-name-from-main-table-and-query-that-table/m-p/723093#M224334</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-03-03T08:25:26Z</dc:date>
    </item>
  </channel>
</rss>

