DATA Step, Macro, Functions and more

macro variable with value of date of today (for example: 10JUL2018 )

Reply
Frequent Contributor
Posts: 142

macro variable with value of date of today (for example: 10JUL2018 )

Hello

I want to create a sas macro variable with value of date of today (for example:  10JUL2018 )

 

What is wrong with this code?

%let d_today=%sysfunc(PUT(today(),date9.));
%put &d_today.;

Respected Advisor
Posts: 3,057

Re: macro variable with value of date of today (for example: 10JUL2018 )

%let d_today=%sysfunc(putn(%sysfunc(today()),date9.));

Just a word of advice. If this macro variable is destined to be used in a title or a label, then formatting it via date9. (or any other format) is meaningful. If it is supposed to be used in code, for example in SQL where you want dates in a certain range, or anywhere else in code, don't format it, that's just a wasted step.

--
Paige Miller
Frequent Contributor
Posts: 142

Re: macro variable with value of date of today (for example: 10JUL2018 )

Posted in reply to PaigeMiller

Hello

Thanks a lot

 

 

I need also to create a parameter of 1 year before yeaterday

Why is it not working?

 

%let gama=%sysfunc(putn(%sysfunc(intnx('MONTH',today()-1,-1,'s')),date9.));
%put &gama.;

 

Thanks

Royein

Super User
Posts: 10,279

Re: macro variable with value of date of today (for example: 10JUL2018 )


@Ronein wrote:

Hello

Thanks a lot

 

 

I need also to create a parameter of 1 year before yeaterday

Why is it not working?

 

%let gama=%sysfunc(putn(%sysfunc(intnx('MONTH',today()-1,-1,'s')),date9.));
%put &gama.;

 

Thanks

Royein


Since the macro processor is a TEXT processor, quotes are not needed and in fact wrong. And you need to use %eval for calculations.

%let gama=%sysfunc(putn(%sysfunc(intnx(year,%eval(%sysfunc(today())-1),-1,s)),date9.));
%put &gama.;

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Super User
Posts: 8,120

Re: macro variable with value of date of today (for example: 10JUL2018 )

[ Edited ]

@Ronein wrote:

Hello

Thanks a lot

 

 

I need also to create a parameter of 1 year before yeaterday

Why is it not working?

 

%let gama=%sysfunc(putn(%sysfunc(intnx('MONTH',today()-1,-1,'s')),date9.));
%put &gama.;

 

Thanks

Royein


Here is what you need.

%let gama=%sysfunc(intnx(month,%sysfunc(today())-1,-1,s),date9.);
%put &gama.;

Let's look at it from the outside in.

1) Call the %SYSFUNC() macro function to access the INTNX() function and format the resulting value using the DATE9. format.

2) For the INTNX() function call using the MONTH interval starting with the day before today and going back one month using the same same day of the month.

3) Use the %SYSFUNC() macro function to access the TODAY() function and return the result using defualt BEST format.

 

If you look at your call you will see that you tried to use the TODAY() function without wrapping it into the %SYSFUNC() macro function. You also told the INTNX() function to use the 'MONTH' interval instead of the MONTH interval.  In macro coding you do not need to add quotes around strings. Everything is a string to macro code.  It is the & and % triggers that cause the macro processor to operate on the code.

Super User
Super User
Posts: 8,120

Re: macro variable with value of date of today (for example: 10JUL2018 )


@Ronein wrote:

Hello

I want to create a sas macro variable with value of date of today (for example:  10JUL2018 )

 

What is wrong with this code?

%let d_today=%sysfunc(PUT(today(),date9.));
%put &d_today.;


Two things. First you cannot use PUT() function with %SYSFUNC(). You need to use either PUTN() or PUTC() depending of the type of variable being formatted.  Second you need to use %SYSFUNC() with the TODAY() function (a.k.a. the DATE() function).

 

You can eliminate one of the %SYSFUNC() calls by using the format parameter to %SYSFUNC() instead of calling PUTN().

