<?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: Conditionally execute Proc sql “delete from” and “insert into” in a macro using %if %then %else in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-execute-Proc-sql-delete-from-and-insert-into-in-a/m-p/651473#M195472</link>
    <description>I would suggest you look into partitioning in the database.   That way you can drop old partititions almost instantly, and the database table is not locked whilst adding thousands of records.</description>
    <pubDate>Thu, 28 May 2020 16:43:00 GMT</pubDate>
    <dc:creator>DavePrinsloo</dc:creator>
    <dc:date>2020-05-28T16:43:00Z</dc:date>
    <item>
      <title>Conditionally execute Proc sql “delete from” and “insert into” in a macro using %if %then %else</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-execute-Proc-sql-delete-from-and-insert-into-in-a/m-p/651467#M195469</link>
      <description>&lt;P&gt;I wonder what would be a correct way to use %if %then %else to conditionally execute proc sql command within a macro?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Background&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;I Need to delete all rows from an existing (master) table if data is already more than several months old, or this table already contains current month’s data (in the case of a re-run)&lt;/LI&gt;&lt;LI&gt;Append new month’s data to the existing (master) table&lt;/LI&gt;&lt;LI&gt;These existing (master) tables are in a DB on MS Sql server, where the new month’s data are SAS tables generated from a SAS process.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;The below codes work fine. Be noted I used macro because I have more than one table, so I will add positional parameters later.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;%macro&lt;/STRONG&gt; &lt;STRONG&gt;&lt;EM&gt;lt1&lt;/EM&gt;&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;PROC SQL;&lt;/P&gt;&lt;P&gt;DELETE&lt;/P&gt;&lt;P&gt;FROM SQL.TABLE /* MASTER TABLE*/&lt;/P&gt;&lt;P&gt;WHERE METRIC_DATE = &amp;amp;SASDATEB OR METRIC_DATE LE &amp;amp;SASDATE12;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;INSERT INTO SQL.TABLE /* MASTER TABLE*/&lt;/P&gt;&lt;P&gt;SELECT * FROM SAS.TABLE; /*NEW TABLE*/&lt;/P&gt;&lt;P&gt;QUIT;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;%MEND&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%&lt;STRONG&gt;&lt;EM&gt;lt1&lt;/EM&gt;&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;SYMBOLGEN:&amp;nbsp; Macro variable SASDATEB resolves to '01APR2020'D&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;SYMBOLGEN:&amp;nbsp; Macro variable SASDATE12 resolves to '01NOV2019'D&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;NOTE: 371655 rows were deleted from SQL.TABLE.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;NOTE: 200 rows were inserted into SQL.TABLE.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, I wonder if there is a way to use %if %then %else to execute these command? &amp;nbsp;&lt;/P&gt;&lt;P&gt;I have been thinking something like below, but obviously it did not work because it just deleted all rows from the master table.&lt;/P&gt;&lt;P&gt;Would be great if anyone could give me some advices.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;%MACRO&lt;/STRONG&gt; &lt;STRONG&gt;&lt;EM&gt;LT&lt;/EM&gt;&lt;/STRONG&gt; ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%IF METRIC_DATE EQ &amp;amp;SASDATEB OR %SYSEVALF(METRIC_DATE LT &amp;amp;SASDATE12) %THEN %DO;&lt;/P&gt;&lt;P&gt;PROC SQL;&lt;/P&gt;&lt;P&gt;DELETE FROM SQL.TABLE; /* MASTER TABLE*/&lt;/P&gt;&lt;P&gt;%END;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%ELSE %DO;&lt;/P&gt;&lt;P&gt;INSERT INTO SQL.TABLE/* MASTER TABLE*/&lt;/P&gt;&lt;P&gt;SELECT * FROM SAS.TABLE; /*NEW TABLE*/&lt;/P&gt;&lt;P&gt;%END;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;QUIT;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;%MEND&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;NOTE: 371855 rows were deleted from ME.SW_EOL_CSI_ACI_T1M.&lt;/P&gt;</description>
      <pubDate>Thu, 28 May 2020 16:36:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-execute-Proc-sql-delete-from-and-insert-into-in-a/m-p/651467#M195469</guid>
      <dc:creator>LL5</dc:creator>
      <dc:date>2020-05-28T16:36:17Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally execute Proc sql “delete from” and “insert into” in a macro using %if %then %else</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-execute-Proc-sql-delete-from-and-insert-into-in-a/m-p/651473#M195472</link>
      <description>I would suggest you look into partitioning in the database.   That way you can drop old partititions almost instantly, and the database table is not locked whilst adding thousands of records.</description>
      <pubDate>Thu, 28 May 2020 16:43:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-execute-Proc-sql-delete-from-and-insert-into-in-a/m-p/651473#M195472</guid>
      <dc:creator>DavePrinsloo</dc:creator>
      <dc:date>2020-05-28T16:43:00Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally execute Proc sql “delete from” and “insert into” in a macro using %if %then %else</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-execute-Proc-sql-delete-from-and-insert-into-in-a/m-p/651474#M195473</link>
      <description>&lt;P&gt;Yes something like the second would work &lt;STRONG&gt;if&lt;/STRONG&gt; the value of Metric_date is a macro variable. The macro processor can't use data set variables. &lt;/P&gt;
