<?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 How to insert a table name into a column in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-insert-a-table-name-into-a-column/m-p/558265#M155795</link>
    <description>&lt;P&gt;I have the following table from which i get the values of the table which i have inserted to monitor.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
   CREATE TABLE LIST_OF_TABLES (TABLE_NAME_COLUMN CHAR(20));

   INSERT INTO LIST_OF_TABLES
   			VALUES("EXAMPLE_TABLE1")
			VALUES("EXAMPLE_TABLE2")
			VALUES("EXAMPLE_TABLE3");

	SELECT LEFT(PUT(COUNT(TABLE_NAME_COLUMN),4.0)) INTO :TABLE_COUNT FROM LIST_OF_TABLES;
	SELECT TABLE_NAME_COLUMN INTO :TABLE_NAME1 - :TABLE_NAME&amp;amp;TABLE_COUNT FROM LIST_OF_TABLES;

	
	create table TEMP (last_date_of_month num informat=date9. format=date9.,
					  date num informat= date9. format= date9.,
					  status char(20),
					  table_name char(20));
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;OL&gt;&lt;LI&gt;Here i am iterating the table name from macro.&lt;/LI&gt;&lt;/OL&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO MONITORING;
	
	%DO I=1 %TO &amp;amp;TABLE_COUNT;
		
		PROC SQL;

		 insert into WORK.TEMP (last_date_of_month,date,status)  
		 
		 /*I need to insert table name in a column for each record.
		I have a variable &amp;amp;&amp;amp;TABLE_NAME&amp;amp;I in which i have table name and i am iterating and getting each table name and preparing work.temp data set*/
			
			SELECT distinct DT.last_of_month format=Date9.,
					DWT.date format=Date9.,
					case
        			when date is NULL then '0'
					when date is NOT NULL then '1'
					end as status
					
			FROM DATES DT
			left join WORK.&amp;amp;&amp;amp;TABLE_NAME&amp;amp;I as DWT
			on DT.last_of_month = DWT.date;

		run;
		QUIT;


	%END;
	RUN;
%MEND;

%MONITORING;&lt;/CODE&gt;&lt;/PRE&gt;&lt;OL&gt;&lt;LI&gt;After inserting the table name in to the "LIST_OF_TABLES" i am iterating the table name and inserting it into work.temp table.&lt;/LI&gt;&lt;LI&gt;In the temp table i have three columns "last_date_of_month, date,&amp;nbsp;status " and i need one more column which is "table-name".&lt;/LI&gt;&lt;LI&gt;Here is the full code.&lt;/LI&gt;&lt;/OL&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let start_date = %sysfunc(intnx(week,%sysfunc(today()),-2,e),date9.);
%let end_date = %sysfunc(intnx(week,%sysfunc(today()),0,e),date9.);

%let days = intck('day',"&amp;amp;start_date."d,"&amp;amp;end_date."d);

 data dates;
 do d = 0 to &amp;amp;days.;
 first_of_month = intnx('DAY',"&amp;amp;start_date."d,d,'s');
 last_of_month = intnx('DAY',"&amp;amp;start_date."d,d,'e');
 output;
 end;
 run;
proc sql;
   CREATE TABLE LIST_OF_TABLES (TABLE_NAME_COLUMN CHAR(20));

   INSERT INTO LIST_OF_TABLES
   			VALUES("EXAMPLE_TABLE1")
			VALUES("EXAMPLE_TABLE2")
			VALUES("EXAMPLE_TABLE3");

	SELECT LEFT(PUT(COUNT(TABLE_NAME_COLUMN),4.0)) INTO :TABLE_COUNT FROM LIST_OF_TABLES;
	SELECT TABLE_NAME_COLUMN INTO :TABLE_NAME1 - :TABLE_NAME&amp;amp;TABLE_COUNT FROM LIST_OF_TABLES;

	
	create table TEMP (last_date_of_month num informat=date9. format=date9.,
					  date num informat= date9. format= date9.,
					  status char(20),
					  table_name char(20));
quit;


