<?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 Need Help With Date Functions in ODS and Base Reporting</title>
    <link>https://communities.sas.com/t5/ODS-and-Base-Reporting/Need-Help-With-Date-Functions/m-p/3418#M1471</link>
    <description>Hi gyes&lt;BR /&gt;
&lt;BR /&gt;
1. If I want to get previous month, I do like this:&lt;BR /&gt;
&lt;B&gt;month(intnx('month', "&amp;amp;theDate."d, -1))&lt;/B&gt;&lt;BR /&gt;
So, how can I get previous month if instead &lt;I&gt;theDate&lt;/I&gt; I have month number (value from 1 to 12)?&lt;BR /&gt;
I made in this way:&lt;BR /&gt;
&lt;B&gt;month(intnx('month', mdy(&amp;amp;theMonth, 1, &amp;amp;theYear), -1))&lt;/B&gt;&lt;BR /&gt;
Is it right or there is special function to do this?&lt;BR /&gt;
&lt;BR /&gt;
2. Also I need to get previous year if it is January.&lt;BR /&gt;
How can I do this?&lt;BR /&gt;
&lt;BR /&gt;
Thanks.</description>
    <pubDate>Thu, 14 Jun 2007 11:03:34 GMT</pubDate>
    <dc:creator>deleted_user</dc:creator>
    <dc:date>2007-06-14T11:03:34Z</dc:date>
    <item>
      <title>Need Help With Date Functions</title>
      <link>https://communities.sas.com/t5/ODS-and-Base-Reporting/Need-Help-With-Date-Functions/m-p/3418#M1471</link>
      <description>Hi gyes&lt;BR /&gt;
&lt;BR /&gt;
1. If I want to get previous month, I do like this:&lt;BR /&gt;
&lt;B&gt;month(intnx('month', "&amp;amp;theDate."d, -1))&lt;/B&gt;&lt;BR /&gt;
So, how can I get previous month if instead &lt;I&gt;theDate&lt;/I&gt; I have month number (value from 1 to 12)?&lt;BR /&gt;
I made in this way:&lt;BR /&gt;
&lt;B&gt;month(intnx('month', mdy(&amp;amp;theMonth, 1, &amp;amp;theYear), -1))&lt;/B&gt;&lt;BR /&gt;
Is it right or there is special function to do this?&lt;BR /&gt;
&lt;BR /&gt;
2. Also I need to get previous year if it is January.&lt;BR /&gt;
How can I do this?&lt;BR /&gt;
&lt;BR /&gt;
Thanks.</description>
      <pubDate>Thu, 14 Jun 2007 11:03:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/ODS-and-Base-Reporting/Need-Help-With-Date-Functions/m-p/3418#M1471</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2007-06-14T11:03:34Z</dc:date>
    </item>
    <item>
      <title>Re: Need Help With Date Functions</title>
      <link>https://communities.sas.com/t5/ODS-and-Base-Reporting/Need-Help-With-Date-Functions/m-p/3419#M1472</link>
      <description>Hi,&lt;BR /&gt;
  If you already have a number from 1-12 as a month value, then simple subtraction would work for your programming problem without using INTNX. Consider the following code:&lt;BR /&gt;
[pre]&lt;BR /&gt;
options nodate nonumber;&lt;BR /&gt;
data testmon;&lt;BR /&gt;
  infile datalines;&lt;BR /&gt;
  input name $ bthmm bthyy;&lt;BR /&gt;
 &lt;BR /&gt;
  prevmon = bthmm - 1;&lt;BR /&gt;
  adjyr = bthyy;&lt;BR /&gt;
  if prevmon = 0 then do;&lt;BR /&gt;
     prevmon = 12;&lt;BR /&gt;
     adjyr = bthyy - 1;&lt;BR /&gt;
  end;&lt;BR /&gt;
  newdate = mdy(prevmon,1,adjyr);&lt;BR /&gt;
 &lt;BR /&gt;
  ** if you wanted to use intnx;&lt;BR /&gt;
  intmon = month(intnx('month', mdy(bthmm, 1, bthyy), -1));&lt;BR /&gt;
  intyy = year(intnx('month',mdy(bthmm,1,bthyy),-1));&lt;BR /&gt;