&lt;P&gt;I would suggest one of two changes depending on where those macro variables for the date comparisons come from.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you are supplying them as an option manually, or are calculated in a previous step , place them as Parameters to the macro&lt;/P&gt;
&lt;P&gt;or calculate them inside the macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are few things harder to debug then macro values that just "appear" in the middle of code with out a known source. Someone may attempt to modify such a macro for another purpose but may not have those macro variables available, or even worse, have the macro variable but with a different value range then expected.&lt;/P&gt;</description>
      <pubDate>Thu, 28 May 2020 16:49:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-execute-Proc-sql-delete-from-and-insert-into-in-a/m-p/651474#M195473</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-05-28T16:49:55Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally execute Proc sql “delete from” and “insert into” in a macro using %if %then %else</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-execute-Proc-sql-delete-from-and-insert-into-in-a/m-p/651481#M195476</link>
      <description>&lt;P&gt;What is it that you want to TEST in your %IF statement?&amp;nbsp; So you posted this line:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%IF METRIC_DATE EQ &amp;amp;SASDATEB OR %SYSEVALF(METRIC_DATE LT &amp;amp;SASDATE12) %THEN %DO;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So if the macro variable SASDATE12 has a value like&amp;nbsp;&lt;SPAN&gt;'01NOV2019'D&amp;nbsp; then you are testing if the character M is less than the character single quote, which is always going to be true.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;So what date is it that you want to compare the value '01NOV2019'D ?&amp;nbsp; Where does that date come from?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 28 May 2020 17:06:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-execute-Proc-sql-delete-from-and-insert-into-in-a/m-p/651481#M195476</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-05-28T17:06:40Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally execute Proc sql “delete from” and “insert into” in a macro using %if %then %else</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-execute-Proc-sql-delete-from-and-insert-into-in-a/m-p/651491#M195480</link>
      <description>&lt;P&gt;Thanks Ballardw. I got the idea, so I added one step inside the macro but got the error saying statment as not valid.&amp;nbsp; See below -&amp;nbsp;&lt;/P&gt;&lt;P&gt;I agreed with you that this method is not ideally, but I want to explore a little bit more. Thanks for your advices.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;%MACRO&lt;/STRONG&gt; &lt;STRONG&gt;&lt;EM&gt;LT&lt;/EM&gt;&lt;/STRONG&gt; ;&lt;/P&gt;&lt;P&gt;PROC SQL;&lt;BR /&gt;SELECT MAX(METRIC_DATE) INTO: D&lt;BR /&gt;FROM SQL.TABLE;&lt;BR /&gt;QUIT;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%IF METRIC_DATE EQ &amp;amp;SASDATEB OR %SYSEVALF(METRIC_DATE LT &amp;amp;SASDATE12) %THEN %DO;&lt;/P&gt;&lt;P&gt;PROC SQL;&lt;/P&gt;&lt;P&gt;DELETE FROM SQL.TABLE; /* MASTER TABLE*/&lt;/P&gt;&lt;P&gt;%END;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%ELSE %DO;&lt;/P&gt;&lt;P&gt;INSERT INTO SQL.TABLE/* MASTER TABLE*/&lt;/P&gt;&lt;P&gt;SELECT * FROM SAS.TABLE; /*NEW TABLE*/&lt;/P&gt;&lt;P&gt;%END;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;QUIT;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;%MEND&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;45 INSERT INTO SQL.TABLE SELECT * FROM SAS.TABLE;&lt;BR /&gt;______&lt;BR /&gt;180&lt;/P&gt;&lt;P&gt;ERROR 180-322: Statement is not valid or it is used out of proper order.&lt;/P&gt;</description>
      <pubDate>Thu, 28 May 2020 17:56:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-execute-Proc-sql-delete-from-and-insert-into-in-a/m-p/651491#M195480</guid>
      <dc:creator>LL5</dc:creator>
      <dc:date>2020-05-28T17:56:54Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally execute Proc sql “delete from” and “insert into” in a macro using %if %then %else</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-execute-Proc-sql-delete-from-and-insert-into-in-a/m-p/651496#M195481</link>
      <description>&lt;P&gt;Thanks Dave for this idea. I learned p&lt;SPAN style="font-family: inherit;"&gt;artitioning is the database process where very large tables are divided into multiple smaller parts. &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: inherit;"&gt;I wonder how would I drop old partitions instantly, would you mind to give me an example?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 28 May 2020 17:59:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-execute-Proc-sql-delete-from-and-insert-into-in-a/m-p/651496#M195481</guid>
      <dc:creator>LL5</dc:creator>
      <dc:date>2020-05-28T17:59:40Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally execute Proc sql “delete from” and “insert into” in a macro using %if %then %else</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-execute-Proc-sql-delete-from-and-insert-into-in-a/m-p/651507#M195482</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/89004"&gt;@LL5&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks Ballardw. I got the idea, so I added one step inside the macro but got the error saying statment as not valid.&amp;nbsp; See below -&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I agreed with you that this method is not ideally, but I want to explore a little bit more. Thanks for your advices.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;%MACRO&lt;/STRONG&gt; &lt;STRONG&gt;&lt;EM&gt;LT&lt;/EM&gt;&lt;/STRONG&gt; ;&lt;/P&gt;
