<?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: Let macro variable be max date in dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Let-macro-variable-be-max-date-in-dataset/m-p/890434#M351847</link>
    <description>&lt;P&gt;Remember that macro variables are about code substitution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you run:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mytable ;
  payout_dt='21Aug2023'd ;
run ;

proc sql;
select max(a.payout_dt) format=date9.
	into :max_dt
	from mytable a;
quit;

%put &amp;amp;=max_dt ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You'll see that the macro variable MAX_DT has the value:&amp;nbsp;21AUG2023&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table report as 
	select *
	from my.database a
	where a.snap_dt&amp;lt;=&amp;amp;max_dt;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It is the same as coding:&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;proc sql;
create table report as 
	select *
	from my.database a
	where a.snap_dt&amp;lt;=21Aug2023;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which will error, because 21Aug2023 is not&amp;nbsp; valid syntax.&amp;nbsp; It's not the name of a variable, and it's not a date value.&amp;nbsp; If you want to a date value, you can use a SAS date literal value:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table report as 
	select *
	from my.database a
	where a.snap_dt&amp;lt;="21Aug2023"d;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So with a macro variable, you can code it like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table report as 
	select *
	from my.database a
	where a.snap_dt&amp;lt;="&amp;amp;max_dt"d;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note this is only needed because you specified that the value in the macro variable should be formatted as date9.&amp;nbsp; If you remove the format option in the&amp;nbsp; PROC SQL step that creates the macro variable, your code would look like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select max(a.payout_dt)
	into :max_dt
	from mytable a;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In that case, your macro variable MAX_DT would have the value 23243.&amp;nbsp; And that value would work in your original code because 23243 is a valid date value.&amp;nbsp; Meaning, you could code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table report as 
	select *
	from my.database a
	where a.snap_dt&amp;lt;=23243;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 22 Aug 2023 16:34:30 GMT</pubDate>
    <dc:creator>Quentin</dc:creator>
    <dc:date>2023-08-22T16:34:30Z</dc:date>
    <item>
      <title>Let macro variable be max date in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Let-macro-variable-be-max-date-in-dataset/m-p/890427#M351841</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I bumped into this post&amp;nbsp;&lt;A title="% Let macro variable be max date in dataset" href="https://communities.sas.com/t5/SAS-Programming/Let-macro-variable-be-max-date-in-dataset/td-p/371356" target="_self"&gt;% Let macro variable be max date in dataset.&amp;nbsp;&lt;/A&gt;&lt;/P&gt;&lt;P&gt;I followed the solution but when I created and called the macro, I'm getting this error message:&lt;/P&gt;&lt;PRE&gt;35         21AUG2023
             _______
             22
             76
&lt;FONT color="#FF0000"&gt;ERROR 22-322: Syntax error, expecting one of the following: !, !!, &amp;amp;, *, **, +, -, /, &amp;lt;, &amp;lt;=, &amp;lt;&amp;gt;, =, &amp;gt;, &amp;gt;=, AND, EQ, EQT, GE, GET, 
              GROUP, GT, GTT, HAVING, LE, LET, LT, LTT, NE, NET, OR, ORDER, ^=, |, ||, ~=.  

ERROR 76-322: Syntax error, statement will be ignored.&lt;/FONT&gt;&lt;/PRE&gt;&lt;P&gt;This is my code :&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc sql;
select max(a.payout_dt) format=date9.
	into :max_dt
	from my.table a;
quit;

proc sql;
create table report as 
	select *
	from my.database a
	where a.snap_dt&amp;lt;=&amp;amp;max_dt;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Appreciate any feedback that can help me troubleshoot this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&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;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Aug 2023 16:13:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Let-macro-variable-be-max-date-in-dataset/m-p/890427#M351841</guid>
      <dc:creator>couthelle</dc:creator>
      <dc:date>2023-08-22T16:13:43Z</dc:date>
    </item>
    <item>
      <title>Re: Let macro variable be max date in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Let-macro-variable-be-max-date-in-dataset/m-p/890432#M351845</link>
      <description>&lt;P&gt;You generated this code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where a.snap_dt&amp;lt;=21AUG2023&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which is clearly not valid SAS code.&lt;/P&gt;
