<?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: Date macro inside proc sql in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Date-macro-inside-proc-sql/m-p/542303#M149837</link>
    <description>&lt;P&gt;How would it be better for change the code? Your option still give an error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table t2 as
	select "&amp;amp;max_dt."d as last_work_dt
	from calendar
	where dt = today()-3
;quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ERROR: Invalid date/time/datetime constant "&amp;amp;max_dt."d.&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;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 12 Mar 2019 09:34:15 GMT</pubDate>
    <dc:creator>elessar</dc:creator>
    <dc:date>2019-03-12T09:34:15Z</dc:date>
    <item>
      <title>Date macro inside proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-macro-inside-proc-sql/m-p/542286#M149830</link>
      <description>&lt;P&gt;Let we have macro which are calculating by proc sql and are defining through "into":&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data calendar;
format dt date9.;
	flg=0;	dt=today();	output;
	flg=1;	dt=today()-1;	output;
	flg=1;	dt=today()-2;	output;
	flg=0;	dt=today()-3;	output;
	flg=0;	dt=today()-4;	output;
	flg=0;	dt=today()-5;	output;
run;

proc sql;
create table t1 as
	select max(dt) as max_dt format=date9. 
	into :max_dt
	from calendar
	where dt &amp;lt; today() and flg = 0
;quit;

%put &amp;amp;max_dt;&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;But when I try to use it in select of second proc sql (this is absurd code, just for example), there is an error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table t2 as
	select &amp;amp;max_dt. as last_work_dt
	from calendar
	where dt = today()-3
;quit;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, a numeric constant, a datetime constant, 
              a missing value, BTRIM, INPUT, PUT, SUBSTRING, USER.  

ERROR 22-322: Syntax error, expecting one of the following: a name, *.  &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;How can I use this date macro inside second proc sql?&lt;/P&gt;</description>
      <pubDate>Tue, 12 Mar 2019 07:49:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-macro-inside-proc-sql/m-p/542286#M149830</guid>
      <dc:creator>elessar</dc:creator>
      <dc:date>2019-03-12T07:49:28Z</dc:date>
    </item>
    <item>
      <title>Re: Date macro inside proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-macro-inside-proc-sql/m-p/542291#M149831</link>
      <description>&lt;P&gt;The macro variable max_dt will contain a string like 12MAR2019. To use that as a date constant in SAS, use "&amp;amp;max_dt"d, so you get a date constant, like "12MAR2019"d.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Mar 2019 08:37:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-macro-inside-proc-sql/m-p/542291#M149831</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2019-03-12T08:37:57Z</dc:date>
    </item>
    <item>
      <title>Re: Date macro inside proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-macro-inside-proc-sql/m-p/542295#M149833</link>
      <description>&lt;P&gt;It highlights one of the primary reasons why you should not keep data in macro variables.&amp;nbsp; Macro is a&amp;nbsp;&lt;U&gt;&lt;STRONG&gt;text find and replace system&lt;/STRONG&gt;&lt;/U&gt;&lt;U&gt;&amp;nbsp;&lt;/U&gt;it only deals with text and the generation of text.&amp;nbsp; So when the text of your code is going through its checks it finds:&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;&lt;SPAN class="token statement"&gt;select&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;max_dt&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt; as last_work_dt&lt;/LI-CODE&gt;
&lt;P&gt;&amp;amp;max_dt. resolves to&amp;nbsp;12MAR2019, so your code line looks like;&lt;/P&gt;
&lt;PRE&gt;select 12MAR2019 as last_work_dt&lt;/PRE&gt;
&lt;P&gt;Which you can see is incorrect code.&amp;nbsp; The above find/replace debugging works on all macro code, as the above is all that macro code does.&amp;nbsp; Datastep processing of datasets has all the different types, collections, functions etc.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Mar 2019 08:48:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-macro-inside-proc-sql/m-p/542295#M149833</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2019-03-12T08:48:56Z</dc:date>
    </item>
    <item>
      <title>Re: Date macro inside proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-macro-inside-proc-sql/m-p/542303#M149837</link>
      <description>&lt;P&gt;How would it be better for change the code? Your option still give an error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table t2 as
	select "&amp;amp;max_dt."d as last_work_dt
	from calendar
	where dt = today()-3
;quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ERROR: Invalid date/time/datetime constant "&amp;amp;max_dt."d.&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;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Mar 2019 09:34:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-macro-inside-proc-sql/m-p/542303#M149837</guid>
      <dc:creator>elessar</dc:creator>
      <dc:date>2019-03-12T09:34:15Z</dc:date>
    </item>
    <item>
      <title>Re: Date macro inside proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-macro-inside-proc-sql/m-p/542305#M149839</link>
      <description>&lt;P&gt;Please post the &lt;EM&gt;whole&lt;/EM&gt; log of the step. I guess you also get a message&lt;/P&gt;
