<?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: Days in Year Question in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Days-in-Year-Question/m-p/467450#M285339</link>
    <description>&lt;P&gt;Get the year from the date&lt;/P&gt;
&lt;P&gt;Check if last day in February is 28 or 29, if it's 29 it's a leap year and 365 days.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If it's not a leap year it's 366 days.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can also use the fact that if you take the mod(year, 4) and it's 0 it's a leap year otherwise it's not but there are exceptions to this rules so it depends on how you want to calculate it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
date='31Jan2020'd;
*check if leap year;
last_day_feb = day(intnx('month', mdy(2, 1, year(date)), 0, 'e'));
if last_day_feb=28 then nDays=365;
else if last_day_feb=29 then nDays=366;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 04 Jun 2018 16:00:40 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2018-06-04T16:00:40Z</dc:date>
    <item>
      <title>Days in Year Question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Days-in-Year-Question/m-p/467448#M285338</link>
      <description>&lt;P&gt;If I have a date variable (DATE_UTC), I need to create a variable that has the number of days in the year. Obviously this will be 365 most of the time, but for data normalization I need to know when it is 366 for leap year. This is probably easy, I tried to work from the number of days in a month:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;day(intnx('month',t1.DATE_UTC,0,'end'))&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But couldn't figure anything out. Is there a way to get the number of days in a year given a date?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Mon, 04 Jun 2018 15:55:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Days-in-Year-Question/m-p/467448#M285338</guid>
      <dc:creator>BCNAV</dc:creator>
      <dc:date>2018-06-04T15:55:13Z</dc:date>
    </item>
    <item>
      <title>Re: Days in Year Question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Days-in-Year-Question/m-p/467450#M285339</link>
      <description>&lt;P&gt;Get the year from the date&lt;/P&gt;
&lt;P&gt;Check if last day in February is 28 or 29, if it's 29 it's a leap year and 365 days.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If it's not a leap year it's 366 days.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can also use the fact that if you take the mod(year, 4) and it's 0 it's a leap year otherwise it's not but there are exceptions to this rules so it depends on how you want to calculate it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
date='31Jan2020'd;
*check if leap year;
last_day_feb = day(intnx('month', mdy(2, 1, year(date)), 0, 'e'));
if last_day_feb=28 then nDays=365;
else if last_day_feb=29 then nDays=366;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 04 Jun 2018 16:00:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Days-in-Year-Question/m-p/467450#M285339</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-06-04T16:00:40Z</dc:date>
    </item>
    <item>
      <title>Re: Days in Year Question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Days-in-Year-Question/m-p/467451#M285340</link>
      <description>&lt;P&gt;My preference:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;days_in_year = 1 + intnx('year', date_utc, 0, 'end') - intnx('year', date_utc, 0, 'begin');&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;*********EDITED:&amp;nbsp; Hold off on this one.&amp;nbsp; I'm getting strange results when I test it.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Jun 2018 16:06:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Days-in-Year-Question/m-p/467451#M285340</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-06-04T16:06:31Z</dc:date>
    </item>
    <item>
      <title>Re: Days in Year Question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Days-in-Year-Question/m-p/467453#M285341</link>
      <description>&lt;P&gt;You can also just subtract the beginning of year date from the end of year date. e.g.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  informat date_utc date9.;
  input date_utc;
  days_in_year=intnx('year',DATE_UTC,0,'e')-intnx('year',DATE_UTC,0,'b')+1;
  cards;
5dec2018
3mar2016
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Jun 2018 16:04:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Days-in-Year-Question/m-p/467453#M285341</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2018-06-04T16:04:50Z</dc:date>
    </item>
    <item>
      <title>Re: Days in Year Question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Days-in-Year-Question/m-p/467457#M285342</link>
      <description>&lt;P&gt;similar with &lt;STRONG&gt;intck&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