%let d_today=%sysfunc(date(),date9.);
%let now=%sysfunc(datetime(),datetime20.);
Frequent Contributor
Posts: 142

Re: macro variable with value of date of today (for example: 10JUL2018 )

Thank you very much.

 

%let d_today1=%sysfunc(today());
%put &d_today1.;/*21375*/

 

%let d_today2=%sysfunc(today(),date9.);
%put &d_today2.;/*10JUL2018*/

 

 

%let d_yesterday2=%sysfunc((today()-1),date9.);
%put &d_yesterday2.;/*error*/

Where  calculation of &d_yesterday2. is not working please?

 

 

 

Super User
Posts: 10,279

Re: macro variable with value of date of today (for example: 10JUL2018 )

As the macro preprocessor is a TEXT processor, it dos not do math on its own (with a very few exceptions).

%sysfunc expects a data step function as its FIRST argument; it does not accept anything else (like another opening bracket in your case).

 

So you need to get the macro processor to do math, which is not that trivial:

%let d_yesterday2=%sysfunc(putn(%eval(%sysfunc(today())-1),date9.));
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Super User
Posts: 8,120

Re: macro variable with value of date of today (for example: 10JUL2018 )

Posted in reply to KurtBremser

@KurtBremser wrote:

As the macro preprocessor is a TEXT processor, it dos not do math on its own (with a very few exceptions).

%sysfunc expects a data step function as its FIRST argument; it does not accept anything else (like another opening bracket in your case).

 

So you need to get the macro processor to do math, which is not that trivial:

%let d_yesterday2=%sysfunc(putn(%eval(%sysfunc(today())-1),date9.));

In general you are right that the macro processor does not do math.  But the %SYSFUNC() function mechanism (or perhaps the PUTN() function itself) can do the math so the %EVAL() is not needed in this example.

 

1979  %put %sysfunc(putn(%sysfunc(today())-1,date9.));
09JUL2018
Super User
Posts: 6,785

Re: macro variable with value of date of today (for example: 10JUL2018 )

99% of the time this is a wasted effort.  SAS already provides such a macro variable:  &sysdate9

 

The only difference would occur if your program begins before midnight and ends after midnight.  In those cases TODAY() changes, but &SYSDATE9 remains the date when the program started to run.

Super User
Posts: 10,787

Re: macro variable with value of date of today (for example: 10JUL2018 )

%put &sysdate9. ;
Frequent Contributor
Posts: 142

Re: macro variable with value of date of today (for example: 10JUL2018 )

What is the meaning of the number of %put &sysdate9.  ?

I see that I get  14382 

 

And IF I run 

%let d_today1=%sysfunc(today());
%put &d_today1.;

then I get 21375 which are number of days since 1.1.1960

Super User
Posts: 10,279

Re: macro variable with value of date of today (for example: 10JUL2018 )


@Ronein wrote:

What is the meaning of the number of %put &sysdate9.  ?

I see that I get  14382 

 

And IF I run 

%let d_today1=%sysfunc(today());
%put &d_today1.;

then I get 21375 which are number of days since 1.1.1960


Try again:

24         %put &sysdate9.;
10JUL2018
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Frequent Contributor
Posts: 142

Re: macro variable with value of date of today (for example: 10JUL2018 )

Posted in reply to KurtBremser

Funny

I am located now in Cyprus and date here is 10JUL2018

But I get 09JUL2018

Maybe date of SAS in my computer is not defined in Cyprus?

 

Super User
Posts: 10,279

Re: macro variable with value of date of today (for example: 10JUL2018 )


@Ronein wrote:

Funny

I am located now in Cyprus and date here is 10JUL2018

But I get 09JUL2018

Maybe date of SAS in my computer is not defined in Cyprus?

 


That's because (as @Astounding already mentioned) &sysdate9 is set when the SAS session starts, and yours probably was started yesterday.

 

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Ask a Question
Discussion stats
  • 14 replies
  • 106 views
  • 2 likes
  • 6 in conversation