Search code examples
datesasintervals

How do you find the first date of the week in SAS from a date?


I have a variable in a SAS dataset that has a number of dates (e.g. 01APR21). What I'm looking to do is create a new variable that shows the date of the first Monday of that week. So using the above example of 01APR21, the output would be 29/03/2021 as that what was when the Monday in that week was. I'm assuming it's using intnx, but I can't get my head around it.

data test;
format date date8.;
format first_day date10.;
date = '01APR21'd;
first_day = ?;
run;

Solution

  • INTNX Parameters:

    • Interval : WEEK
    • Increment: 0 (same week)
    • Alignment: Beginning (Sunday)

    Then add 1 to get to Monday instead of Sunday. You could probably play with the SHIFT INDEX parameter as well.

    Monday = intnx('week', dateVariable, 0, 'B') + 1