date='02jan2016'd;
days=intck('days',intnx('year',date,0,'b'), intnx('year',date,0,'e'))+1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 04 Jun 2018 16:11:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Days-in-Year-Question/m-p/467457#M285342</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-06-04T16:11:06Z</dc:date>
    </item>
    <item>
      <title>Re: Days in Year Question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Days-in-Year-Question/m-p/467458#M285343</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What is weird?&lt;/P&gt;</description>
      <pubDate>Mon, 04 Jun 2018 16:12:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Days-in-Year-Question/m-p/467458#M285343</guid>
      <dc:creator>BCNAV</dc:creator>
      <dc:date>2018-06-04T16:12:04Z</dc:date>
    </item>
    <item>
      <title>Re: Days in Year Question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Days-in-Year-Question/m-p/467460#M285344</link>
      <description>&lt;P&gt;Here's a test program, producing inconsistent results.&amp;nbsp; For INTNX, calculation always yields 365.&amp;nbsp; For MDY, it (properly) can calculate either 365 or 366:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;data _null_;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;do year = 2010 to 2018;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp; days = 1 + intnx('year', "&amp;amp;sysdate9"d, 0, 'e') - intnx('year', "&amp;amp;sysdate9"d, 0, 'b');&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp; begin = mdy(1, 1, year);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp; end = mdy(12, 31, year);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp; diff = end - begin + 1;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp; put year= days= begin= end= diff=;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;end;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;run;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;LI-WRAPPER&gt;&lt;/LI-WRAPPER&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can't explain it!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;EDITED: **************************** No problem.&amp;nbsp; I'm just using a constant date in INTNX instead of a date that varies by year.&amp;nbsp; The formula is fine.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Coffee anyone?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Jun 2018 16:17:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Days-in-Year-Question/m-p/467460#M285344</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-06-04T16:17:16Z</dc:date>
    </item>
    <item>
      <title>Re: Days in Year Question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Days-in-Year-Question/m-p/467461#M285345</link>
      <description>&lt;P&gt;Using the MOD function on a mathematical condition to find whether it's a leap year or not.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test ;
date='21AUG2016'D;
If (mod(year(date),4)=0 and mod(year(date),100)^=0) or mod(year(date),400)=0 then Days=366;
else Days=365;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 04 Jun 2018 16:18:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Days-in-Year-Question/m-p/467461#M285345</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2018-06-04T16:18:30Z</dc:date>
    </item>
    <item>
      <title>Re: Days in Year Question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Days-in-Year-Question/m-p/467468#M285346</link>
      <description>&lt;P&gt;I have checked your post from 1900 to 21000 and it picks them all...so I am not sure of your check?&lt;/P&gt;</description>
      <pubDate>Mon, 04 Jun 2018 16:47:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Days-in-Year-Question/m-p/467468#M285346</guid>
      <dc:creator>BCNAV</dc:creator>
      <dc:date>2018-06-04T16:47:48Z</dc:date>
    </item>
    <item>
      <title>Re: Days in Year Question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Days-in-Year-Question/m-p/467473#M285347</link>
      <description>&lt;P&gt;The formula is fine.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The test program is faulty.&amp;nbsp; Instead of using "&amp;amp;sysdate9"d I should have used a value that changes as YEAR changes.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Jun 2018 17:06:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Days-in-Year-Question/m-p/467473#M285347</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-06-04T17:06:29Z</dc:date>
    </item>
    <item>
      <title>Re: Days in Year Question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Days-in-Year-Question/m-p/467517#M285348</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/83078"&gt;@SuryaKiran&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;I also like MOD but coupled with the JULDATE function:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;   daysinyear= mod(juldate(intnx('year',date_utc,0,'e')),1000);
&lt;/PRE&gt;
&lt;P&gt;JULDATE returns a value like 18155 (year 2018, 155th day of the year).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Jun 2018 20:54:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Days-in-Year-Question/m-p/467517#M285348</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-06-04T20:54:03Z</dc:date>
    </item>
    <item>
      <title>Re: Days in Year Question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Days-in-Year-Question/m-p/467520#M285349</link>
      <description>&lt;P&gt;That's clever. And if you are doing it in macro code you can use the JULDATE7. format.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let year=2016 ;
%let days=%substr(%sysfunc(intnx(year,"01JAN&amp;amp;year"d,0,e),juldate7),5);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;3724  %put &amp;amp;=year &amp;amp;=days;
YEAR=2016 DAYS=366
&lt;/PRE&gt;</description>
      <pubDate>Mon, 04 Jun 2018 21:02:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Days-in-Year-Question/m-p/467520#M285349</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-06-04T21:02:01Z</dc:date>
    </item>
  </channel>
</rss>

