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.;
Why? The way you fill CurDate allows the usage in any case i could think of.
%let Curdate= "%sysfunc(today(),date9.)"d;
Why? The way you fill CurDate allows the usage in any case i could think of.
%let Curdate= "%sysfunc(today(),date9.)"d;
@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.))
Don't. Just store the raw value in your macro variable, that is much easier to handle.
%let curdate=%sysfunc(today());
Also see Maxim 28.
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.
@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.
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.
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.
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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.