&lt;P&gt;PROC SQL;&lt;BR /&gt;SELECT MAX(METRIC_DATE) INTO: D&lt;BR /&gt;FROM SQL.TABLE;&lt;BR /&gt;QUIT;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%IF METRIC_DATE EQ &amp;amp;SASDATEB OR %SYSEVALF(METRIC_DATE LT &amp;amp;SASDATE12) %THEN %DO;&lt;/P&gt;
&lt;P&gt;PROC SQL;&lt;/P&gt;
&lt;P&gt;DELETE FROM SQL.TABLE; /* MASTER TABLE*/&lt;/P&gt;
&lt;P&gt;%END;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%ELSE %DO;&lt;/P&gt;
&lt;P&gt;INSERT INTO SQL.TABLE/* MASTER TABLE*/&lt;/P&gt;
&lt;P&gt;SELECT * FROM SAS.TABLE; /*NEW TABLE*/&lt;/P&gt;
&lt;P&gt;%END;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;QUIT;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;%MEND&lt;/STRONG&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;45 INSERT INTO SQL.TABLE SELECT * FROM SAS.TABLE;&lt;BR /&gt;______&lt;BR /&gt;180&lt;/P&gt;
&lt;P&gt;ERROR 180-322: Statement is not valid or it is used out of proper order.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If you want to conditionally modify statements within a procedure then the Procedure start has to come before the condtion:&lt;/P&gt;
&lt;P&gt;something like:&lt;/P&gt;
&lt;PRE&gt;PROC SQL;
   %IF &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;METRIC_DATE&lt;/STRONG&gt;&lt;/FONT&gt; EQ &amp;amp;SASDATEB OR %SYSEVALF(&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;METRIC_DATE&lt;/STRONG&gt;&lt;/FONT&gt; LT &amp;amp;SASDATE12) %THEN %DO;
      DELETE FROM SQL.TABLE; /* MASTER TABLE*/
   %END;
 
   %ELSE %DO;
      INSERT INTO SQL.TABLE/* MASTER TABLE*/
      SELECT * FROM SAS.TABLE; /*NEW TABLE*/
   %END;
Quit;&lt;/PRE&gt;
&lt;P&gt;Your code did not have the Proc SQL; start with the second condition. Your posted code still does not use the correct comparison though. The red highlighted text is going to be compared not anything that resembles a date value.&lt;/P&gt;
&lt;P&gt;OR wrap entire Proc steps inside %do/%end blocks:&lt;/P&gt;
&lt;PRE&gt;%IF METRIC_DATE EQ &amp;amp;SASDATEB OR %SYSEVALF(METRIC_DATE LT &amp;amp;SASDATE12) %THEN %DO;
   PROC SQL;
      DELETE FROM SQL.TABLE; /* MASTER TABLE*/
   quit;
%END;

