Solved
Contributor
Posts: 30

# Date Macro or Function

Hi Team,

I am working on automating some of the manual steps in a new team and in need of your SAS knowledge.

There is this particular date logic within SAS that my new team keeps changing every day.

Whilst I know how to determine the previous day and today's date, there is this other additional weekly change that I don't know how to do, hence I am here.

My new team have to change the reporting date as per follows:

1. Every Monday

'old date'  must be Thursday of last week and 'new date' must be Saturday of last week

2. Every Tuesday

'old date' must be Friday of last week and 'new date' must be Sunday of last week.

3. Every other day

'old date' = previous day

'new date' = today () minus 1

would there be a function that could do all  1, 2 & 3 ?

Mags

Accepted Solutions
Solution
‎06-11-2018 05:27 AM
Posts: 4,779

## Re: Date Macro or Function

@Timbim

I've missed that the example @Tom posted used the first (and wrong) logic to determine the old and new dates.

Still following what @Tom posted but amending to the final desired result below should do the job.

The first data step is just re-implementing Tom's sample code with the amended logic, the second data step is how you could implement this.

I've chosen this time a data NULL step to create the macro variables to avoid this sometimes hard-to-read spaghetti code when using %sysfunc().

``````data test ;
do today = '16APR2018'd to '20APR2018'd ;
new = intnx('weekday1w',today,-1) ;
old = intnx('weekday1w',intnx('weekday17w',today,-1),-1) ;
day1 = put(today,downame.-l);
day2 = put(old,downame.-l);
day3 = put(new,downame.-l);
output;
format today old new yymmdd10. ;
end;
run;
proc print; run;

data _null_;
format
executionDate previousExecutiondate
startDate endDate WEEKDATX.;

/*  executionDate='17APR2018'd;*/
executionDate         =today();
previousExecutiondate =intnx('weekday17w',executionDate,-1);

startDate =intnx('weekday1w',previousExecutiondate,-1);
endDate   =intnx('weekday1w',executionDate,-1);

put _all_;

call symputx('old',put(startDate,10.));
call symputx('new',put(endDate,10.));
stop;
run;
``````

All Replies
Posts: 3,257

## Re: Date Macro or Function

You want to use the WEEKDAY function to determine if the day of the week is Monday, Tuesday, etc.

From there, a series of IF statements ought to get the proper 'old date' and 'new date'.

--
Paige Miller
Super User
Posts: 10,534

## Re: Date Macro or Function

Since #1 and #2 have the same math, the statements are quite simple:

``````if weekday(date) in (2,3)
then do;
old_date = date - 4;
new_date = date - 2;
end;
else do;
old_date = date - 1;
new_date = today() - 1;
end;``````

if date = today() and weekday(date) neither a Monday nor a Tuesday, old_date and new_date would be identical.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 30

## Re: Date Macro or Function

Thanks Kurt. How can I write that in a Macro?

Kind regards,

Maggie

Super User
Posts: 10,534

## Re: Date Macro or Function

@Timbim wrote:

Thanks Kurt. How can I write that in a Macro?

Kind regards,

Maggie

Very simple: you don't. This is data handling, for which the data step is the tool of choice. If you need certain values later on in your program, use call symput() or call symputx() to store them in macro variables.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 30

## Re: Date Macro or Function

Hi Kurt, I tried your statements but it is not quiet giving me the output I am looking for.

It is an account level data and must only spit out 2 records of data for 1 account that must contain the ‘old date ‘ date and ‘new date’ data only
If the weekday is Monday , give me data for Thursday and Saturday, if the week day is Tuesday give me data for Friday and Sunday else give me yesterday and the previous day data.

Kind regards,
Mags

Super User
Posts: 10,534

## Re: Date Macro or Function

