find the nth business day of a given month and year in SAS


/ Published in: SAS
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /* This code creates the macro variable DAY that is the nth day of the month.
  2.   The macro variable is in the SAS date format
  3. */
  4.  
  5. %macro test1(checkday,month,year);
  6. %global day;
  7. %let date=%str(%'01)%upcase(%substr(&month,1,3))&year%str(%'d);
  8. %put date=&date;
  9.  
  10. /*determine today's date
  11. %let date=%sysfunc(today());
  12. */
  13.  
  14. %let nthday=0;
  15. %let n=0;
  16. %do %until(&nthday=&checkday);
  17. /* create a variable whose value is the first day of the month*/
  18. %let begin_month=%sysfunc(intnx(month,&date,0,B));
  19. /* increment the date by 1 day at a time*/
  20. %let day=%sysfunc(intnx(day,&begin_month,&n));
  21. /* determine the day of the week*/
  22. %let weekday=%sysfunc(weekday(&day));
  23. /* if the day of the week is not Saturday or Sunday then increment
  24.   nthday plus 1*/
  25. %if &weekday ne 7 and &weekday ne 1 %then %let nthday=%eval(&nthday+1);
  26. %let n=%eval(&n+1);
  27. %end;
  28. /* checks the DAY macro variable by writing it to the log in the DATE9.
  29.   format*/
  30. %put %sysfunc(putn(&day,date9.));
  31. %mend;
  32.  
  33. %test1(5,SEP,2010)
  34.  
  35.  
  36. %macro test2(checkday,month,year););
  37. %global day;
  38. data test;
  39. date=input(compress('01'||substr("&month",1,3)||&year),date9.);
  40.  
  41. put date=;
  42. nthday=0;
  43. n=0;
  44. do until(nthday=&checkday);
  45. begin_month=intnx('month',date,0,'B');
  46. day=intnx('day',begin_month,n);
  47. weekday=weekday(day);
  48. if weekday ne 7 and weekday ne 1 then nthday=nthday+1;
  49. n+1;
  50. end;
  51. drop begin_month weekday date n;
  52. run;
  53.  
  54. proc print;
  55. format day date9.;
  56. run;
  57.  
  58. %mend;
  59.  
  60. %test2(8,april,2010)

URL: http://sastechies.blogspot.com/2009/12/finding-nth-business-day-of-month.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.