<?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: How to subtract months to a date in DATETIME25.6 format (e.g., 2024-12-31 00:00) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-months-to-a-date-in-DATETIME25-6-format-e-g/m-p/958876#M374218</link>
    <description>You were right. This was not the issue. The code was run with another macro variable that used to work properly. I am going to try the unformatted version per your instructions and see how that goes. Thanks again for your help.</description>
    <pubDate>Mon, 10 Feb 2025 20:59:14 GMT</pubDate>
    <dc:creator>donspaul</dc:creator>
    <dc:date>2025-02-10T20:59:14Z</dc:date>
    <item>
      <title>How to subtract months to a date in DATETIME25.6 format (e.g., 30JAN2025:00:00:00.000000)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-months-to-a-date-in-DATETIME25-6-format-e-g/m-p/958862#M374208</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to subtract a certain number of months to dates that are in DATETIME25.6 format.&amp;nbsp; For example, if the original date is 31DEC2024:00:00:00.000000 and I would like to subtract 1 month, I would like the resulting date to be 30NOV2024:00:00:00.000000.&amp;nbsp; Both the original date and the resulting date should be in DATETIME25.6 format.&amp;nbsp; I tried using the following in a query and the RESULTING_DATE is showing up as a missing value.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;INTNX('MONTHS',ORIGINAL_DATE,-11,'E') AS RESULTING_DATE FORMAT=DATETIME25.6&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help would be greatly appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Don&lt;/P&gt;</description>
      <pubDate>Mon, 10 Feb 2025 19:36:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-months-to-a-date-in-DATETIME25-6-format-e-g/m-p/958862#M374208</guid>
      <dc:creator>donspaul</dc:creator>
      <dc:date>2025-02-10T19:36:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to subtract months to a date in DATETIME25.6 format (e.g., 2024-12-31 00:00)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-months-to-a-date-in-DATETIME25-6-format-e-g/m-p/958863#M374209</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;INTNX('DTMONTH',ORIGINAL_DATE,-11,'E') AS RESULTING_DATE FORMAT=DATETIME25.6&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you are working with Date/Time values, you must use DTMONTH (beginning with DT to indicate that you want date/time calculations). Also note it is DTMONTH and not plural DTMONTHS&lt;/P&gt;</description>
      <pubDate>Mon, 10 Feb 2025 19:38:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-months-to-a-date-in-DATETIME25-6-format-e-g/m-p/958863#M374209</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-02-10T19:38:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to subtract months to a date in DATETIME25.6 format (e.g., 2024-12-31 00:00)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-months-to-a-date-in-DATETIME25-6-format-e-g/m-p/958864#M374210</link>
      <description>&lt;P&gt;You asked SAS to subtract around 11 times 30 from the value since there are about 30 days in month.&lt;/P&gt;
&lt;P&gt;But since your variable has DATETIME values it is stored in SECONDS, not DAYS.&amp;nbsp; So even if that worked it would only change the value by about 5 and half minutes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use the DTMONTH interval instead.&lt;/P&gt;</description>
      <pubDate>Mon, 10 Feb 2025 19:35:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-months-to-a-date-in-DATETIME25-6-format-e-g/m-p/958864#M374210</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-02-10T19:35:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to subtract months to a date in DATETIME25.6 format (e.g., 2024-12-31 00:00)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-months-to-a-date-in-DATETIME25-6-format-e-g/m-p/958868#M374211</link>
      <description>&lt;P&gt;Thank you.&amp;nbsp; That worked, but I have another issue related to my original post.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to pass the resulting date to a macro variable and I did that using the following code:&lt;/P&gt;&lt;P&gt;PROC SQL;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;SELECT RESULTING_DATE&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;INTO: RESULTING_DATE&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;FROM TIME_DATA;&lt;BR /&gt;QUIT;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, when I did that, it resolves to&amp;nbsp;30NOV2024.&amp;nbsp; Could you please let me know how I could reformat the macro variable %RESULTING_DATE to be in DATETIME25.6 format.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again for your assistance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Don&lt;/P&gt;</description>
      <pubDate>Mon, 10 Feb 2025 19:56:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-months-to-a-date-in-DATETIME25-6-format-e-g/m-p/958868#M374211</guid>
      <dc:creator>donspaul</dc:creator>
      <dc:date>2025-02-10T19:56:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to subtract months to a date in DATETIME25.6 format (e.g., 2024-12-31 00:00)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-months-to-a-date-in-DATETIME25-6-format-e-g/m-p/958869#M374212</link>
      <description>&lt;P&gt;In most cases, macro variables should be unformatted, not formatted.&amp;nbsp;&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/Maxims-of-Maximally-Efficient-SAS-Programmers/ta-p/352068" target="_self"&gt;Maxim 28&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What do you plan to do with this macro variable?&lt;/P&gt;</description>
      <pubDate>Mon, 10 Feb 2025 20:01:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-months-to-a-date-in-DATETIME25-6-format-e-g/m-p/958869#M374212</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-02-10T20:01:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to subtract months to a date in DATETIME25.6 format (e.g., 2024-12-31 00:00)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-months-to-a-date-in-DATETIME25-6-format-e-g/m-p/958870#M374213</link>
      <description>&lt;P&gt;That would only happen if you had attached the DTDATE9. format to the variable.&lt;/P&gt;