Please post some example data (see my footnote for this), and a matching example for the expected output (the expected output doesn't need to be in a data step).

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 30

## Re: Date Macro or Function

Hi Kurt,

Below is the current example and please note every Mondays, the 'old date' must be Thursdays and the 'new date' must be Saturdays. Every Tuesdays, the 'old date' must be Saturdays and 'new date' must be Mondays. Every other day from Wed-Friday 'old date' is today() minus 2 and 'new date' is today() minus 1.

* Date Comparisons;

%let old = '17Apr2018'd; ;*old date;

%let new = '18Apr2018'd; ;*new date

%_eg_conditional_dropds(WORK.DAILY_T);

PROC SQL;

CREATE TABLE WORK.DAILY_T AS SELECT

Date,

AccNo,

BranchNumber,

AccBal

FROM ACCOUNT_DAILY_TABLE

WHERE Date IN (&old, &new);

RUN;

I can easily use the intnx function to do Wed-Fri like below but having issue trying to figure out a way to do Mon and Tues.

For example:

Proc Sql;

Create Table DAILY_T as

Select Date,

(case

when Date = today()2 then 'Previous' else 'Current'

end) as Period,

AccNo,

BranchNumber,

AccBal

From ACCOUNT_DAILY_TABLE

where Date >=intnx ('day',today(),-2);

Quit;

if Monday, then 'old date' is Thur and 'new date' is Saturday

if Tuesday, the 'old date' is Saturday and 'new date' is Monday

else 'old date' is today() minus 2 and 'new date' is today() minus 1

----OUTPUT-----

The output has to have 2 records of the same account - 1 record for 'old date' and 1 record for 'new date'

Thanks kindly,

Mags

Super User
Posts: 10,534

## Re: Date Macro or Function

I see code, but no example data to apply it to. I need example data to play around with, posted in a data step (see second link in my footnote).

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 30

## Re: Date Macro or Function

Hi Kurt,

Below are dummy figures in the exact same format like the real data.

 Date AccNo BranchNumber AccBal 12-Apr-18 1234567 123 2345.87 13-Apr-18 1234567 123 2345.87 14-Apr-18 1234567 123 2345.87 15-Apr-18 1234567 123 2345.87 16-Apr-18 1234567 123 2345.87 17-Apr-18 1234567 123 2345.87 18-Apr-18 1234567 123 2345.87 12-Apr-18 3456789 456 5678.47 13-Apr-18 3456789 456 5678.47 14-Apr-18 3456789 456 5678.47 15-Apr-18 3456789 456 5678.47 16-Apr-18 3456789 456 5678.47 17-Apr-18 3456789 456 5678.47 18-Apr-18 3456789 456 5678.47

Thanks kindly,

Mags

Super User
Posts: 10,534

Posts: 4,779

## Re: Date Macro or Function

[ Edited ]

@Timbim

The required rule for date selections is not really clear to me. Can you please confirm or amend below assumption?

 Execution Day Start Day End Day Monday Thursday Saturday Tuesday Friday Sunday Wednesday Monday Tuesday Thursday Tuesday Wednesday Friday Wednesday Thursday

From what you describe I assume there is no execution on week ends.

You've also posted the following code:

`when Date = today() - 2 then 'Previous' else 'Current`

How does this look on a Monday (execution day). Are you also selecting Friday? And if so is Friday categorized as "Previous" or as "Current"?

I believe once we fully understand the selection logic and things really depend only on the execution date - today() - then some improved usage of the intnx() function will solve the problem.

Contributor
Posts: 30

## Re: Date Macro or Function

Hi Patrick,

The assumption you made on the table are correct expect for Tuesday:

Execution Day = Tuesday
Start Day = Saturday
Finish Day = Monday

For Monday:

Execution Day = Monday
Start Day = Thursday
Finish Day = Saturday

The code I posted can only give me current and previous day data of other days except for Monday and Tuesday. I need help with a logic for Monday and Tuesday.

To put in words:
If today is Monday, give me me Thursday and Saturday’s data
If today is Tuesday, give me Saturday and Monday’s days
else last 2 days data. I hope this makes sense.

Kind regards,
Mags

If Tu
Posts: 4,779

## Re: Date Macro or Function

[ Edited ]

@Timbim

"If today is Monday, give me me Thursday and Saturday’s data"

So also on Monday and Tuesday we're also selecting only 2 days? So on a Monday we're only selecting Thursday and Saturday and we skip Friday? Please confirm!

...and: I've amended my table based on how I understand you. What happens to Friday?

 Execution Date start date end date Monday Thursday Saturday Tuesday Saturday Monday Wednesday Monday Tuesday Thursday Tuesday Wednesday Friday Wednesday Thursday

Can you please just post above table with the right days?

Super User
Posts: 8,267

## Re: Date Macro or Function

You can do most of this with INTNX() function using the WEEKDAY interval and specifying the proper week-end days.  You will need to use different "week-end" days for the OLD and NEW date calculations.

``````old = intnx('weekday17w',today,-2) ;
new = intnx('weekday2w',today,-1) ;
``````

For the OLD date you are just treating Sunday (1) and Saturday (7) as the week-end days. So Monday - 2 > Thursday.

For the NEW data you only need to treat Monday (2) as the week-end day.

Example:

``````data test ;
do today = '16APR2018'd to '20APR2018'd ;
old = intnx('weekday17w',today,-2) ;
new = intnx('weekday2w',today,-1) ;
day1 = put(today,downame.-l);
day2 = put(old,downame.-l);
day3 = put(new,downame.-l);
output;
format today old new yymmdd10. ;
end;
run;
proc print; run;``````
```Obs         today           old           new    day1         day2         day3

1     2018-04-16    2018-04-12    2018-04-14    Monday       Thursday     Saturday
2     2018-04-17    2018-04-13    2018-04-15    Tuesday      Friday       Sunday
3     2018-04-18    2018-04-16    2018-04-17    Wednesday    Monday       Tuesday
4     2018-04-19    2018-04-17    2018-04-18    Thursday     Tuesday      Wednesday
5     2018-04-20    2018-04-18    2018-04-19    Friday       Wednesday    Thursday
```

☑ This topic is solved.