Advertisement
OrphansVendetta

DATES cheat sheet

Nov 14th, 2022
824
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SAS 5.01 KB | None | 0 0
  1. /**********************************************
  2. A rundown of using %sysfunc to always have the
  3. date needed. This might be able to help eliminate
  4. hard coding dates into reports.
  5. **********************************************/
  6.  
  7. /*Basic Day Functions*/
  8. %let today=%sysfunc(today(), date9.); %put &today.;
  9. %let yesterday =  %sysfunc(intnx(day,%sysfunc(today()),-1),date9.); %put &yesterday.;
  10. %let tomorrow =  %sysfunc(intnx(day,%sysfunc(today()),+1),date9.); %put &tomorrow.;
  11.  
  12.  
  13.  
  14. /*Weekly Functions*/
  15. %let week_from_today =  %sysfunc(intnx(day,%sysfunc(today()),+7),date9.); %put &week_from_today.;
  16. %let week_ago_from_today =  %sysfunc(intnx(day,%sysfunc(today()),-7),date9.); %put &week_ago_from_today.;
  17. %let week_ago_yesterday =  %sysfunc(intnx(day,%sysfunc(today()),-8),date9.); %put &week_ago_yesterday.;
  18.  
  19. /*if you run previous'day of the week' on that day, it will return today()*/
  20. %let Previous_Sunday = %sysfunc(intnx(week1.1,%sysfunc(today()),0),date9.); %put &Previous_Sunday.;
  21. %let Two_Sundays_Ago = %sysfunc(intnx(week1.1,%sysfunc(today()),-1),date9.); %put &Two_Sundays_Ago.;
  22. %let Next_Sunday = %sysfunc(intnx(week1.1,%sysfunc(today()),+1),date9.); %put &Next_Sunday.;
  23.  
  24. %let Previous_Monday = %sysfunc(intnx(week1.2,%sysfunc(today()),0),date9.); %put &Previous_Monday.;
  25. %let Two_Mondays_Ago = %sysfunc(intnx(week1.2,%sysfunc(today()),-1),date9.); %put &Two_Mondays_Ago.;
  26. %let Next_Monday = %sysfunc(intnx(week1.2,%sysfunc(today()),+1),date9.); %put &Next_Monday.;
  27.  
  28. %let Previous_Tuesday = %sysfunc(intnx(week1.3,%sysfunc(today()),0),date9.); %put &Previous_Tuesday.;
  29. %let Two_Tuesdays_Ago = %sysfunc(intnx(week1.3,%sysfunc(today()),-1),date9.); %put &Two_Tuesdays_Ago.;
  30. %let Next_Tuesday = %sysfunc(intnx(week1.3,%sysfunc(today()),+1),date9.); %put &Next_Tuesday.;
  31.  
  32. %let Previous_Wednesday = %sysfunc(intnx(week1.4,%sysfunc(today()),0),date9.); %put &Previous_Wednesday.;
  33. %let Two_Wednesdays_Ago = %sysfunc(intnx(week1.4,%sysfunc(today()),-1),date9.); %put &Two_Wednesdays_Ago.;
  34. %let Next_Wednesday = %sysfunc(intnx(week1.4,%sysfunc(today()),+1),date9.); %put &Next_Wednesday.;
  35.  
  36. %let Previous_Thursday = %sysfunc(intnx(week1.5,%sysfunc(today()),0),date9.); %put &Previous_Thursday.;
  37. %let Two_Thursdays_Ago = %sysfunc(intnx(week1.5,%sysfunc(today()),-1),date9.); %put &Two_Thursdays_Ago.;
  38. %let Next_Thursday = %sysfunc(intnx(week1.5,%sysfunc(today()),+1),date9.); %put &Next_Thursday.;
  39.  
  40. %let Previous_Friday = %sysfunc(intnx(week1.6,%sysfunc(today()),0),date9.); %put &Previous_Friday.;
  41. %let Two_Fridays_Ago = %sysfunc(intnx(week1.6,%sysfunc(today()),-1),date9.); %put &Two_Fridays_Ago.;
  42. %let Next_Friday = %sysfunc(intnx(week1.6,%sysfunc(today()),+1),date9.); %put &Next_Friday.;
  43.  
  44. %let Previous_Saturday = %sysfunc(intnx(week1.7,%sysfunc(today()),0),date9.); %put &Previous_Saturday.;
  45. %let Two_Saturdays_Ago = %sysfunc(intnx(week1.7,%sysfunc(today()),-1),date9.); %put &Two_Saturdays_Ago.;
  46. %let Next_Saturday = %sysfunc(intnx(week1.7,%sysfunc(today()),+1),date9.); %put &Next_Saturday.;
  47.  
  48.  
  49. /*example*/
  50. PROC SQL;
  51. CREATE TABLE temp AS
  52. SELECT
  53.     count(*)
  54. FROM table_i_want
  55. WHERE
  56.     date_field BETWEEN "&week_ago_yesterday."d AND "&yesterday."d
  57. ;
  58. run;
  59.  
  60.  
  61. /*Monthly Functions*/
  62. %let First_of_curMth = %sysfunc(intnx(month,%sysfunc(today()),0),date9.); %put &First_of_curMth.;
  63. %let Last_day_of_curMth = %sysfunc(intnx(month,%sysfunc(today()),0, end),date9.); %put &Last_day_of_curMth.;
  64.  
  65. %let First_of_preMth = %sysfunc(intnx(month,%sysfunc(today()),-1),date9.); %put &First_of_preMth.;
  66. %let Last_day_of_preMth = %sysfunc(intnx(month,%sysfunc(today()),-1, end),date9.); %put &Last_day_of_preMth.;
  67.  
  68. %let First_of_nextMth = %sysfunc(intnx(month,%sysfunc(today()),+1),date9.); %put &First_of_nextMth.;
  69. %let Last_day_of_nextMth = %sysfunc(intnx(month,%sysfunc(today()),+1, end),date9.); %put &Last_day_of_nextMth.;
  70.  
  71.  
  72. /*Quarterly Functions*/
  73.  
  74. %let First_of_curQtr = %sysfunc(intnx(qtr,%sysfunc(today()),0),date9.); %put &First_of_curQtr.;
  75. %let Last_day_of_curQtr = %sysfunc(intnx(qtr,%sysfunc(today()),0, end),date9.); %put &Last_day_of_curQtr.;
  76.  
  77. %let First_of_preQtr = %sysfunc(intnx(qtr,%sysfunc(today()),-1),date9.); %put &First_of_preQtr.;
  78. %let Last_day_of_preQtr = %sysfunc(intnx(qtr,%sysfunc(today()),-1, end),date9.); %put &Last_day_of_preQtr.;
  79.  
  80. %let First_of_nextQtr = %sysfunc(intnx(qtr,%sysfunc(today()),+1),date9.); %put &First_of_nextQtr.;
  81. %let Last_day_of_nextQtr = %sysfunc(intnx(qtr,%sysfunc(today()),+1, end),date9.); %put &Last_day_of_nextQtr.;
  82.  
  83.  
  84.  
  85.  
  86. /**********************************************
  87. Change the arguments around to get the exact
  88. date frame needed.
  89.  
  90. EXAMPLE
  91.  
  92. Each quarter a report is needed to complie
  93. the previous two quarters.
  94. **********************************************/
  95.  
  96. %let First_day_two_qtrs_ago = %sysfunc(intnx(qtr,%sysfunc(today()),-2),date9.); %put &First_day_two_qtrs_ago.;
  97.  
  98.  
  99.  
  100. PROC SQL;
  101. CREATE TABLE previous_two_quarters_report AS
  102. SELECT
  103.     *
  104. FROM Database_table
  105. WHERE
  106.     date_field BETWEEN "&First_day_two_qtrs_ago."d AND "&Last_day_of_preQtr."d
  107. ;
  108. run;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement