<?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: Is there a way to reference date macro variable in proc SQL like operator? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-way-to-reference-date-macro-variable-in-proc-SQL-like/m-p/956647#M373516</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/472222"&gt;@SASlearner60&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I was wondering if there was a way to reference date macro variable in proc SQL like operator.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, I want to reference macro variable PREVDATE in like operator. Please refer code example below:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%LET PREVDATE = %sysfunc(intnx(day,%sysfunc(inputn(&amp;amp;sysdate9,date11.)),-2,end),);&lt;BR /&gt;%LET PREVDATE = %sysfunc(putn(&amp;amp;PREVDATE,date11.));&lt;BR /&gt;%PUT &amp;amp;PREVDATE;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Proc sql;&lt;/P&gt;
&lt;P&gt;Create table XYZ as&lt;/P&gt;
&lt;P&gt;Select distinct A, B&amp;nbsp;&lt;/P&gt;
&lt;P&gt;From C&lt;/P&gt;
&lt;P&gt;Where B like '%&amp;amp;PREVDATE.%';&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;In my opinion, the only way this code makes sense is if variable B is a date value stored as a character string. This should be avoided, dates should be stored as numeric SAS variables, in which case all of the macro variable code to create &amp;amp;PREVDATE can be greatly simplified. Make your programming easy ... store dates as valid numeric variables that contain date values.&lt;/P&gt;</description>
    <pubDate>Mon, 20 Jan 2025 10:24:20 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2025-01-20T10:24:20Z</dc:date>
    <item>
      <title>Is there a way to reference date macro variable in proc SQL like operator?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-way-to-reference-date-macro-variable-in-proc-SQL-like/m-p/956569#M373506</link>
      <description>&lt;P&gt;I was wondering if there was a way to reference date macro variable in proc SQL like operator.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, I want to reference macro variable PREVDATE in like operator. Please refer code example below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%LET PREVDATE = %sysfunc(intnx(day,%sysfunc(inputn(&amp;amp;sysdate9,date11.)),-2,end),);&lt;BR /&gt;%LET PREVDATE = %sysfunc(putn(&amp;amp;PREVDATE,date11.));&lt;BR /&gt;%PUT &amp;amp;PREVDATE;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Proc sql;&lt;/P&gt;&lt;P&gt;Create table XYZ as&lt;/P&gt;&lt;P&gt;Select distinct A, B&amp;nbsp;&lt;/P&gt;&lt;P&gt;From C&lt;/P&gt;&lt;P&gt;Where B like '%&amp;amp;PREVDATE.%';&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;</description>
      <pubDate>Sun, 19 Jan 2025 07:48:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-a-way-to-reference-date-macro-variable-in-proc-SQL-like/m-p/956569#M373506</guid>
      <dc:creator>SASlearner60</dc:creator>
      <dc:date>2025-01-19T07:48:02Z</dc:date>
    </item>
    <item>
      <title>Re: Is there a way to reference date macro variable in proc SQL like operator?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-way-to-reference-date-macro-variable-in-proc-SQL-like/m-p/956570#M373507</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/472222"&gt;@SASlearner60&lt;/a&gt;&amp;nbsp;and welcome to the SAS Support Communities!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As a rule, macro variable references such as &amp;amp;PREVDATE are not resolved if they are enclosed in &lt;EM&gt;single&lt;/EM&gt; quotes. In addition, % and &amp;amp; are macro triggers, but here you only want &amp;amp; to be treated as such. Duplicating the first percent sign helps to avoid this ambiguity:&lt;/P&gt;