&lt;PRE&gt;WARNING: Apparent symbolic reference MAX_DT not resolved.&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Mar 2019 09:42:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-macro-inside-proc-sql/m-p/542305#M149839</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-03-12T09:42:05Z</dc:date>
    </item>
    <item>
      <title>Re: Date macro inside proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-macro-inside-proc-sql/m-p/542306#M149840</link>
      <description>&lt;P&gt;But I can do that this way:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let random_dt = today();

proc sql;
create table t3 as
	select &amp;amp;random_dt. format=date9. as last_work_dt
	from calendar
	where dt = today()-3
;quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;or this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let random_dt = %unquote('12mar2019'd);

proc sql;
create table t3 as
	select &amp;amp;random_dt. format=date9. as last_work_dt
	from calendar
	where dt = today()-3
;quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;and both options work.&amp;nbsp;What the difference between that and macro's definition through "into"?&lt;/P&gt;</description>
      <pubDate>Tue, 12 Mar 2019 09:44:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-macro-inside-proc-sql/m-p/542306#M149840</guid>
      <dc:creator>elessar</dc:creator>
      <dc:date>2019-03-12T09:44:56Z</dc:date>
    </item>
    <item>
      <title>Re: Date macro inside proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-macro-inside-proc-sql/m-p/542311#M149842</link>
      <description>&lt;P&gt;There is a magical thing called the %put macro statement, and you should use it for debbugging:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let random_dt = %unquote('12mar2019'd);

%put &amp;amp;random_dt.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;log:&lt;/P&gt;
&lt;PRE&gt;24         %let random_dt = %unquote('12mar2019'd);
25         
26         %put &amp;amp;random_dt.;
'12mar2019'd
&lt;/PRE&gt;
&lt;P&gt;You can see that the %unquote does NOT remove the single quotes, and the date literal stays a valid date literal.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your first code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let random_dt = today();&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;assigns the TEXT(!)&lt;/P&gt;
&lt;PRE&gt;today()&lt;/PRE&gt;
&lt;P&gt;(not a value!) to the macro variable, and so that function call is inserted wherever you use the macro variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt; already said, the macro preprocessor is just an advanced text replacement engine, and you have to "get" that before you'll be able to successfully use it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In your initial &lt;FONT face="courier new,courier"&gt;select into&lt;/FONT&gt;, you made the mistake of formatting the date value with date9, which is unnecessary and impedes further use of the macro variable by requring to enclose it in ""d. See Maxim 28.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Mar 2019 10:00:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-macro-inside-proc-sql/m-p/542311#M149842</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-03-12T10:00:34Z</dc:date>
    </item>
    <item>
      <title>Re: Date macro inside proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-macro-inside-proc-sql/m-p/542315#M149844</link>
      <description>&lt;P&gt;Step 1 to learning Macro, turn on the options:&lt;/P&gt;
&lt;PRE&gt;options mlogic mprint sybolgen source source2;&lt;/PRE&gt;
&lt;P&gt;You can turn it back off with:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;options nomlogic nomprint nosybolgen nosource nosource2;&lt;/PRE&gt;
&lt;P&gt;These options present to the log more information about the process which is happening within the macro pre-processor, you can see what macro variables contain, what logic is executed.&amp;nbsp; If you review the variables and what they contain, and then do the find/replace yourself, you will see what is valid code and what is not.&amp;nbsp; &amp;nbsp;Remember, macro is&amp;nbsp;&lt;U&gt;&lt;STRONG&gt;nothing more than a find/replace system&lt;/STRONG&gt;&lt;/U&gt;&lt;U&gt;.&lt;/U&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Mar 2019 10:11:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-macro-inside-proc-sql/m-p/542315#M149844</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2019-03-12T10:11:56Z</dc:date>
    </item>
    <item>
      <title>Re: Date macro inside proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-macro-inside-proc-sql/m-p/542316#M149845</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/238185"&gt;@elessar&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;But I can do that this way:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let random_dt = today();

proc sql;
create table t3 as
	select &amp;amp;random_dt. format=date9. as last_work_dt
	from calendar
	where dt = today()-3
;quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;or this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let random_dt = %unquote('12mar2019'd);

proc sql;
create table t3 as
	select &amp;amp;random_dt. format=date9. as last_work_dt
	from calendar
	where dt = today()-3
;quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and both options work.&amp;nbsp;What the difference between that and macro's definition through "into"?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;In both of these, you are not passing a formatted date into the macro variable. In both of these you are working with an un-formatted SAS date value, which PROC SQL understands. PROC SQL does not understand a formatted date value unless you enclose it in quotes with a d on the end, such as "&amp;amp;random_dt"d&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There is no need to format SAS date or datetime or time values when you put them into macro variable. This was the cause of your original problems.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, people have suggested you use certain options when you work with macros, and also the %PUT statement to see the values contained in macro variables, these are highly recommended!&lt;/P&gt;</description>
      <pubDate>Tue, 12 Mar 2019 10:46:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-macro-inside-proc-sql/m-p/542316#M149845</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-03-12T10:46:09Z</dc:date>
    </item>
  </channel>
</rss>