return;&lt;BR /&gt;
datalines;&lt;BR /&gt;
alice 11 1950&lt;BR /&gt;
bill   08 1951&lt;BR /&gt;
carol 01 2000&lt;BR /&gt;
dave 01 1986&lt;BR /&gt;
edna 12 2000&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
   &lt;BR /&gt;
proc print data=testmon;&lt;BR /&gt;
title 'started with month as a number';&lt;BR /&gt;
format newdate mmddyy10.;&lt;BR /&gt;
run;&lt;BR /&gt;
** program results are shown at the end of the posting;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
In this data, every person has a birth month (bthmm) and a birth year (bthyy), So the statement:&lt;BR /&gt;
&lt;B&gt;  prevmon = bthmm - 1;&lt;/B&gt;&lt;BR /&gt;
will net me the previous month, but, as you noted, someone born in January (1) will now have a previous month value of 0. So I can take care of both month and year in an IF statement:&lt;BR /&gt;
[pre]&lt;BR /&gt;
  if prevmon = 0 then do;&lt;BR /&gt;
     prevmon = 12;&lt;BR /&gt;
     adjyr = bthyy - 1;&lt;BR /&gt;
  end;&lt;BR /&gt;
  newdate = mdy(prevmon,1,adjyr);&lt;BR /&gt;
[/pre]&lt;BR /&gt;
 &lt;BR /&gt;
The advantage of INTNX is that it will do the adjustment for you. So if you wanted to stick with an INTNX method, you could do this:&lt;BR /&gt;
[pre]&lt;BR /&gt;
  intmon = month(intnx('month', mdy(bthmm, 1, bthyy), -1));&lt;BR /&gt;
  intyy = year(intnx('month',mdy(bthmm,1,bthyy),-1));&lt;BR /&gt;
[/pre]&lt;BR /&gt;
The first statement assigns a value to the INTMON variable based on using the MONTH function on the nested INTNX function. The second statement assigns a value to the INTYY variable based on using the YEAR function on the returned value from the INTNX function. Both the month AND the year are adjusted by the INTNX function.&lt;BR /&gt;
 &lt;BR /&gt;
But, if you are starting with a month value and a year value and not a SAS date value, then you are forcing the use of an extra function (the MDY inside the INTNX) in order to be able to use the YEAR or MONTH function on what was returned. In this instance, I would probably pick subtraction and an IF statement over INTNX because &lt;BR /&gt;
1) it's simpler &lt;BR /&gt;
2) it's easier to understand by a non-SAS programmer (like an auditor)&lt;BR /&gt;
3) it's easier to maintain.&lt;BR /&gt;
 &lt;BR /&gt;
  However, it is less elegant and takes more than one statement to put into effect. So if you needed to accomplish your setting of month in just one statement and your setting of year in just one statement, then INTNX would be the method, even if you do have some overhead in all the nested functions.&lt;BR /&gt;
 &lt;BR /&gt;
Good luck!&lt;BR /&gt;
cynthia&lt;BR /&gt;
Results of above program:&lt;BR /&gt;
[pre]&lt;BR /&gt;
                     started with month as a number&lt;BR /&gt;
&lt;BR /&gt;
Obs  name   bthmm   bthyy  prevmon  adjyr     newdate  intmon  intyy&lt;BR /&gt;
 1   alice    11     1950     10     1950  10/01/1950    10     1950&lt;BR /&gt;
 2   bill      8     1951      7     1951  07/01/1951     7     1951&lt;BR /&gt;
 3   carol     1     2000     12     1999  12/01/1999    12     1999&lt;BR /&gt;
 4   dave      1     1986     12     1985  12/01/1985    12     1985&lt;BR /&gt;
 5   edna     12     2000     11     2000  11/01/2000    11     2000&lt;BR /&gt;
[/pre]</description>
      <pubDate>Thu, 14 Jun 2007 16:04:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/ODS-and-Base-Reporting/Need-Help-With-Date-Functions/m-p/3419#M1472</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2007-06-14T16:04:04Z</dc:date>
    </item>
  </channel>
</rss>

