BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
davidlogan
Fluorite | Level 6

Over the years I've seen a variety of useful SAS macros, some more general solution than others, but I don't think there's a central searchable location where they can be posted to. 

 

Q - Is there a recognised/searchable location for general purpose SAS Macros like this one?

 

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

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)

 

%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-1645007158798.png

 

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

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

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 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

 

1 ACCEPTED SOLUTION
4 REPLIES 4
davidlogan
Fluorite | Level 6

Hi Reeza,

 

This look like a more suitable location, thanks! looks like SAS doesn't consider me experienced enough yet though 🙂

 

davidlogan_0-1645099036583.png

 

Quentin
Super User

Q - Is there a recognised/searchable location for general purpose SAS Macros like this one?

 

I'd say no.  A lot of individuals and groups have developed and shared their macro libraries either on websites, or github, or via user group papers.  But it sounds like you're hoping for something like https://cran.r-project.org/ or https://pypi.org/ where there would be an organization reviewing/curating macros and providing a searchable interface.  I don't think there's anything like that for the SAS community.

 

If you spend some time googling you'll find plenty of SAS macro definitions, but the onus is on you to separate the wheat from the chaff. 

 

BTW, if it's possible to edit the subject of a question, I would suggest editing this one, as the subject "SAS macro %setdates - Generates SAS date macro variables in multi-formats" makes it  sound like the question is about a particular macro.

 

BASUG is hosting free webinars Next up: Mark Keintz presenting History Carried Forward, Future Carried Back: Mixing Time Series of Differing Frequencies on May 8. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
davidlogan
Fluorite | Level 6

Hi Quentin,

 

Thanks for this, googling does indeed produce plenty of SAS macro definitions, with the onus on you to separate the wheat from the chaff, hence my question (within the topic) about if there is a recognised repository.

 

I was posting the macro as a something which other users might find useful, so it wasn't actually a question (despite being in the questions section), although it appears to be the only place I could post something like this

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 883 views
  • 7 likes
  • 3 in conversation