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

Hello

I am using %let to find date of today and put it in macro .

Then I want to create another macro variable that take the date and add   'd  at the end.

What is the way to do it please?

I want to get value   '23MAR2020'd

 

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

Why? The way you fill CurDate allows the usage in any case i could think of.

%let Curdate= "%sysfunc(today(),date9.)"d;

View solution in original post

8 REPLIES 8
andreas_lds
Jade | Level 19

Why? The way you fill CurDate allows the usage in any case i could think of.

%let Curdate= "%sysfunc(today(),date9.)"d;
PaigeMiller
Diamond | Level 26

@Ronein wrote:

Hello

I am using %let to find date of today and put it in macro .

Then I want to create another macro variable that take the date and add   'd  at the end.

What is the way to do it please?

I want to get value   '23MAR2020'd

 

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

 


I think this is a poor idea. You almost never need a macro variable to contain '23MAR2020'd. 

 

If your macro variable contains the numeric date (so if today is 23MAR20, you want the macro variable to have the value of 21997). This macro variable, with value 21997, will work in almost every situation imaginable. 

 

So

 

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

 

is all you need.

 

If, for purposes of display, you need 23MAR2020 (without the quotes and with no D on the end), that's easy to obtain as well.

 

%sysfunc(putn(&currdate,date9.))

 

 

--
Paige Miller
Quentin
Super User

I'm curious, @Kurt_Bremser and @PaigeMiller , when you write a macro with where the user passes a date argument, what format do you use for the parameter value?

 

This is a case where I think it's reasonable that someone might want to create the a date literal.

 

So if I write:

%macro foo(date=);
  ...
%mend foo;

Would you want that macro to be called like %foo(date="01Jan2020"d) or would you want the macro to expect a numeric value: %foo(date=21915) or %foo(date=%sysfunc(putn("01Jan2020"d,8.)) ?

 

I tend to like the idea of passing date values to macros as date literals, as I think it makes it easier for users.  And the log is easier to read too.  But in reality, if you look at my macros, they're probably inconsistent in which format is used to pass date values.  Sometimes I allow both the date literal and the numeric value.

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
PaigeMiller
Diamond | Level 26

@Quentin wrote:

I'm curious, @Kurt_Bremser and @PaigeMiller , when you write a macro with where the user passes a date argument, what format do you use for the parameter value?

 

This is a case where I think it's reasonable that someone might want to create the a date literal.

 

So if I write:

%macro foo(date=);
  ...
%mend foo;

Would you want that macro to be called like %foo(date="01Jan2020"d) or would you want the macro to expect a numeric value: %foo(date=21915) or %foo(date=%sysfunc(putn("01Jan2020"d,8.)) ?

 

I tend to like the idea of passing date values to macros as date literals, as I think it makes it easier for users.  And the log is easier to read too.  But in reality, if you look at my macros, they're probably inconsistent in which format is used to pass date values.  Sometimes I allow both the date literal and the numeric value.


I think if the argument to the macro call is something a human has to specify as a calendar date, then either 

%foo(date=01JAN20) or %foo(date='01JAN20'd) works for me. In the first case, the logic inside the macro has to be aware that the text string 01JAN20 must be converted to an actual SAS date.

 

On the other hand, if the macro is called by a program that is computing dates somehow, then %foo(date=&today) where &today is a defined macro variable with value 21997, that works for me. I realize the LOG isn't as readable with 21997, but I prefer to simplify the programming, rather than simplify the reading of the log.

--
Paige Miller
Tom
Super User Tom
Super User

Normally I would require the user to supply a valid date value.  They can choose whether they want to use a date literal or a raw number of days.

%foo(date="&sysdate9."d)
%foo(date=%sysfunc(date()))

But it really depends on how the macro is using &DATE.  Is it just going to put it into SAS code?  Or is it going to try to use the value in macro logic?

It also depends on how the users are using those values in the larger problem.  If they are mainly using the values as strings that they use to generate filenames or put into labels or titles then the values should be human readable. The macro can contain the logic to convert them to actual dates where needed.

 

Kurt_Bremser
Super User

Whenever I have something where a date is entered, it comes from a prompt, and will come in date9 format without the quotes and the d. I never let users enter just any text, inputs have to come from selections.

All date macro variables created within a program are numeric.

Quentin
Super User

Thanks @Kurt_Bremser . That definitely makes sense for naive users.  But I was thinking about about macros that are used (called) by SAS programmers/developers.  So the users are writing macro calls.

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 11745 views
  • 5 likes
  • 6 in conversation