<?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: Specifying a start date for the week function  (sas 9.4) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Specifying-a-start-date-for-the-week-function-sas-9-4/m-p/917275#M361320</link>
    <description>&lt;P&gt;So your year always starts at the beginning of July.&amp;nbsp; And you want to generate the week number of your date (variable MYDATE) versus the start of the most recent July.&amp;nbsp; For each date you could do a two line code:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Get the date value for the most recent July 1:&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  year_beg_date=intnx('year.7',date,0,'beg');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI&gt;
&lt;LI&gt;Count the number of weeks since that date (and add 1, so you don't start with week zero):&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;weeknum=intck('week',year_beg_date,mydate)+1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Or in a single line:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  weeknum=intck('week',intnx('year.7',mydate,0,'beg'),mydate)+1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The possible problem in the above is that you have to accept that each week starts with Sunday.&amp;nbsp; So if a July 1st occurs on Saturday, then July 2nd, being a Sunday, would be in week 2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want count weeks as starting on the weekday of July 1st, then a simple subtraction&amp;nbsp; of MYDATE minus the most recent June 30th, then divided by 7 and rounded up:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  weeknum=ceil((mydate-intnx('year.7',mydate,-1,'end'))/7)  ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 21 Feb 2024 23:34:58 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2024-02-21T23:34:58Z</dc:date>
    <item>
      <title>Specifying a start date for the week function  (sas 9.4)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Specifying-a-start-date-for-the-week-function-sas-9-4/m-p/917229#M361310</link>
      <description>&lt;P&gt;Is there a way to specify a different start date so that week 1 would = the first week in July of each year instead of the first week in January?&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data My_data;
 set data;
week=week(Paydt);
 run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thanks!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Feb 2024 19:59:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Specifying-a-start-date-for-the-week-function-sas-9-4/m-p/917229#M361310</guid>
      <dc:creator>Whitlea</dc:creator>
      <dc:date>2024-02-21T19:59:18Z</dc:date>
    </item>
    <item>
      <title>Re: Specifying a start date for the week function  (sas 9.4)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Specifying-a-start-date-for-the-week-function-sas-9-4/m-p/917258#M361318</link>
      <description>&lt;P&gt;"Week" is a pretty flexible term, which is why the default SAS WEEK function has three optional parameters to work with start of year.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You&amp;nbsp; can use INTCK function to get the number of intervals between two dates:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;week = intck('week','01JUL2023'd,paydt,'C')&lt;/PRE&gt;
&lt;P&gt;If you have many years of data to do this with you would have to adjust the start date.&lt;/P&gt;
&lt;P&gt;Provide some examples as needed.&lt;/P&gt;
&lt;P&gt;Note the INTCK function above will assume Saturday is the first day of the week. If that data not meet you need then provide more details.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Feb 2024 21:41:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Specifying-a-start-date-for-the-week-function-sas-9-4/m-p/917258#M361318</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-02-21T21:41:02Z</dc:date>
    </item>
    <item>
      <title>Re: Specifying a start date for the week function  (sas 9.4)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Specifying-a-start-date-for-the-week-function-sas-9-4/m-p/917275#M361320</link>
      <description>&lt;P&gt;So your year always starts at the beginning of July.&amp;nbsp; And you want to generate the week number of your date (variable MYDATE) versus the start of the most recent July.&amp;nbsp; For each date you could do a two line code:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Get the date value for the most recent July 1:&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  year_beg_date=intnx('year.7',date,0,'beg');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI&gt;
&lt;LI&gt;Count the number of weeks since that date (and add 1, so you don't start with week zero):&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;weeknum=intck('week',year_beg_date,mydate)+1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Or in a single line:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  weeknum=intck('week',intnx('year.7',mydate,0,'beg'),mydate)+1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The possible problem in the above is that you have to accept that each week starts with Sunday.&amp;nbsp; So if a July 1st occurs on Saturday, then July 2nd, being a Sunday, would be in week 2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want count weeks as starting on the weekday of July 1st, then a simple subtraction&amp;nbsp; of MYDATE minus the most recent June 30th, then divided by 7 and rounded up:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  weeknum=ceil((mydate-intnx('year.7',mydate,-1,'end'))/7)  ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Feb 2024 23:34:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Specifying-a-start-date-for-the-week-function-sas-9-4/m-p/917275#M361320</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-02-21T23:34:58Z</dc:date>
    </item>
    <item>
      <title>Re: Specifying a start date for the week function  (sas 9.4)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Specifying-a-start-date-for-the-week-function-sas-9-4/m-p/917329#M361353</link>
      <description>&lt;P&gt;Another approach would be to use the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/n1ka2ulrvrjlasn0z7beco2yrgas.htm" target="_blank" rel="noopener"&gt;WEEK function&lt;/A&gt; with a suitable offset. Suppose you want to mimic week numbers like &lt;FONT face="courier new,courier"&gt;WEEK(&lt;EM&gt;date&lt;/EM&gt;, 'V')&lt;/FONT&gt;, but with July 1st playing the role of January 1st. Then you could define the offset as&lt;/P&gt;
&lt;PRE&gt;%let d=%sysevalf('01JUL2023'd-'01JAN2000'd);&lt;/PRE&gt;
&lt;P&gt;and compute the week number &lt;FONT face="courier new,courier"&gt;w&lt;/FONT&gt;&amp;nbsp;for a &lt;FONT face="courier new,courier"&gt;date&lt;/FONT&gt; as&lt;/P&gt;
&lt;PRE&gt;w=week(date-&amp;amp;d,'V');&lt;/PRE&gt;
&lt;P&gt;The rationale of the offset definition is: 01JUL2023 and 01JAN2000 were the same day of the week (Saturday) &lt;EM&gt;and&lt;/EM&gt; the 12 months following these two dates were leap years (i.e., contained February 29th), so the definition of the week number is consistent for several decades before and after 01JUL2023.&lt;/P&gt;</description>
      <pubDate>Thu, 22 Feb 2024 10:34:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Specifying-a-start-date-for-the-week-function-sas-9-4/m-p/917329#M361353</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2024-02-22T10:34:33Z</dc:date>
    </item>
  </channel>
</rss>