&lt;P&gt;You need to either generate code like one of these two options&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where a.snap_dt&amp;lt;="21AUG2023"d
where a.snap_dt&amp;lt;=23243
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So you could modify the WHERE clause like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where a.snap_dt&amp;lt;="&amp;amp;max_dt"d&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or leave the where clause as it is and create the macro variable without the formatting.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select max(a.payout_dt) format=32. into :max_dt trimmed&amp;nbsp;from my.table a;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 22 Aug 2023 16:31:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Let-macro-variable-be-max-date-in-dataset/m-p/890432#M351845</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-08-22T16:31:06Z</dc:date>
    </item>
    <item>
      <title>Re: Let macro variable be max date in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Let-macro-variable-be-max-date-in-dataset/m-p/890433#M351846</link>
      <description>&lt;P&gt;When you refer to the macro variable here:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;where a.snap_dt&amp;lt;=&amp;amp;max_dt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;it resolves as you would expect:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;where a.snap_dt&amp;lt;=21AUG2023;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;However, that's not the correct syntax to refer to a date in SAS language (whether or not macro language is involved).&amp;nbsp; Just as a matter of correct SAS syntax, you would need to generate:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;where a.snap_dt&amp;lt;="21AUG2023"D;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Therefore, your macro language reference needs to add the quotes and the D :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;where a.snap_dt&amp;lt;="&amp;amp;max_dt"D;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Aug 2023 16:32:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Let-macro-variable-be-max-date-in-dataset/m-p/890433#M351846</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2023-08-22T16:32:39Z</dc:date>
    </item>
    <item>
      <title>Re: Let macro variable be max date in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Let-macro-variable-be-max-date-in-dataset/m-p/890434#M351847</link>
      <description>&lt;P&gt;Remember that macro variables are about code substitution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you run:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mytable ;
  payout_dt='21Aug2023'd ;
run ;

proc sql;
select max(a.payout_dt) format=date9.
	into :max_dt
	from mytable a;
quit;

%put &amp;amp;=max_dt ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You'll see that the macro variable MAX_DT has the value:&amp;nbsp;21AUG2023&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table report as 
	select *
	from my.database a
	where a.snap_dt&amp;lt;=&amp;amp;max_dt;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It is the same as coding:&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;proc sql;
create table report as 
	select *
	from my.database a
	where a.snap_dt&amp;lt;=21Aug2023;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which will error, because 21Aug2023 is not&amp;nbsp; valid syntax.&amp;nbsp; It's not the name of a variable, and it's not a date value.&amp;nbsp; If you want to a date value, you can use a SAS date literal value:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table report as 
	select *
	from my.database a
	where a.snap_dt&amp;lt;="21Aug2023"d;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So with a macro variable, you can code it like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table report as 
	select *
	from my.database a
	where a.snap_dt&amp;lt;="&amp;amp;max_dt"d;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note this is only needed because you specified that the value in the macro variable should be formatted as date9.&amp;nbsp; If you remove the format option in the&amp;nbsp; PROC SQL step that creates the macro variable, your code would look like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select max(a.payout_dt)
	into :max_dt
	from mytable a;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In that case, your macro variable MAX_DT would have the value 23243.&amp;nbsp; And that value would work in your original code because 23243 is a valid date value.&amp;nbsp; Meaning, you could code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table report as 
	select *
	from my.database a
	where a.snap_dt&amp;lt;=23243;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Aug 2023 16:34:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Let-macro-variable-be-max-date-in-dataset/m-p/890434#M351847</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-08-22T16:34:30Z</dc:date>
    </item>
    <item>
      <title>Re: Let macro variable be max date in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Let-macro-variable-be-max-date-in-dataset/m-p/890435#M351848</link>
      <description>&lt;P&gt;For values to be compared don't use formatted values.&lt;/P&gt;
