<?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 how to automatically find week number of the month in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-automatically-find-week-number-of-the-month/m-p/359839#M84645</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a code that's using week number of the month as constant and every time I have to change it manually.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;is there &amp;nbsp;a way to automate it, for example, it I am running the code today (may 18, 2017) then the value of week should automatically set to 3&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let week = 3;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please advise&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance&lt;/P&gt;</description>
    <pubDate>Thu, 18 May 2017 22:48:41 GMT</pubDate>
    <dc:creator>tparvaiz</dc:creator>
    <dc:date>2017-05-18T22:48:41Z</dc:date>
    <item>
      <title>how to automatically find week number of the month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-automatically-find-week-number-of-the-month/m-p/359839#M84645</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a code that's using week number of the month as constant and every time I have to change it manually.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;is there &amp;nbsp;a way to automate it, for example, it I am running the code today (may 18, 2017) then the value of week should automatically set to 3&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let week = 3;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please advise&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance&lt;/P&gt;</description>
      <pubDate>Thu, 18 May 2017 22:48:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-automatically-find-week-number-of-the-month/m-p/359839#M84645</guid>
      <dc:creator>tparvaiz</dc:creator>
      <dc:date>2017-05-18T22:48:41Z</dc:date>
    </item>
    <item>
      <title>Re: how to automatically find week number of the month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-automatically-find-week-number-of-the-month/m-p/359843#M84647</link>
      <description>&lt;P&gt;With week you have to be very explicit about how you define a week number.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you mean from day 1 to day 7 of the month regardless of which day of the week the month starts?&lt;/P&gt;
&lt;P&gt;If the day of the week it starts on is important then what day does the week start, Sunday, Monday, Saturday something else?&lt;/P&gt;
&lt;P&gt;If the week starts on sunday and the first day of the month is a saturday day which week does it belong to?&lt;/P&gt;
&lt;P&gt;Is there a minimum number of days to consider as a week?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that if May starts on a Friday then 18 May would be in a week with the 4th Friday of the month.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With all that in mind consider:&lt;/P&gt;
&lt;PRE&gt;data example;
   mydate = '18MAY2017'd;
   myweek = week(mydate) - week((intnx('month',mydate,0,'B'))) +1;
   put mydate= date9. myweek=;
run;&lt;/PRE&gt;
&lt;P&gt;The +1 above is needed because the subtraction (or the intck WEEK function) returns intervals and you want the number.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 May 2017 22:58:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-automatically-find-week-number-of-the-month/m-p/359843#M84647</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-05-18T22:58:49Z</dc:date>
    </item>
    <item>
      <title>Re: how to automatically find week number of the month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-automatically-find-week-number-of-the-month/m-p/359845#M84648</link>
      <description>&lt;P&gt;I tried following&amp;nbsp;and it failed&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let week = week(today()) - week((intnx('month',today(),0,'B'))) +1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;maybe its not the way to define a variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 May 2017 23:10:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-automatically-find-week-number-of-the-month/m-p/359845#M84648</guid>
      <dc:creator>tparvaiz</dc:creator>
      <dc:date>2017-05-18T23:10:45Z</dc:date>
    </item>
    <item>
      <title>Re: how to automatically find week number of the month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-automatically-find-week-number-of-the-month/m-p/359850#M84651</link>
      <description>&lt;P&gt;use the data step and call symputx if you must have a macro variable.&lt;/P&gt;
