BookmarkSubscribeRSS Feed
Mirisage
Obsidian | Level 7

Hi community,

I have to run a same SAS program everyday having macro definitions like below.

%let CDATE=May08; (lets call this eq. 1)

%let PDATE=May07; (lets call this eq. 2)

Then macro variables named &CDATE and &PDATE  are used in several places across the SAS program.

Right hand side of eq. 1 refers to yesterday.

Right hand side of eq. 2 refers to day before yesterday.

I am manually changing the right hand side of the eq. 1 and eq. 2 everyday.

I have a feeling that %sysdate automatic macro variable can be used to automate the process but fear it might upset everything in my organization.

Any help to automate the process is highly appreciated.

Mirisage

8 REPLIES 8
Reeza
Super User

There's %sysdate and today, the problem is there isn't a predefined format that outputs the date  as monday. Do you always use a 3 letter month, is September 7, 2012 Sep07 or September07?

Would writing your own format be an acceptable solution and use that in combination with sysfunc?

%let cdate=%sysfunc(today(), worddate12.);

%put cdate;

Astounding
PROC Star

Reeza's question is an important one ... what do you want to do when the date is in September instead of May.

Here are some recommendations.

First, leave these statements in the program:

%let CDATE=;

%let PDATE=;

Then add a DATA step to re-assign values.  The DATA step can easily check the values of &CDATE, &PDATE and &SYSDATE9, and act accordingly.  Some code snippets appear below.  It will be much easier to read the code in the DATA step, rather than nesting multiple %SYSFUNCs if you try to do it all in macro language.

One reason for this approach is that you will one day have a backup in your production process.  You may have to backdate the report instead of relying on using the previous day.  In that case, assign values in the %LET statements.  But the subsequent DATA step can remain in the program as is.  It can be programmed to leave &CDATE and &PDATE unchanged if they already have assigned values.

OK, some code snippets.

%let CDATE=;

%let PDATE=;

data _null_;

if "&CDATE"=' ';

cdate = put("&sysdate9"d - 1, worddate18.);

pdate = put("&sysdate9"d - 2, worddate18.);

cdate = /* use DATA step functions to pull out the proper pieces of CDATE */;

call symputx('cdate', cdate);

** same for pdate;

run;

Good luck.

Reeza
Super User

I prefer today() rather than %sysdate, because %sysdate doesn't update if you don't restart your SAS session overnight.

Depends on how you run jobs and where your computer is I suppose.

The following code is based on Astounding's suggested method, which I like Smiley Happy

data _null_;

c_date = today()-1;

p_date = today()-2;

cdate = put(c_date, monname9.)||put(day(c_date), z2.);

pdate = put(p_date, monname9.)||put(day(p_date), z2.);

call symputx('cdate', cdate);

call symputx('pdate', pdate);

** same for pdate;

run;

%put &cdate.;

%put &pdate.;

art297
Opal | Level 21

Given your example, it wasn't clear whether you want a 3 character month.  Regardless, you can do it all with %let statements.  E.g.,

%let today=%sysfunc(today());

%let cdate = %sysfunc(putn(&today.-1, monname3.))%sysfunc(putn(%sysfunc(day(&today.))-1, z2.));

%let pdate = %sysfunc(putn(&today.-2, monname3.))%sysfunc(putn(%sysfunc(day(&today.))-2, z2.));

%put &cdate;

%put &pdate;

Mirisage
Obsidian | Level 7

Hi Reeza, Astounding and Art,

Thank you very much for all three of you.

This is a tremendous knowledge.

Thanks again!

Best regards

Mirisage

Astounding
PROC Star

Art, nice job.  It's easy enough to read, and it still meets my added condition ... if the program ever needs to be backdated, the changes are limited to just a single line in the program.

mrgibson
Calcite | Level 5

What if today is a monday, do you really want yesterday (sunday) on your report or the previous work day (friday)?

You can always use the INTNX date function to move back to the last busness day.

such as:

lstyr = intnx('WEEKDAY',today());

There are a bunch of formats and functions that work with SAS dates.

Mirisage
Obsidian | Level 7

Hi Mrgibson,

You are very correct. Actually I was thinking how to incorporate last business day, because I needed the last business day, and not just yesterday.

Your input is certainly helpful for this.

Thank you again.

Mirisage

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 8 replies
  • 2701 views
  • 0 likes
  • 5 in conversation