<?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: Problem with MDY function to take the previous month in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Problem-with-MDY-function-to-take-the-previous-month/m-p/611646#M18236</link>
    <description>&lt;P&gt;INTNX() is used to move the date back one month, you can not use base math here because you cycle from 1 to 12 with month. You can use MOD() if you want to use math type functions, but using INTNX() appropriately is a better approach.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/303367"&gt;@Mick_lb&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello everyone,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;From a date, I want to take the last day of the previous month. And after that, display the month and the year (of the previous month).&lt;/P&gt;
&lt;P&gt;The problem is, my code is working for every month except January.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let date="01JAN2017:00:00:00"dt;
%put ===&amp;gt; &amp;amp;date.;

data _null_;
last_day = day(intnx('month', datepart(&amp;amp;date.), -1, "end"));
CALL SYMPUT("last_day",PUT(last_day,DATE9.));
/*To display the month*/
p_month=MDY(month(datepart(&amp;amp;date.))-1, last_day, year(datepart(&amp;amp;date.))); /*Error here*/
CALL SYMPUT("p_month",PUT(p_month,FRADFMN.));
/*To display the year*/
p_year=MDY(month(datepart(&amp;amp;date.))-1, last_day, year(datepart(&amp;amp;date.))); /*Error here*/
CALL SYMPUT("p_year",PUT(p_year,YEAR4.));
run;
%put ===&amp;gt; &amp;amp;last_day.;
%put ===&amp;gt; &amp;amp;p_month.;
%put ===&amp;gt; &amp;amp;p_year.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;For January, I got the following error :&lt;/SPAN&gt;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;NOTE: Invalid argument for MDY function (0,31,2017)&amp;nbsp;at line 31 column 9.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;NOTE: Invalid argument for MDY function (0,31,2017) at line 34 column 8.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;last_day=31 p_month=. p_year=. _ERROR_=1 _N_=1 &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;NOTE: Mathematical operations could not be performed at the following places. The results of the operations have been set to missing values.&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;How to tell SAS not to take month = 0 when it's January ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you in advance for your help !&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best regards,&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 13 Dec 2019 16:41:12 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2019-12-13T16:41:12Z</dc:date>
    <item>
      <title>Problem with MDY function to take the previous month</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Problem-with-MDY-function-to-take-the-previous-month/m-p/611537#M18213</link>
      <description>&lt;P&gt;Hello everyone,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;From a date, I want to take the last day of the previous month. And after that, display the month and the year (of the previous month).&lt;/P&gt;&lt;P&gt;The problem is, my code is working for every month except January.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let date="01JAN2017:00:00:00"dt;
%put ===&amp;gt; &amp;amp;date.;

data _null_;
last_day = day(intnx('month', datepart(&amp;amp;date.), -1, "end"));
CALL SYMPUT("last_day",PUT(last_day,DATE9.));
/*To display the month*/
p_month=MDY(month(datepart(&amp;amp;date.))-1, last_day, year(datepart(&amp;amp;date.))); /*Error here*/
CALL SYMPUT("p_month",PUT(p_month,FRADFMN.));
/*To display the year*/
p_year=MDY(month(datepart(&amp;amp;date.))-1, last_day, year(datepart(&amp;amp;date.))); /*Error here*/
CALL SYMPUT("p_year",PUT(p_year,YEAR4.));
run;
%put ===&amp;gt; &amp;amp;last_day.;
%put ===&amp;gt; &amp;amp;p_month.;
%put ===&amp;gt; &amp;amp;p_year.;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;For January, I got the following error :&lt;/SPAN&gt;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;NOTE: Invalid argument for MDY function (0,31,2017)&amp;nbsp;at line 31 column 9.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;NOTE: Invalid argument for MDY function (0,31,2017) at line 34 column 8.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;last_day=31 p_month=. p_year=. _ERROR_=1 _N_=1 &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;NOTE: Mathematical operations could not be performed at the following places. The results of the operations have been set to missing values.&lt;/SPAN&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;How to tell SAS not to take month = 0 when it's January ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you in advance for your help !&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best regards,&lt;/P&gt;</description>
      <pubDate>Fri, 13 Dec 2019 10:04:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Problem-with-MDY-function-to-take-the-previous-month/m-p/611537#M18213</guid>
      <dc:creator>Mick_lb</dc:creator>
      <dc:date>2019-12-13T10:04:30Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with MDY function to take the previous month</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Problem-with-MDY-function-to-take-the-previous-month/m-p/611540#M18216</link>
      <description>&lt;P&gt;Overcomplicating things by a mile.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
