BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

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

14 REPLIES 14
PaigeMiller
Diamond | Level 26
%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
Ronein
Meteorite | Level 14

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

Kurt_Bremser
Super User

@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.;

 

Tom
Super User Tom
Super User

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

Tom
Super User Tom
Super User

@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.);
Ronein
Meteorite | Level 14

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?

 

 

 

Kurt_Bremser
Super User

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.));
Tom
Super User Tom
Super User

@Kurt_Bremser 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
Astounding
PROC Star

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.

Ronein
Meteorite | Level 14

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

Kurt_Bremser
Super User

@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
Ronein
Meteorite | Level 14

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?

 

Kurt_Bremser
Super User

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

 

 

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!

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
  • 14 replies
  • 8315 views
  • 2 likes
  • 6 in conversation