<?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 get preceding month or following month value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-preceding-month-or-following-month-value/m-p/730607#M227522</link>
    <description>&lt;P&gt;Is "month" a formatted sas date or a string?&lt;/P&gt;
&lt;P&gt;If it is not a date, fix that problem first.&lt;/P&gt;</description>
    <pubDate>Thu, 01 Apr 2021 06:28:41 GMT</pubDate>
    <dc:creator>andreas_lds</dc:creator>
    <dc:date>2021-04-01T06:28:41Z</dc:date>
    <item>
      <title>How to get preceding month or following month value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-preceding-month-or-following-month-value/m-p/730535#M227480</link>
      <description>&lt;P&gt;Hi SAS Pros,&lt;/P&gt;
&lt;P&gt;I have a dataset with month values like this:&lt;/P&gt;
&lt;TABLE width="169"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="64"&gt;ID&lt;/TD&gt;
&lt;TD width="105"&gt;&lt;STRONG&gt;month&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD width="105"&gt;2015-02&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD width="105"&gt;2015-10&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD width="105"&gt;2015-10&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD width="105"&gt;2019-01&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD width="105"&gt;2019-02&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD width="105"&gt;2019-01&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD width="105"&gt;2019-02&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD width="105"&gt;2019-06&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD width="105"&gt;2019-07&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD width="105"&gt;2019-11&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to create a new variable called month_preceding with the values for the following month showed as below:&lt;/P&gt;
&lt;TABLE width="287"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="47" height="30"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="82" height="30"&gt;month&lt;/TD&gt;
&lt;TD width="123" height="30"&gt;month_preceding&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="47" height="30"&gt;1&lt;/TD&gt;
&lt;TD width="82" height="30"&gt;2015-02&lt;/TD&gt;
&lt;TD width="123" height="30"&gt;2015-02&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="47" height="30"&gt;1&lt;/TD&gt;
&lt;TD width="82" height="30"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="123" height="30"&gt;&lt;FONT color="#ff0000"&gt;2015-03&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="47" height="30"&gt;1&lt;/TD&gt;
&lt;TD width="82" height="30"&gt;2015-10&lt;/TD&gt;
&lt;TD width="123" height="30"&gt;2015-10&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="47" height="30"&gt;1&lt;/TD&gt;
&lt;TD width="82" height="30"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="123" height="30"&gt;&lt;FONT color="#ff0000"&gt;2015-11&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="47" height="30"&gt;2&lt;/TD&gt;
&lt;TD width="82" height="30"&gt;2015-10&lt;/TD&gt;
&lt;TD width="123" height="30"&gt;2015-10&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="47" height="30"&gt;2&lt;/TD&gt;
&lt;TD width="82" height="30"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="123" height="30"&gt;&lt;FONT color="#ff0000"&gt;2015-11&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="47" height="30"&gt;2&lt;/TD&gt;
&lt;TD width="82" height="30"&gt;2019-01&lt;/TD&gt;
&lt;TD width="123" height="30"&gt;2019-01&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="47" height="30"&gt;2&lt;/TD&gt;
&lt;TD width="82" height="30"&gt;2019-02&lt;/TD&gt;
&lt;TD width="123" height="30"&gt;2019-02&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="47" height="30"&gt;2&lt;/TD&gt;
&lt;TD width="82" height="30"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="123" height="30"&gt;&lt;FONT color="#ff0000"&gt;2019-03&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="47" height="30"&gt;3&lt;/TD&gt;
&lt;TD width="82" height="30"&gt;2019-01&lt;/TD&gt;
&lt;TD width="123" height="30"&gt;2019-01&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="47" height="30"&gt;3&lt;/TD&gt;
&lt;TD width="82" height="30"&gt;2019-02&lt;/TD&gt;
&lt;TD width="123" height="30"&gt;2019-02&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="47" height="30"&gt;3&lt;/TD&gt;
&lt;TD width="82" height="30"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="123" height="30"&gt;&lt;FONT color="#ff0000"&gt;2019-03&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="47" height="30"&gt;3&lt;/TD&gt;
&lt;TD width="82" height="30"&gt;2019-06&lt;/TD&gt;
&lt;TD width="123" height="30"&gt;2019-06&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="47" height="30"&gt;3&lt;/TD&gt;
&lt;TD width="82" height="30"&gt;2019-07&lt;/TD&gt;
&lt;TD width="123" height="30"&gt;2019-07&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="47" height="30"&gt;3&lt;/TD&gt;
&lt;TD width="82" height="30"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="123" height="30"&gt;&lt;FONT color="#ff0000"&gt;2019-08&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="47" height="30"&gt;3&lt;/TD&gt;
&lt;TD width="82" height="30"&gt;2019-11&lt;/TD&gt;
&lt;TD width="123" height="30"&gt;2019-11&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="47" height="30"&gt;3&lt;/TD&gt;
&lt;TD width="82" height="30"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="123" height="30"&gt;&lt;FONT color="#ff0000"&gt;2019-12&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;If they are non-continous&amp;nbsp;month, then just have that month and&amp;nbsp;its following month for the&amp;nbsp;each ID; if they are continuous months, then just have&amp;nbsp;those continuous&amp;nbsp;months and the following month of the last month.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you very much for any help!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best regards,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;C&lt;/P&gt;</description>
      <pubDate>Wed, 31 Mar 2021 22:24:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-preceding-month-or-following-month-value/m-p/730535#M227480</guid>
      <dc:creator>CynthiaWei</dc:creator>
      <dc:date>2021-03-31T22:24:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to get preceding month or following month value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-preceding-month-or-following-month-value/m-p/730540#M227483</link>
      <description>&lt;P&gt;MONTH and MONTH_PRECEDING have the same value for some rows?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 31 Mar 2021 22:53:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-preceding-month-or-following-month-value/m-p/730540#M227483</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-03-31T22:53:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to get preceding month or following month value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-preceding-month-or-following-month-value/m-p/730607#M227522</link>
      <description>&lt;P&gt;Is "month" a formatted sas date or a string?&lt;/P&gt;