&lt;P&gt;If you don't know the proper syntax to call datastep functions such as WEEK, INTNX then time to review basic macro language.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since the "macro" version for this&lt;/P&gt;
&lt;P&gt;%let week = week(today()) - week((intnx('month',today(),0,'B'))) +1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;would require using &lt;STRONG&gt;&lt;FONT color="#ff0000" size="4"&gt;5&lt;/FONT&gt;&lt;/STRONG&gt; calls to the macro function %sysfunc() and nested up to 3 levels that line would get VERY UGLY and getting all the () to match is a headach.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Easier&lt;/P&gt;
&lt;P&gt;data _null_;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; week = week(today()) - week((intnx('month',today(),0,'B'))) +1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; call symputx('week',week);&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;Plus if end up needing other moderately complicated macro variables built then they may all go into the same data step.&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2017 14:31:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-automatically-find-week-number-of-the-month/m-p/359850#M84651</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-05-19T14:31:11Z</dc:date>
    </item>
    <item>
      <title>Re: how to automatically find week number of the month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-automatically-find-week-number-of-the-month/m-p/359860#M84653</link>
      <description>&lt;P&gt;Remember macro language %EVAL function performs integer arithmetic&amp;nbsp;automatically. &amp;nbsp;Have you tried:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let week = %eval(("&amp;amp;sysdate9"d + 6) / 7);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think +6 in the formula is right, but you might need to play with it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And I can't test right now whether %EVAL will recognize a date literal, but it might.&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2017 00:09:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-automatically-find-week-number-of-the-month/m-p/359860#M84653</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-05-19T00:09:21Z</dc:date>
    </item>
    <item>
      <title>Re: how to automatically find week number of the month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-automatically-find-week-number-of-the-month/m-p/359871#M84654</link>
      <description>&lt;P&gt;If you want macro code only,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%put week = %eval( %sysfunc(week(%sysfunc(today()))) + 1  
                 - %sysfunc(week(%sysfunc(intnx(month,%sysfunc(today()),0,B)))) );
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2017 03:30:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-automatically-find-week-number-of-the-month/m-p/359871#M84654</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-05-19T03:30:42Z</dc:date>
    </item>
    <item>
      <title>Re: how to automatically find week number of the month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-automatically-find-week-number-of-the-month/m-p/359885#M84656</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/22922"&gt;@tparvaiz&lt;/a&gt;&amp;nbsp;You never stated how you define a week.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One quick and dirty way is to take the day of month and divide by 7 and either round it up or down depending on your rules.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
    day=day(today());
    week_ceil=ceil(day/7);
    week_floor=floor(day/7);
    call symputx('week_ceil', week_ceil);
    call symputx('week_floor', week_floor);
run;

%put Week (Rounded up): &amp;amp;week_ceil.;
%put Week (Rounded down): &amp;amp;week_floor.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;OUTPUT:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; 65         
 66         %put Week (Rounded up): &amp;amp;week_ceil.;
 Week (Rounded up): 3
 67         %put Week (Rounded down): &amp;amp;week_floor.;
 Week (Rounded down): 2&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 19 May 2017 06:13:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-automatically-find-week-number-of-the-month/m-p/359885#M84656</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-05-19T06:13:21Z</dc:date>
    </item>
    <item>
      <title>Re: how to automatically find week number of the month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-automatically-find-week-number-of-the-month/m-p/359894#M84660</link>
      <description>&lt;P&gt;I concur with what the others said.&lt;/P&gt;
&lt;P&gt;NEVER, EVER do such a complex calculation directly in the %let macro statement. Use a data _null_ step and call symput or call symputx.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Part of Maxim 11.&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2017 07:54:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-automatically-find-week-number-of-the-month/m-p/359894#M84660</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-05-19T07:54:24Z</dc:date>
    </item>
    <item>
      <title>Re: how to automatically find week number of the month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-automatically-find-week-number-of-the-month/m-p/359902#M84661</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp; I disagree. Purely macro routines, without any L4G, can be called anywhere and are more useful than routines containing statements. Hence my for example this &lt;A href="https://communities.sas.com/t5/SASware-Ballot-Ideas/New-functions-to-access-data-sets/idi-p/220486" target="_self"&gt;request&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2017 08:28:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-automatically-find-week-number-of-the-month/m-p/359902#M84661</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-05-19T08:28:24Z</dc:date>
    </item>
    <item>
      <title>Re: how to automatically find week number of the month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-automatically-find-week-number-of-the-month/m-p/359967#M84673</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let week=%eval(%sysfunc(intck(week,%sysfunc(intnx(month,%sysfunc(today()),0)) ,%sysfunc(today())))+1);
%put &amp;amp;week ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 19 May 2017 13:40:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-automatically-find-week-number-of-the-month/m-p/359967#M84673</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-05-19T13:40:51Z</dc:date>
    </item>
  </channel>
</rss>