%MACRO MONITORING;
	
	%DO I=1 %TO &amp;amp;TABLE_COUNT;
		
		PROC SQL;

		 insert into WORK.TEMP (last_date_of_month,date,status)  
		 
		 /*I need to insert table name in a column for each record.
		I have a variable &amp;amp;&amp;amp;TABLE_NAME&amp;amp;I in which i have table name and i am iterating and getting each table name and preparing work.temp data set*/
			
			SELECT distinct DT.last_of_month format=Date9.,
					DWT.date format=Date9.,
					case
        			when date is NULL then '0'
					when date is NOT NULL then '1'
					end as status
					
			FROM DATES DT
			left join WORK.&amp;amp;&amp;amp;TABLE_NAME&amp;amp;I as DWT
			on DT.last_of_month = DWT.date;

		run;
		QUIT;


	%END;
	RUN;
%MEND;

%MONITORING;



&amp;nbsp;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Can anyone share the inputs.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 13 May 2019 11:45:45 GMT</pubDate>
    <dc:creator>priya_05</dc:creator>
    <dc:date>2019-05-13T11:45:45Z</dc:date>
    <item>
      <title>How to insert a table name into a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-insert-a-table-name-into-a-column/m-p/558265#M155795</link>
      <description>&lt;P&gt;I have the following table from which i get the values of the table which i have inserted to monitor.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
   CREATE TABLE LIST_OF_TABLES (TABLE_NAME_COLUMN CHAR(20));

   INSERT INTO LIST_OF_TABLES
   			VALUES("EXAMPLE_TABLE1")
			VALUES("EXAMPLE_TABLE2")
			VALUES("EXAMPLE_TABLE3");

	SELECT LEFT(PUT(COUNT(TABLE_NAME_COLUMN),4.0)) INTO :TABLE_COUNT FROM LIST_OF_TABLES;
	SELECT TABLE_NAME_COLUMN INTO :TABLE_NAME1 - :TABLE_NAME&amp;amp;TABLE_COUNT FROM LIST_OF_TABLES;

	
	create table TEMP (last_date_of_month num informat=date9. format=date9.,
					  date num informat= date9. format= date9.,
					  status char(20),
					  table_name char(20));
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;OL&gt;&lt;LI&gt;Here i am iterating the table name from macro.&lt;/LI&gt;&lt;/OL&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO MONITORING;
	
	%DO I=1 %TO &amp;amp;TABLE_COUNT;
		
		PROC SQL;

		 insert into WORK.TEMP (last_date_of_month,date,status)  
		 
		 /*I need to insert table name in a column for each record.
		I have a variable &amp;amp;&amp;amp;TABLE_NAME&amp;amp;I in which i have table name and i am iterating and getting each table name and preparing work.temp data set*/
			
			SELECT distinct DT.last_of_month format=Date9.,
					DWT.date format=Date9.,
					case
        			when date is NULL then '0'
					when date is NOT NULL then '1'
					end as status
					
			FROM DATES DT
			left join WORK.&amp;amp;&amp;amp;TABLE_NAME&amp;amp;I as DWT
			on DT.last_of_month = DWT.date;

		run;
		QUIT;


	%END;
	RUN;
%MEND;

%MONITORING;&lt;/CODE&gt;&lt;/PRE&gt;&lt;OL&gt;&lt;LI&gt;After inserting the table name in to the "LIST_OF_TABLES" i am iterating the table name and inserting it into work.temp table.&lt;/LI&gt;&lt;LI&gt;In the temp table i have three columns "last_date_of_month, date,&amp;nbsp;status " and i need one more column which is "table-name".&lt;/LI&gt;&lt;LI&gt;Here is the full code.&lt;/LI&gt;&lt;/OL&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let start_date = %sysfunc(intnx(week,%sysfunc(today()),-2,e),date9.);
%let end_date = %sysfunc(intnx(week,%sysfunc(today()),0,e),date9.);

%let days = intck('day',"&amp;amp;start_date."d,"&amp;amp;end_date."d);

 data dates;
 do d = 0 to &amp;amp;days.;
 first_of_month = intnx('DAY',"&amp;amp;start_date."d,d,'s');
 last_of_month = intnx('DAY',"&amp;amp;start_date."d,d,'e');
 output;
 end;
 run;
proc sql;
   CREATE TABLE LIST_OF_TABLES (TABLE_NAME_COLUMN CHAR(20));

   INSERT INTO LIST_OF_TABLES
   			VALUES("EXAMPLE_TABLE1")
			VALUES("EXAMPLE_TABLE2")
			VALUES("EXAMPLE_TABLE3");

	SELECT LEFT(PUT(COUNT(TABLE_NAME_COLUMN),4.0)) INTO :TABLE_COUNT FROM LIST_OF_TABLES;
	SELECT TABLE_NAME_COLUMN INTO :TABLE_NAME1 - :TABLE_NAME&amp;amp;TABLE_COUNT FROM LIST_OF_TABLES;

	
	create table TEMP (last_date_of_month num informat=date9. format=date9.,
					  date num informat= date9. format= date9.,
					  status char(20),
					  table_name char(20));