&lt;P&gt;Would you typically write code that looked like?&lt;/P&gt;
&lt;PRE&gt;where var = 01Jan2023;&lt;/PRE&gt;
&lt;P&gt;That would incorrect for either a character variable or date value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I bet this works if snap_dt is actually a date value.&lt;/P&gt;
&lt;PRE&gt;proc sql;
select max(a.payout_dt) 
	into :max_dt
	from my.table a;
quit;

proc sql;
create table report as 
	select *
	from my.database a
	where a.snap_dt&amp;lt;=&amp;amp;max_dt;
quit;&lt;/PRE&gt;
&lt;P&gt;If you apply a format to the macro variable then you need to create a date literal value. With &amp;amp;max_dt&lt;/P&gt;
&lt;P&gt;that would look like "&amp;amp;max_dt."d for SAS to recognize the use as a date.&lt;/P&gt;</description>
      <pubDate>Tue, 22 Aug 2023 16:34:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Let-macro-variable-be-max-date-in-dataset/m-p/890435#M351848</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-08-22T16:34:41Z</dc:date>
    </item>
    <item>
      <title>Re: Let macro variable be max date in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Let-macro-variable-be-max-date-in-dataset/m-p/890441#M351852</link>
      <description>&lt;P&gt;I really appreciate the explanation.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I did fix my code to this&lt;/P&gt;&lt;PRE&gt;proc sql;
select max(a.payout_dt) format=date9.
	into :max_dt
	from my.table a;
quit;

proc sql;
create table report as 
	select *
	from my.database a
	where a.snap_dt&amp;lt;='&amp;amp;max_dt'd;
quit;&lt;/PRE&gt;&lt;P&gt;but I'm got a new error&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;48         where a.snap_dt&amp;lt;='&amp;amp;max_dt'd;
&lt;FONT color="#FF0000"&gt;ERROR: Invalid date/time/datetime constant '&amp;amp;max_dt'd.&lt;/FONT&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;FYI, the the variables properties are:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;a.payout_dt Format Date9., Informat Date9.&lt;/LI&gt;&lt;LI&gt;snap_dt format Date9.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;That's why not sure why I'm now getting this error when I already formatted max_dt as date9.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your help again!&lt;/P&gt;</description>
      <pubDate>Tue, 22 Aug 2023 17:10:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Let-macro-variable-be-max-date-in-dataset/m-p/890441#M351852</guid>
      <dc:creator>couthelle</dc:creator>
      <dc:date>2023-08-22T17:10:50Z</dc:date>
    </item>
    <item>
      <title>Re: Let macro variable be max date in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Let-macro-variable-be-max-date-in-dataset/m-p/890442#M351853</link>
      <description>Macro variables don’t resolve inside single quotes so you need to use double quotes, eg:&lt;BR /&gt;&lt;BR /&gt;where a.snap_dt&amp;lt;=“&amp;amp;max_dt”d;</description>
      <pubDate>Tue, 22 Aug 2023 17:15:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Let-macro-variable-be-max-date-in-dataset/m-p/890442#M351853</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-08-22T17:15:14Z</dc:date>
    </item>
    <item>
      <title>Re: Let macro variable be max date in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Let-macro-variable-be-max-date-in-dataset/m-p/890443#M351854</link>
      <description>&lt;P&gt;Use double quotes when you want the macro processor to take action on a quoted string.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The value inside the quotes in a date literal has to be something the DATE informat can understand.&lt;/P&gt;
&lt;P&gt;Since you used single quote characters instead of double quote characters the macro processor ignored the &amp;amp;max_dt inside the single quotes.&amp;nbsp; So the DATE informat did not know what to do with the literal characters &amp;amp;max_dt you passed it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Aug 2023 17:17:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Let-macro-variable-be-max-date-in-dataset/m-p/890443#M351854</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-08-22T17:17:58Z</dc:date>
    </item>
  </channel>
</rss>

