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.

BASUG is hosting free webinars ! Check out our recordings of past webinars: https://www.basug.org/videos. Be sure to subscribe to our email list for notification of future BASUG events.
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.

BASUG is hosting free webinars ! Check out our recordings of past webinars: https://www.basug.org/videos. Be sure to subscribe to our email list for notification of future BASUG events.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 8 replies
  • 11154 views
  • 5 likes
  • 6 in conversation