&lt;PRE&gt;&lt;FONT size="4"&gt;where B like &lt;FONT color="#00DD00"&gt;"%%&lt;/FONT&gt;&amp;amp;PREVDATE.%&lt;FONT color="#00DD00"&gt;"&lt;/FONT&gt;;&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;Alternatively, you can use the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lepg/p1ty9a8u40yv4wn1k5xfn0pe6pof.htm#n0qflha6f75et2n1cif2dk530vqv" target="_blank" rel="noopener"&gt;CONTAINS operator&lt;/A&gt; (alias: ? [question mark]) and thus avoid the percent signs:&lt;/P&gt;
&lt;PRE&gt;&lt;FONT size="4"&gt;where B &lt;FONT color="#00DD00"&gt;?&lt;/FONT&gt; "&amp;amp;PREVDATE";&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also note that your definition of macro variable PREVDATE can be simplified by creating a &lt;EM&gt;date literal&lt;/EM&gt; from &amp;amp;SYSDATE9:&lt;/P&gt;
&lt;PRE&gt;%let PREVDATE = %sysfunc(putn("&amp;amp;sysdate9"d-2,date11));
%put &amp;amp;PREVDATE;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 19 Jan 2025 09:22:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-a-way-to-reference-date-macro-variable-in-proc-SQL-like/m-p/956570#M373507</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2025-01-19T09:22:00Z</dc:date>
    </item>
    <item>
      <title>Re: Is there a way to reference date macro variable in proc SQL like operator?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-way-to-reference-date-macro-variable-in-proc-SQL-like/m-p/956573#M373509</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your swift response! This works like a charm!&lt;/P&gt;</description>
      <pubDate>Sun, 19 Jan 2025 10:08:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-a-way-to-reference-date-macro-variable-in-proc-SQL-like/m-p/956573#M373509</guid>
      <dc:creator>SASlearner60</dc:creator>
      <dc:date>2025-01-19T10:08:56Z</dc:date>
    </item>
    <item>
      <title>Re: Is there a way to reference date macro variable in proc SQL like operator?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-way-to-reference-date-macro-variable-in-proc-SQL-like/m-p/956647#M373516</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/472222"&gt;@SASlearner60&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I was wondering if there was a way to reference date macro variable in proc SQL like operator.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, I want to reference macro variable PREVDATE in like operator. Please refer code example below:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%LET PREVDATE = %sysfunc(intnx(day,%sysfunc(inputn(&amp;amp;sysdate9,date11.)),-2,end),);&lt;BR /&gt;%LET PREVDATE = %sysfunc(putn(&amp;amp;PREVDATE,date11.));&lt;BR /&gt;%PUT &amp;amp;PREVDATE;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Proc sql;&lt;/P&gt;
&lt;P&gt;Create table XYZ as&lt;/P&gt;
&lt;P&gt;Select distinct A, B&amp;nbsp;&lt;/P&gt;
&lt;P&gt;From C&lt;/P&gt;
&lt;P&gt;Where B like '%&amp;amp;PREVDATE.%';&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;In my opinion, the only way this code makes sense is if variable B is a date value stored as a character string. This should be avoided, dates should be stored as numeric SAS variables, in which case all of the macro variable code to create &amp;amp;PREVDATE can be greatly simplified. Make your programming easy ... store dates as valid numeric variables that contain date values.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Jan 2025 10:24:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-a-way-to-reference-date-macro-variable-in-proc-SQL-like/m-p/956647#M373516</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-01-20T10:24:20Z</dc:date>
    </item>
    <item>
      <title>Re: Is there a way to reference date macro variable in proc SQL like operator?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-way-to-reference-date-macro-variable-in-proc-SQL-like/m-p/956653#M373518</link>
      <description>&lt;P&gt;Two more options that works, just for fun:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%PUT &amp;amp;PREVDATE; /* 18-JAN-2025 */

data C; 
  a="1";
  b="bal bla 18-JAN-2025 bla bal";
  output;
  a="2";
  b="bal bla 19-JAN-2025 bla bal";
  output;
run;

Proc sql;
  Create table XYZ1 as
  Select distinct A, B 
  From C
  Where B like '%' !! "&amp;amp;PREVDATE." !! '%';

  Create table XYZ2 as
  Select distinct A, B 
  From C
  Where B like cats('%',"&amp;amp;PREVDATE.",'%');
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Jan 2025 10:43:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-a-way-to-reference-date-macro-variable-in-proc-SQL-like/m-p/956653#M373518</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2025-01-20T10:43:36Z</dc:date>
    </item>
  </channel>
</rss>

