<?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: first day of last month as datetime for where condition in proc sql in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/first-day-of-last-month-as-datetime-for-where-condition-in-proc/m-p/943771#M369871</link>
    <description>&lt;P&gt;I think you are overcomplicating the answer. From your example, you always want midnight on the first day of the previous month. If so, just tack on the time as a literal value. To get the resolved text surrounded by single quotes, use the built-in %tslit function. Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let mydt = %tslit(%sysfunc(intnx(day, %sysfunc(intnx(month, %sysfunc(date()), -1)), 0), Date9.) 00:00:00)dt;
%put NOTE: &amp;amp;=mydt;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;NOTE: MYDT='01AUG2024 00:00:00'dt&lt;/PRE&gt;</description>
    <pubDate>Fri, 13 Sep 2024 12:21:41 GMT</pubDate>
    <dc:creator>SASJedi</dc:creator>
    <dc:date>2024-09-13T12:21:41Z</dc:date>
    <item>
      <title>first day of last month as datetime for where condition in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/first-day-of-last-month-as-datetime-for-where-condition-in-proc/m-p/943766#M369869</link>
      <description>&lt;P&gt;I want to make the following make the following dynamic/automated with intnx(), so I don't need to change the macrovariable manually each month I use my code. After I define it, I use it in the where condition of a SQL procedure.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;%let datetime = '01aug2024 00:00:00'dt;&lt;/PRE&gt;&lt;P&gt;The following code generates the first day of the last month, however I need it exactly as stated in the static example above.&lt;/P&gt;&lt;PRE&gt;%let mydt = %sysfunc(intnx(day, %sysfunc(intnx(month, %sysfunc(date()), -1)), 0), Date9.);
%put &amp;amp;mydt;&lt;/PRE&gt;&lt;P&gt;When I try the following code, which should be a valid solution according to my knowledge, I get 01JAN1960:06:00:00&lt;/P&gt;&lt;PRE&gt;%let mydt = %sysfunc(intnx(day, %sysfunc(intnx(month, %sysfunc(date()), -1)), 0));
%let mydt = %sysfunc(intnx(hour, &amp;amp;mydt., 0));
%let mydt = %sysfunc(intnx(minute, &amp;amp;mydt., 0));
%let mydt = %sysfunc(intnx(second, &amp;amp;mydt., 0), Datetime.);
%put &amp;amp;mydt;&lt;/PRE&gt;&lt;P&gt;How can I solve this problem? I tried concatenation and countless other approaches that didn't work and it starts to drive me to despair. Thank you for your advice and help in advance!&lt;/P&gt;</description>
      <pubDate>Fri, 13 Sep 2024 11:25:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/first-day-of-last-month-as-datetime-for-where-condition-in-proc/m-p/943766#M369869</guid>
      <dc:creator>SAS_Newbie2024</dc:creator>
      <dc:date>2024-09-13T11:25:05Z</dc:date>
    </item>
    <item>
      <title>Re: first day of last month as datetime for where condition in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/first-day-of-last-month-as-datetime-for-where-condition-in-proc/m-p/943771#M369871</link>
      <description>&lt;P&gt;I think you are overcomplicating the answer. From your example, you always want midnight on the first day of the previous month. If so, just tack on the time as a literal value. To get the resolved text surrounded by single quotes, use the built-in %tslit function. Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let mydt = %tslit(%sysfunc(intnx(day, %sysfunc(intnx(month, %sysfunc(date()), -1)), 0), Date9.) 00:00:00)dt;
%put NOTE: &amp;amp;=mydt;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;NOTE: MYDT='01AUG2024 00:00:00'dt&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Sep 2024 12:21:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/first-day-of-last-month-as-datetime-for-where-condition-in-proc/m-p/943771#M369871</guid>
      <dc:creator>SASJedi</dc:creator>
      <dc:date>2024-09-13T12:21:41Z</dc:date>
    </item>
    <item>
      <title>Re: first day of last month as datetime for where condition in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/first-day-of-last-month-as-datetime-for-where-condition-in-proc/m-p/943776#M369875</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let mydt = %sysfunc(intnx(dtmonth,%sysfunc(datetime()),-1,b));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Maxim 28: Macro Variables Need No Formats.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Sep 2024 12:44:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/first-day-of-last-month-as-datetime-for-where-condition-in-proc/m-p/943776#M369875</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-09-13T12:44:26Z</dc:date>
    </item>
    <item>
      <title>Re: first day of last month as datetime for where condition in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/first-day-of-last-month-as-datetime-for-where-condition-in-proc/m-p/943780#M369877</link>
      <description>&lt;P&gt;If you want to use the value in SAS code then don't bother to format it. Just let %SYSFUNC() generate the number that SAS uses to store that datetime value.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let datetime = %sysfunc(intnx(dtmonth,%sysfunc(datetime()),-1));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want the value to be something a human can understand then you can convert it to a datetime literal by&amp;nbsp;enclosing it in quotes and appending the letters dt.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let datetime = "%sysfunc(intnx(dtmonth,%sysfunc(datetime()),-1),datetime19.)"dt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;1    %let datetime1 = %sysfunc(intnx(dtmonth,%sysfunc(datetime()),-1));
2    %let datetime2 = "%sysfunc(intnx(dtmonth,%sysfunc(datetime()),-1),datetime19.)"dt;
3    %put &amp;amp;=datetime1 &amp;amp;=datetime2;
DATETIME1=2038089600 DATETIME2=" 01AUG2024:00:00:00"dt
4    data test;
5      datetime1 = &amp;amp;datetime1;
6      datetime2 = &amp;amp;datetime2;
7      if datetime1=datetime2 then put 'SAME VALUE';
8      else put 'DIFFERENT VALUE';
9    run;

SAME VALUE
NOTE: The data set WORK.TEST has 1 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
&lt;/PRE&gt;
&lt;P&gt;Note: The DATETIME format has a bug.&amp;nbsp; If you attempt to use DATETIME18. you will get only 2 digits for the year.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Sep 2024 12:57:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/first-day-of-last-month-as-datetime-for-where-condition-in-proc/m-p/943780#M369877</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-09-13T12:57:52Z</dc:date>
    </item>
  </channel>
</rss>

