<?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: Check if a table exist in PROC SQL in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Check-if-a-table-exist-in-PROC-SQL/m-p/297997#M60311</link>
    <description>&lt;P&gt;I also need this question answered as we set up tables on a quarterley basis that have data added to them. I would to verify if the table exists at the start of a new quarter and, if not, to create it.&lt;/P&gt;</description>
    <pubDate>Tue, 13 Sep 2016 12:37:56 GMT</pubDate>
    <dc:creator>mbsuther</dc:creator>
    <dc:date>2016-09-13T12:37:56Z</dc:date>
    <item>
      <title>Check if a table exist in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Check-if-a-table-exist-in-PROC-SQL/m-p/272982#M58418</link>
      <description>&lt;P&gt;Hello everyone,&lt;BR /&gt;In a proc sql, I would like to test the existence of a table. This is my code (which doesn't work and i have no idea how to do it) :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
	create table union as 
	if exist(table1) then (
		select id1, . as id2, number
		from table1
	)
	union
	if exist(table2) then (
		select '' as id1, id2, number
		from table2
	)
; quit ;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;I have seen on the internet something like this, but i don't know how to put it in my case :&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test(name);
%if %sysfunc(exist(&amp;amp;name)) %then
   /* union of the 2 tables */
;
%mend test;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you in advance, and sorry for my poor english &lt;span class="lia-unicode-emoji" title=":confused_face:"&gt;😕&lt;/span&gt; !&lt;/P&gt;&lt;P&gt;Have a nice afternoon !&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Alison&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 25 May 2016 12:06:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Check-if-a-table-exist-in-PROC-SQL/m-p/272982#M58418</guid>
      <dc:creator>alisondu77</dc:creator>
      <dc:date>2016-05-25T12:06:43Z</dc:date>
    </item>
    <item>
      <title>Re: Check if a table exist in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Check-if-a-table-exist-in-PROC-SQL/m-p/272984#M58419</link>
      <description>&lt;P&gt;Yes, what you have posted is not SQL at all. &amp;nbsp;SQL is a language which assumes you know what data you are dealing with and so does not have any existence checking. &amp;nbsp;Why do you not know if your datasets will exist or not, it doesn't make much sense. &amp;nbsp;At worst case scenario create an empty dataset, then append your data to that:&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table FINAL (VAR1 char(200),VAR2 num);
quit;

data final;  
  set final your_data;
run;&lt;/PRE&gt;
&lt;P&gt;You know then that the dataset will always exist even if there is no data. &amp;nbsp; Sounds to me like your process before that is not optimal hence you have this issue.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note, you could create some macro language code to do such a thing, but the question remains why, fix the source.&lt;/P&gt;
&lt;PRE&gt;%macro sql;
  proc sql;
    create table UNION as 
    %if %sysfunc(exists(table1)) %then %do;
      select ID1,. as ID2 from TABLE1
    %end;
    %if %sysfunc(exists(table1)) and %sysfunc(exists(table2)) %then %do;
      union all
    %end;
    %if %sysfunc(exists(table2)) %then %do;
      select ID1,ID2 from TABLE2
    %end;
    ;quit;
%mend sql;
%sql;&lt;/PRE&gt;
&lt;P&gt;You can see from the above what a mess that looks like.&lt;/P&gt;</description>
      <pubDate>Wed, 25 May 2016 12:15:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Check-if-a-table-exist-in-PROC-SQL/m-p/272984#M58419</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-05-25T12:15:45Z</dc:date>
    </item>
    <item>
      <title>Re: Check if a table exist in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Check-if-a-table-exist-in-PROC-SQL/m-p/273038#M58421</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro union_tables(tab_list= sashelp.class sashelp.class);
      * get number of table in list;
%let Tab_cnt=%eval(%sysfunc(countc(strip(&amp;amp;tab_list), %str( )))+1);
     * set first table flag to 0, we are setting it to 1 where table exist first time;
%let first_tab=0;
     * Proc sql begins;
proc sql;
/*Loop throught list of tables*/
%do i=1 %to &amp;amp;Tab_cnt;
     %let tab=%sysfunc(scan(&amp;amp;tab_list,&amp;amp;i,%str( )));
    %if %sysfunc(exist(&amp;amp;tab)) %then %do; /*Check if table exist*/
           %if &amp;amp;first_tab=0 %then %do; /*Check if this is first table that exist*/
                %let first_tab=1; /*Set value flag to 1*/
               create table out_tab as /*Add create tastemet for first table which exist*/
          %end;
    %else %do; union all %end; /*if this is second or other table that exists*/
    select * from &amp;amp;tab /*Select statment*/
  %end;
%end;
;
quit ;
%mend;

options mlogic symbolgen mprint;
%union_tables(tab_list= sashelp.class sashelp.class)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 25 May 2016 14:53:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Check-if-a-table-exist-in-PROC-SQL/m-p/273038#M58421</guid>
      <dc:creator>umeshMahajan</dc:creator>
      <dc:date>2016-05-25T14:53:46Z</dc:date>
    </item>
    <item>
      <title>Re: Check if a table exist in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Check-if-a-table-exist-in-PROC-SQL/m-p/273862#M58483</link>
      <description>&lt;P&gt;Thanks you for your quick answer ! That works very well, it's exactly what i want,&amp;nbsp;even if it's " a mess" like you said&amp;nbsp;! My team wanted an automatization of a script, which work even if table1 or table2 doesn't exist.&lt;/P&gt;&lt;P&gt;Thanks you !&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Have a nice day,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Alison&lt;/P&gt;</description>
      <pubDate>Mon, 30 May 2016 07:06:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Check-if-a-table-exist-in-PROC-SQL/m-p/273862#M58483</guid>
      <dc:creator>alisondu77</dc:creator>
      <dc:date>2016-05-30T07:06:44Z</dc:date>
    </item>
    <item>
      <title>Re: Check if a table exist in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Check-if-a-table-exist-in-PROC-SQL/m-p/273863#M58484</link>
      <description>&lt;P&gt;Thanks you for your answer ! In my case it doesn't fit really well because of my "select" which is different given the table, but i'm going to use it in an other work. Thanks you very much !&lt;/P&gt;&lt;P&gt;Have a nice day !&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Alison&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 May 2016 07:09:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Check-if-a-table-exist-in-PROC-SQL/m-p/273863#M58484</guid>
      <dc:creator>alisondu77</dc:creator>
      <dc:date>2016-05-30T07:09:27Z</dc:date>
    </item>
    <item>
      <title>Re: Check if a table exist in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Check-if-a-table-exist-in-PROC-SQL/m-p/297997#M60311</link>
      <description>&lt;P&gt;I also need this question answered as we set up tables on a quarterley basis that have data added to them. I would to verify if the table exists at the start of a new quarter and, if not, to create it.&lt;/P&gt;</description>
      <pubDate>Tue, 13 Sep 2016 12:37:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Check-if-a-table-exist-in-PROC-SQL/m-p/297997#M60311</guid>
      <dc:creator>mbsuther</dc:creator>
      <dc:date>2016-09-13T12:37:56Z</dc:date>
    </item>
    <item>
      <title>Re: Check if a table exist in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Check-if-a-table-exist-in-PROC-SQL/m-p/298004#M60312</link>
      <description>&lt;P&gt;This question was answered, 4 months ago. &amp;nbsp;If your problem is different, start a new topic, provide full information as to the problem (i.e. where the data is), example data/output if useful. &amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;Personally I avoid using this scenario where "data" - in this case quarters, is used in names of datasets and one of the resons behind that is to avoid this whole need to know issues. &amp;nbsp;If you have a master data table, and update that, and it has quarter information, it is simple to code with, and you can pull out any required information.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Sep 2016 12:44:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Check-if-a-table-exist-in-PROC-SQL/m-p/298004#M60312</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-09-13T12:44:26Z</dc:date>
    </item>
  </channel>
</rss>