&lt;P&gt;If it is not a date, fix that problem first.&lt;/P&gt;</description>
      <pubDate>Thu, 01 Apr 2021 06:28:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-preceding-month-or-following-month-value/m-p/730607#M227522</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2021-04-01T06:28:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to get preceding month or following month value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-preceding-month-or-following-month-value/m-p/730611#M227526</link>
      <description>&lt;P&gt;Use a "look-ahead":&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id $ monthstr :$7.;
format month yymmd7.;
month = input(monthstr!!"-01",yymmdd10.);
drop monthstr;
datalines;
1 2015-02
1 2015-10
2 2015-10
2 2019-01
2 2019-02
3 2019-01
3 2019-02
3 2019-06
3 2019-07
3 2019-11
;

data want;
merge
  have
  have (
    firstobs=2
    rename=(id=_id month=_month)
  )
;
format preceding_month yymmd7.;
preceding_month = month;
output;
if _id ne id or _month ne intnx('month',month,1,'s')
then do;
  month = .;
  preceding_month = intnx('month',preceding_month,1,'s');
  output;
end;
drop _:;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Please do always supply example data in a data step with datalines! Otherwise your helpers will have to do extra work, and make guesses about variable attributes and the real content.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Even idiots like myself manage to do it, so it should not be a problem for you.&lt;/P&gt;</description>
      <pubDate>Thu, 01 Apr 2021 06:53:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-preceding-month-or-following-month-value/m-p/730611#M227526</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-04-01T06:53:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to get preceding month or following month value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-preceding-month-or-following-month-value/m-p/730790#M227605</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for getting back to me! Sorry for the confusion. Actually, &lt;STRONG&gt;month_preceding should be called month_new&lt;/STRONG&gt; because it will still include the values in month&amp;nbsp;and have&amp;nbsp;one more preceding/following month. If&amp;nbsp; the months in&amp;nbsp;MONTH are not continuous (e.g. 2015-01 and 2019-03), then just have the month and&amp;nbsp;following month for each&amp;nbsp;observation in the&amp;nbsp;MONTH_NEW&amp;nbsp;(2015-01, 2015-02 and 2019-03, 2019-04). If the months in&amp;nbsp;MONTH are continuous (e.g. 2016-04, 2016-05, &amp;amp; 2016-06), then want to have these three&amp;nbsp;months and one following month of the last month (2016-06), which is 2016-07, in the MONTH_NEW (e.g. 2016-04, 2016-05, 2016-06, &amp;amp; 2016-07).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;</description>
      <pubDate>Thu, 01 Apr 2021 17:33:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-preceding-month-or-following-month-value/m-p/730790#M227605</guid>
      <dc:creator>CynthiaWei</dc:creator>
      <dc:date>2021-04-01T17:33:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to get preceding month or following month value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-preceding-month-or-following-month-value/m-p/730884#M227649</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you so much for the code! It worked good!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am sorry that I was using a remote version of SAS, the little running man icon wasn't showing up. I will try to post SAS code for the data next time! Thanks for reminding me that!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best regards,&lt;/P&gt;</description>
      <pubDate>Thu, 01 Apr 2021 23:45:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-preceding-month-or-following-month-value/m-p/730884#M227649</guid>
      <dc:creator>CynthiaWei</dc:creator>
      <dc:date>2021-04-01T23:45:07Z</dc:date>
    </item>
  </channel>
</rss>

