Hi,
I have a code that will append information to an existing table last day of each month. Now I want to append historic data and my thoughts are that I can use the same method like "today()-30" to get previous months, but instead with my macro variable.
Can anyone tell me if this pink idea below will work? Or have a better idea.
date="&todaysDate"d()-30;
Thanks!
Susannah
If &todaysdate contains a date in the date9. format, and you omit the brackets (they were part of the function call).
If you define macro variable todaysdate without a format, it's even simpler:
%let todaysdate=%sysfunc(today());
data test;
date = &todaysdate. - 30;
run;
Note that if you want to get the date of a month back, you're better off using the intnx() function.
If &todaysdate contains a date in the date9. format, and you omit the brackets (they were part of the function call).
If you define macro variable todaysdate without a format, it's even simpler:
%let todaysdate=%sysfunc(today());
data test;
date = &todaysdate. - 30;
run;
Note that if you want to get the date of a month back, you're better off using the intnx() function.
Thanks a lot for your fast reply!
First, as usual @Kurt_Bremser gives very good advice. I would like to add that when creating macro variables, I never use formatted (human readable) values of those macro variables.
%let todaysdate=%sysfunc(today());
%put &=todaysdate;
produces the value 21858, which is how SAS stores dates, as the number of days since January 1, 1960 and this is fine for all macro computations and all macro work, and this is also easier than working with formatted values. The only time you need it to appear formatted such as 05NOV19 is when you need to report something that a human will need to understand, and only at that point in time do I format the macro variable value.
I beg to differ. I almost always use human-readable date values for macrovars - but always in the DATE9 format. I find this user-friendly and easy to use, as in:
%let cutoff_date=07oct2015;
Then in my programs I just use statements such as
data want;
set have;
where date >= "&cutoff_date"d;
run;
But I would avoid like the plague using any other date specification in macrovars.
Hi,
I like this more "simple" solutions, and I realised now that I didn't specify that
1. I want to deploy the code automatically in the future, so I need a date this is not manually entered.
2. The date is not just the last day of each month, I want the SAS sytem to act as if the entered date is today, so to speak. That's why I involved the sysfunc.
And I also like the dates formatted upfront since I work a lot with order dates and first purchase dates, it makes it more easy to follow.
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.