BookmarkSubscribeRSS Feed

%setdates macro - generates SAS date macro variables in multi-formats

Started ‎02-18-2022 by
Modified ‎02-18-2022 by
Views 3,989

See attached setdates.sas macro, which generates a large number of SAS global macro variables containing (day,wk,month,qtr,year) dates of differing formats (e.g. monyy7). Values are generated relative to current date i.e. day0=today, day1=yesterday, mon0=this month, mon1=last month, etc.

 

Example:

 

%put &mon1_start_date9;

 

resolves to 01JAN2022 (1st of previous month if current month is Feb2022) and:

 

%put &mon1_start_date9q;

resolves to '01JAN2022' (the above value in single quotes - useful for pass-thru SQL queries.

 

Can be overridden using:

 

%setdates(date=ddmonyy);

 

This is useful for program testing back and forward in time.

 

Particularly useful when added to the SAS root macro folder and invoked on any SAS session execution. Allows for standardisation across installations with hundreds of users.

 

sysdt macro values always refer to system date (not reference date).

 

 

davidlogan_0-1645172976282.png

 

---------------------

 

Having standardised, predictable, meaningful macro variables names is useful in a number of ways:

 

1) Looping around dates (see %tmp macro below).

2) Also using for e.g. &mon1_yyyy (which is the yyyy year of the previous month) means not having to write logic to cater for year changes when performing a Dec21 run in Jan22. 

3) Avoids users creating (and referencing macro variables with personal preference naming convention like 'mydate' which has little meaning.

4) Avoids sometimes convoluted date mathematics to get basic date parameters. When reading someone else's code this has to be first deconstructed to understand meaning.

 

%macro tmp;
    %do i=1 %to 10;
    %put &&mon&i._yyyymm;
    %end;
%mend;
%tmp;
202201
202112
202111
202110
202109
202108
202107
202106
202105
202104

 

 

Version history
Last update:
‎02-18-2022 08:12 AM
Updated by:

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

SAS AI and Machine Learning Courses

The rapid growth of AI technologies is driving an AI skills gap and demand for AI talent. Ready to grow your AI literacy? SAS offers free ways to get started for beginners, business leaders, and analytics professionals of all skill levels. Your future self will thank you.

Get started

Article Tags