last_date = intnx('month', datepart(&amp;amp;date.), -1, "end");
CALL SYMPUT("last_day",PUT(day(last_date),2.));
/*To display the month*/
CALL SYMPUT("p_month",PUT(month(last_date),2.);
/*To display the year*/
CALL SYMPUT("p_year",PUT(year(last_date),4.));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Dec 2019 10:17:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Problem-with-MDY-function-to-take-the-previous-month/m-p/611540#M18216</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-12-13T10:17:18Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with MDY function to take the previous month</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Problem-with-MDY-function-to-take-the-previous-month/m-p/611541#M18217</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/303367"&gt;@Mick_lb&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You need to be very careful with dates in macro variables.&lt;/P&gt;
&lt;P&gt;It should not be formatted to be used for further calculation&lt;/P&gt;
&lt;P&gt;Typically, 01JAN2017 (which is human-readable) is stored as the numeric value&amp;nbsp;&lt;SPAN&gt;20820 by SAS (=&amp;nbsp;number of days since 01JAN1960)&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Best,&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let date=01JAN2017:00:00:00;
%put ===&amp;gt; &amp;amp;date;

%let date_2=%sysfunc(datepart(%sysfunc(inputn(&amp;amp;date,datetime18))));

%let last_day=%sysfunc(day(%sysfunc(intnx(month,&amp;amp;date_2,-1,end))));
%let p_month=%sysfunc(month(%sysfunc(intnx(month,&amp;amp;date_2,-1,end))));
%let p_year=%sysfunc(year(%sysfunc(intnx(month,&amp;amp;date_2,-1,end))));

%put ===&amp;gt; &amp;amp;last_day.;
%put ===&amp;gt; &amp;amp;p_month.;
%put ===&amp;gt; &amp;amp;p_year.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Dec 2019 10:29:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Problem-with-MDY-function-to-take-the-previous-month/m-p/611541#M18217</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2019-12-13T10:29:28Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with MDY function to take the previous month</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Problem-with-MDY-function-to-take-the-previous-month/m-p/611646#M18236</link>
      <description>&lt;P&gt;INTNX() is used to move the date back one month, you can not use base math here because you cycle from 1 to 12 with month. You can use MOD() if you want to use math type functions, but using INTNX() appropriately is a better approach.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/303367"&gt;@Mick_lb&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello everyone,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;From a date, I want to take the last day of the previous month. And after that, display the month and the year (of the previous month).&lt;/P&gt;
&lt;P&gt;The problem is, my code is working for every month except January.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let date="01JAN2017:00:00:00"dt;
%put ===&amp;gt; &amp;amp;date.;

data _null_;
last_day = day(intnx('month', datepart(&amp;amp;date.), -1, "end"));
CALL SYMPUT("last_day",PUT(last_day,DATE9.));
/*To display the month*/
p_month=MDY(month(datepart(&amp;amp;date.))-1, last_day, year(datepart(&amp;amp;date.))); /*Error here*/
CALL SYMPUT("p_month",PUT(p_month,FRADFMN.));
/*To display the year*/
p_year=MDY(month(datepart(&amp;amp;date.))-1, last_day, year(datepart(&amp;amp;date.))); /*Error here*/
CALL SYMPUT("p_year",PUT(p_year,YEAR4.));
run;
%put ===&amp;gt; &amp;amp;last_day.;
%put ===&amp;gt; &amp;amp;p_month.;
%put ===&amp;gt; &amp;amp;p_year.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;For January, I got the following error :&lt;/SPAN&gt;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;NOTE: Invalid argument for MDY function (0,31,2017)&amp;nbsp;at line 31 column 9.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;NOTE: Invalid argument for MDY function (0,31,2017) at line 34 column 8.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;last_day=31 p_month=. p_year=. _ERROR_=1 _N_=1 &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;NOTE: Mathematical operations could not be performed at the following places. The results of the operations have been set to missing values.&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;How to tell SAS not to take month = 0 when it's January ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you in advance for your help !&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best regards,&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Dec 2019 16:41:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Problem-with-MDY-function-to-take-the-previous-month/m-p/611646#M18236</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-12-13T16:41:12Z</dc:date>
    </item>
  </channel>
</rss>

