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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

View solution in original post

6 REPLIES 6
Kurt_Bremser
Super User

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.

SURIM
Obsidian | Level 7

Thanks a lot for your fast reply!

PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
SURIM
Obsidian | Level 7
Good advice, thanks you!
mkeintz
PROC Star

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.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
SURIM
Obsidian | Level 7

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: 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
  • 6 replies
  • 14278 views
  • 2 likes
  • 4 in conversation