quit;


%MACRO MONITORING;
	
	%DO I=1 %TO &amp;amp;TABLE_COUNT;
		
		PROC SQL;

		 insert into WORK.TEMP (last_date_of_month,date,status)  
		 
		 /*I need to insert table name in a column for each record.
		I have a variable &amp;amp;&amp;amp;TABLE_NAME&amp;amp;I in which i have table name and i am iterating and getting each table name and preparing work.temp data set*/
			
			SELECT distinct DT.last_of_month format=Date9.,
					DWT.date format=Date9.,
					case
        			when date is NULL then '0'
					when date is NOT NULL then '1'
					end as status
					
			FROM DATES DT
			left join WORK.&amp;amp;&amp;amp;TABLE_NAME&amp;amp;I as DWT
			on DT.last_of_month = DWT.date;

		run;
		QUIT;


	%END;
	RUN;
%MEND;

%MONITORING;



&amp;nbsp;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Can anyone share the inputs.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 May 2019 11:45:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-insert-a-table-name-into-a-column/m-p/558265#M155795</guid>
      <dc:creator>priya_05</dc:creator>
      <dc:date>2019-05-13T11:45:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to insert a table name into a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-insert-a-table-name-into-a-column/m-p/558395#M155850</link>
      <description>&lt;P&gt;I don't understand what the question is.&lt;/P&gt;
&lt;P&gt;It looks like you have the value in a macro variable. So if you want to use that value in some SAS code, like your SQL statements then it as simple as just enclosing the reference to the macro variable in double quotes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let table_name=FRED;

insert into some_other_table (table_name,var1,var2)
select "&amp;amp;table_Name" as table_name
,a.var1
,a.var2
from &amp;amp;table_name A
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that your whole approach seems way too complex, but wasn't your question.&lt;/P&gt;</description>
      <pubDate>Mon, 13 May 2019 17:51:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-insert-a-table-name-into-a-column/m-p/558395#M155850</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-05-13T17:51:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to insert a table name into a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-insert-a-table-name-into-a-column/m-p/558412#M155855</link>
      <description>&lt;P&gt;In the code that your provided, you will need to make updates in two locations in order to have the table name of the current data being reviewed added into a column on your final table as values:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1.Insert Statement:&amp;nbsp;&lt;/P&gt;&lt;P&gt;- CURRENT LOGIC&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;insert into WORK.TEMP (last_date_of_month,date,status) &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;- UPDATED LOGIC (Add the table_name variable to the list of variables being inserted into WORK.Temp)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2. Select Statement:&lt;/P&gt;&lt;P&gt;- CURRENT LOGIC&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;SELECT distinct
       DT.last_of_month format=Date9.
     , DWT.date format=Date9.
     , (CASE WHEN date is NULL then '0'
                   WHEN date is NOT NULL then '1'
		   end)   as status&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- UPDATED LOGIC&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;SELECT distinct 
       DT.last_of_month format=Date9.
     , DWT.date format=Date9.
     , (CASE WHEN date is NULL then '0'
		   WHEN date is NOT NULL then '1'
		   END) as status
     , "&amp;amp;&amp;amp;TABLE_NAME&amp;amp;I."     AS table_name
					&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Add the last line, referencing the iteration specific table name macro as the text value to populated in the column, 'table_name'.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hopefully this helps.&lt;/P&gt;</description>
      <pubDate>Mon, 13 May 2019 18:25:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-insert-a-table-name-into-a-column/m-p/558412#M155855</guid>
      <dc:creator>tsap</dc:creator>
      <dc:date>2019-05-13T18:25:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to insert a table name into a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-insert-a-table-name-into-a-column/m-p/559241#M156141</link>
      <description>&lt;P&gt;Thanks for the Reply. It works for me .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again.&lt;/P&gt;</description>
      <pubDate>Thu, 16 May 2019 09:26:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-insert-a-table-name-into-a-column/m-p/559241#M156141</guid>
      <dc:creator>priya_05</dc:creator>
      <dc:date>2019-05-16T09:26:18Z</dc:date>
    </item>
  </channel>
</rss>

