<?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 use macro date variable in a query in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/How-to-use-macro-date-variable-in-a-query/m-p/376733#M65397</link>
    <description>&lt;P&gt;Hi everyone,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is a similar question to what I posted last time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I tried&amp;nbsp;plug in a date macro variable (&amp;amp;curdat_2mon as in the code below)&amp;nbsp;in my query. But it looks the quote (&lt;FONT color="#0000ff" face="Courier New"&gt;%str&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;(%')&lt;/FONT&gt;)&amp;nbsp;I used is not right? Can anyone help me correct again?&amp;nbsp;If there is&amp;nbsp;a simplified way to use the macro variable in the query, that would be great too.&amp;nbsp;Thanks a lot!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;_null_&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt; curdat_2m = intnx(&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'Month'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;,&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;"&amp;amp;sysdate"d&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;,-&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;2&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;,&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'s'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;call&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; symput (&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'curdat_2mon'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;,put(curdat_2m,&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;date9.&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;)) ;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%put&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; &amp;amp;curdat_2mon ;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;PROC&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;SQL&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;CREATE&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;TABLE&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; new_tbl &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;as&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;select&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;distinct&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; MBR_ID, FST_NM, LST_NM, DOB, ZIP&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;from&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; DB.MBR_TBL &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;where&lt;/FONT&gt;&amp;nbsp; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%str&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;(%')&amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;curdat_2mon.&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%str&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;(%')d&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;between&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; BEG_DT &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;and&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; END_DT&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;QUIT&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 17 Jul 2017 19:53:18 GMT</pubDate>
    <dc:creator>lizzy28</dc:creator>
    <dc:date>2017-07-17T19:53:18Z</dc:date>
    <item>
      <title>How to use macro date variable in a query</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-use-macro-date-variable-in-a-query/m-p/376733#M65397</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is a similar question to what I posted last time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I tried&amp;nbsp;plug in a date macro variable (&amp;amp;curdat_2mon as in the code below)&amp;nbsp;in my query. But it looks the quote (&lt;FONT color="#0000ff" face="Courier New"&gt;%str&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;(%')&lt;/FONT&gt;)&amp;nbsp;I used is not right? Can anyone help me correct again?&amp;nbsp;If there is&amp;nbsp;a simplified way to use the macro variable in the query, that would be great too.&amp;nbsp;Thanks a lot!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;_null_&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt; curdat_2m = intnx(&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'Month'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;,&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;"&amp;amp;sysdate"d&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;,-&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;2&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;,&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'s'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;call&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; symput (&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'curdat_2mon'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;,put(curdat_2m,&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;date9.&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;)) ;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%put&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; &amp;amp;curdat_2mon ;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;PROC&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;SQL&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;CREATE&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;TABLE&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; new_tbl &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;as&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;select&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;distinct&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; MBR_ID, FST_NM, LST_NM, DOB, ZIP&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;from&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; DB.MBR_TBL &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;where&lt;/FONT&gt;&amp;nbsp; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%str&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;(%')&amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;curdat_2mon.&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%str&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;(%')d&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;between&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; BEG_DT &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;and&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; END_DT&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;QUIT&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Jul 2017 19:53:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-use-macro-date-variable-in-a-query/m-p/376733#M65397</guid>
      <dc:creator>lizzy28</dc:creator>
      <dc:date>2017-07-17T19:53:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to use macro date variable in a query</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-use-macro-date-variable-in-a-query/m-p/376735#M65398</link>
      <description>&lt;P&gt;Either don't apply the format, the easiest method, or in your usage of the variable just use double quotes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
CREATE TABLE new_tbl as
select distinct MBR_ID, FST_NM, LST_NM, DOB, ZIP
from DB.MBR_TBL
where  "&amp;amp;curdat_2mon"d
between BEG_DT and END_DT
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Easier:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
curdat_2m = intnx('Month',"&amp;amp;sysdate"d,-2,'s');
call symput ('curdat_2mon',curdat_2m) ;
run;

%put &amp;amp;curdat_2mon ;

PROC SQL;
CREATE TABLE new_tbl as
select distinct MBR_ID, FST_NM, LST_NM, DOB, ZIP
from DB.MBR_TBL
where  &amp;amp;curdat_2mon.
between BEG_DT and END_DT
;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Jul 2017 19:57:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-use-macro-date-variable-in-a-query/m-p/376735#M65398</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-07-17T19:57:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to use macro date variable in a query</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-use-macro-date-variable-in-a-query/m-p/376742#M65399</link>
      <description>&lt;P&gt;I would suggest using double quotes:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where "&amp;amp;curdat_2mon"d
between BEG_DT and END_DT
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That avoids the need to %STR() for macro quoting.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you really want to use single quotes, it should work if you %unquote it yourself, e.g.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where  %unquote(%str(%')&amp;amp;curdat_2mon.%str(%')d)
between BEG_DT and END_DT
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The macro processor sometimes failes to unquote code automatically.&lt;/P&gt;</description>
      <pubDate>Mon, 17 Jul 2017 20:09:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-use-macro-date-variable-in-a-query/m-p/376742#M65399</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2017-07-17T20:09:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to use macro date variable in a query</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-use-macro-date-variable-in-a-query/m-p/376745#M65400</link>
      <description>Thanks a lot, Reeza and Quentin!</description>
      <pubDate>Mon, 17 Jul 2017 20:12:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-use-macro-date-variable-in-a-query/m-p/376745#M65400</guid>
      <dc:creator>lizzy28</dc:creator>
      <dc:date>2017-07-17T20:12:08Z</dc:date>
    </item>
  </channel>
</rss>