&lt;PRE&gt;1    data test;
2      dt=datetime();
3      date=dt;
4      format dt datetime19. date dtdate9.;
5    run;

NOTE: The data set WORK.TEST has 1 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds


6
7    proc sql noprint;
8    select dt,date
9      into :dt,:date
10     from test;
11   ;
12   quit;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


13   %put &amp;amp;=dt &amp;amp;=date ;
DT= 10FEB2025:15:06:36 DATE=10FEB2025
&lt;/PRE&gt;
&lt;P&gt;What do you plan to do with the macro variable?&lt;/P&gt;
&lt;P&gt;If you plan to compare it to other SAS variables with datetime values then take the unformatted value.&amp;nbsp; (That is the raw number of seconds).&lt;/P&gt;
&lt;P&gt;If you plan to use it in a title or other text string then apply the format you want in the SELECT statement that makes the macro variable.&lt;/P&gt;
&lt;PRE&gt;14
15   proc sql noprint;
16   select dt format=32.
17        , dt format=dtdate9.
18        , dt format=datetime19.
19     into :raw_dt trimmed
20        , :date trimmed
21        , :dt trimmed
22     from test
23   ;
24   quit;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


25   %put &amp;amp;=raw_dt &amp;amp;=date &amp;amp;=dt ;
RAW_DT=2054819196 DATE=10FEB2025 DT=10FEB2025:15:06:36&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Feb 2025 20:08:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-months-to-a-date-in-DATETIME25-6-format-e-g/m-p/958870#M374213</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-02-10T20:08:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to subtract months to a date in DATETIME25.6 format (e.g., 2024-12-31 00:00)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-months-to-a-date-in-DATETIME25-6-format-e-g/m-p/958872#M374214</link>
      <description>&lt;P&gt;Tom,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You are right.&amp;nbsp; The date resolved to the correct format in the macro variable.&amp;nbsp; I should have checked the Macro Variable Viewer before posting in the first place.&amp;nbsp; My bad.&amp;nbsp; I am trying to set up an automation where my code automatically pulls rolling 12-month of data based on the date field.&amp;nbsp; I have approached this as follows:&lt;/P&gt;&lt;P&gt;1. My code checks the max date that is in the data table which represents the most recent month of data (i.e., the "END_DATE" for my purposes)&lt;/P&gt;&lt;P&gt;2.&amp;nbsp;Using the DTTIME option in INTNX I calculate the 12-month PRIOR date, (i.e., the "BEGIN_DATE" for my purposes)&lt;/P&gt;&lt;P&gt;3. I create %END_DATE and %BEGIN_DATE macro variables based on steps #1 and #2 above.&amp;nbsp; This way when I run this code next month, it will pull the appropriate rolling 12-month window without manually having to specify it each time.&lt;/P&gt;&lt;P&gt;4. I pass the macro variables in the WHERE statement in PROC SQL to only return data rows that are between %BEGIN_DATE and %END_DATE&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the syntax for the WHERE clause I am using:&lt;/P&gt;&lt;P&gt;where (&amp;amp;STRT_DATE &amp;lt;= a.veh_first_quot_tstmp_local and a.veh_first_quot_tstmp_local &amp;lt;= &amp;amp;END_DATE)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, when I pass the macro variable in step #4, I am getting the following error in my query which I am not able to decipher.&amp;nbsp; The date format in the source table as well as the macro variables are both DATE25.6.&amp;nbsp; In my data,&amp;nbsp; %END_DATE is&amp;nbsp;30JAN2025:00:00:00.000000 and %BEGIN_DATE is&amp;nbsp;01FEB2024:00:00:00.000000&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ERROR: CLI prepare error: [Redshift][ODBC Driver][Server]42601:ERROR: syntax error at or near "FEB2024" in context "where&lt;BR /&gt;(01FEB2024", at line 1 LINE 1, POSITION 50: ..._id and a.hhld_exp_cnt = b.hhld_exp_cnt) where (01FEB2024:00...&lt;/P&gt;</description>
      <pubDate>Mon, 10 Feb 2025 20:30:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-months-to-a-date-in-DATETIME25-6-format-e-g/m-p/958872#M374214</guid>
      <dc:creator>donspaul</dc:creator>
      <dc:date>2025-02-10T20:30:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to subtract months to a date in DATETIME25.6 format (e.g., 2024-12-31 00:00)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-months-to-a-date-in-DATETIME25-6-format-e-g/m-p/958873#M374215</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where (&amp;amp;STRT_DATE &amp;lt;= a.veh_first_quot_tstmp_local and a.veh_first_quot_tstmp_local &amp;lt;= &amp;amp;END_DATE)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Arithmetic operations and logical operations work properly with &lt;EM&gt;&lt;FONT color="#FF0000"&gt;unformatted&lt;/FONT&gt; &lt;/EM&gt;macro variables.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To get unformatted macro variables, use&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select variablename format=best32. into :strt_date from have&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(Best32. produces the integer that represents the date/time value, essentially this is the unformatted value. If you don't like all the spaces before this integer in the macro variable use &lt;CODE class=" language-sas"&gt;select variablename format=best32. into :strt_date trimmed from have&lt;/CODE&gt;)&lt;/P&gt;</description>
      <pubDate>Mon, 10 Feb 2025 20:38:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-months-to-a-date-in-DATETIME25-6-format-e-g/m-p/958873#M374215</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-02-10T20:38:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to subtract months to a date in DATETIME25.6 format (e.g., 2024-12-31 00:00)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-months-to-a-date-in-DATETIME25-6-format-e-g/m-p/958874#M374216</link>
      <description>&lt;P&gt;I figured out the issue, I forgot to put a period when calling the macro variable in the WHERE clause, I modified the syntax as follows and it works exactly as intended.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;where (&amp;amp;STRT_DATE. &amp;lt;= a.veh_first_quot_tstmp_local and a.veh_first_quot_tstmp_local &amp;lt;= &amp;amp;END_DATE.)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again for all the thought partnership on this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Don&lt;/P&gt;</description>
      <pubDate>Mon, 10 Feb 2025 20:39:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-months-to-a-date-in-DATETIME25-6-format-e-g/m-p/958874#M374216</guid>
      <dc:creator>donspaul</dc:creator>
      <dc:date>2025-02-10T20:39:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to subtract months to a date in DATETIME25.6 format (e.g., 2024-12-31 00:00)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-months-to-a-date-in-DATETIME25-6-format-e-g/m-p/958875#M374217</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/209976"&gt;@donspaul&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I figured out the issue, I forgot to put a period when calling the macro variable in the WHERE clause, I modified the syntax as follows and it works exactly as intended.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;where (&amp;amp;STRT_DATE. &amp;lt;= a.veh_first_quot_tstmp_local and a.veh_first_quot_tstmp_local &amp;lt;= &amp;amp;END_DATE.)&lt;/SPAN&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This would not cause the error messages you saw. More than likely, the error messages were because you used formatted macro variables instead of &lt;FONT color="#FF0000"&gt;un&lt;/FONT&gt;formatted macro variables.&lt;/P&gt;</description>
      <pubDate>Mon, 10 Feb 2025 20:47:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-months-to-a-date-in-DATETIME25-6-format-e-g/m-p/958875#M374217</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-02-10T20:47:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to subtract months to a date in DATETIME25.6 format (e.g., 2024-12-31 00:00)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-months-to-a-date-in-DATETIME25-6-format-e-g/m-p/958876#M374218</link>
      <description>You were right. This was not the issue. The code was run with another macro variable that used to work properly. I am going to try the unformatted version per your instructions and see how that goes. Thanks again for your help.</description>
      <pubDate>Mon, 10 Feb 2025 20:59:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-months-to-a-date-in-DATETIME25-6-format-e-g/m-p/958876#M374218</guid>
      <dc:creator>donspaul</dc:creator>
      <dc:date>2025-02-10T20:59:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to subtract months to a date in DATETIME25.6 format (e.g., 2024-12-31 00:00)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-months-to-a-date-in-DATETIME25-6-format-e-g/m-p/958877#M374219</link>
      <description>&lt;P&gt;THANK YOU!!!&amp;nbsp; This finally worked (and yes, I double, triple checked this time around:-) ).&amp;nbsp; Using unformatted macro vars was the key.&amp;nbsp; It worked fine without trimming the spaces.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Feb 2025 21:05:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-months-to-a-date-in-DATETIME25-6-format-e-g/m-p/958877#M374219</guid>
      <dc:creator>donspaul</dc:creator>
      <dc:date>2025-02-10T21:05:12Z</dc:date>
    </item>
  </channel>
</rss>