%ELSE %DO;
   Proc sql;
      INSERT INTO SQL.TABLE/* MASTER TABLE*/
      SELECT * FROM SAS.TABLE; /*NEW TABLE*/
   Quit;
%END;&lt;/PRE&gt;
&lt;P&gt;Please post code and log entries into a code box opened on the forum with the &amp;lt;/&amp;gt; icon to preserve formatting. For Log error messages copy the entire proc step with errors from the log and paste into the code box. For debugging macros you will want to set OPTIONS MPRINT; to see the code actually generated by your macro. You may need SYMBOLGEN and MLOGIC options as well if the problem seems to be with the values of constructed variables or logic evaluation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I notice that you create a macro variable D but don't use that in the code you have shared.&lt;/P&gt;</description>
      <pubDate>Fri, 29 May 2020 15:09:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-execute-Proc-sql-delete-from-and-insert-into-in-a/m-p/651507#M195482</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-05-29T15:09:46Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally execute Proc sql “delete from” and “insert into” in a macro using %if %then %else</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-execute-Proc-sql-delete-from-and-insert-into-in-a/m-p/651512#M195483</link>
      <description>&lt;P&gt;You are still comparing the letters MET... to the value of the macro variable.&amp;nbsp; Did you mean to compare to the value of the macro variable D instead?&amp;nbsp; If you are going to compare date literals you need to use %SYSEVALF(). Normally the macro processor will just use %EVAL() to evaluate the conditions and that does not understand date literals, they look like strings to %EVAL.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%IF %sysevalf( (&amp;amp;d EQ &amp;amp;SASDATEB) OR (&amp;amp;d LT &amp;amp;SASDATE12)) %THEN %DO;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Do you really want to delete ALL of the observations when the %IF condition is true?&amp;nbsp; Before you moved into macro code you had a WHERE condition on the DELETE statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You cannot run the INSERT statement outside of a PROC SQL step.&lt;/P&gt;</description>
      <pubDate>Thu, 28 May 2020 18:46:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-execute-Proc-sql-delete-from-and-insert-into-in-a/m-p/651512#M195483</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-05-28T18:46:16Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally execute Proc sql “delete from” and “insert into” in a macro using %if %then %else</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-execute-Proc-sql-delete-from-and-insert-into-in-a/m-p/651765#M195574</link>
      <description>&lt;P&gt;Thanks Tom and Ballardw. This works now- see below codes and log (still haven't figure out how to put them in a code box?).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I realized I had two problems: 1) I should use %sysevalf to evaulate and compare date literals. 2) Since my current dataset has all the rows that meet the first %if condtion, so everything are being deleted (and this is the desired result). In this case, the %else never happen. In order to fix this, I put the insert command right after the delete command without the %else. According to the log, i got the expected result.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again for all the advics and suggestions.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%MACRO LT;&lt;BR /&gt;PROC SQL;&lt;BR /&gt;SELECT MAX(METRIC_DATE) INTO: D&lt;BR /&gt;FROM SQL.TABLE;&lt;BR /&gt;QUIT;&lt;/P&gt;&lt;P&gt;PROC SQL;&lt;BR /&gt;%IF %sysevalf( (&amp;amp;D EQ &amp;amp;SASDATEB) OR (&amp;amp;D LT &amp;amp;SASDATE12)) %THEN %DO;&lt;BR /&gt;DELETE FROM SQL.TABLE;&lt;BR /&gt;INSERT INTO SQL.TABLE /* MASTER TABLE*/&lt;BR /&gt;SELECT * FROM SAS.TABLE; /*NEW TABLE*/&lt;BR /&gt;%END;&lt;BR /&gt;QUIT;&lt;BR /&gt;%MEND;&lt;/P&gt;&lt;P&gt;%LT ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;NOTE: 371655 rows were deleted from SQL.TABLE.&lt;/P&gt;&lt;P&gt;NOTE: 200 rows were inserted into SQL.TABLE.&lt;/P&gt;&lt;P&gt;NOTE: PROCEDURE SQL used (Total process time):&lt;BR /&gt;real time 0.54 seconds&lt;BR /&gt;cpu time 0.32 seconds&lt;/P&gt;</description>
      <pubDate>Fri, 29 May 2020 15:33:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-execute-Proc-sql-delete-from-and-insert-into-in-a/m-p/651765#M195574</guid>
      <dc:creator>LL5</dc:creator>
      <dc:date>2020-05-29T15:33:55Z</dc:date>
    </item>
  </channel>
</rss>

