DATA Step, Macro, Functions and more

Macro Dates

Reply
Contributor
Posts: 42

Macro Dates

Hi,

Currently within my SAS code there is a date macro

%let datemonth =04jan2018

Next month it could read

%let datemonth =12feb2018

So the day of the month changes. My macro for automation doesn't seem to work for this reason I think when I write this

%let Dartmouth =%sysfunc(intnx(month,%sysfunc(today())),-1),date9.));

Any ideas how to correct the macro please?
Super User
Posts: 10,280

Re: Macro Dates

Posted in reply to Brandon16

You have 4 opening, but 6 closing parenthesis.

Try this:

%let datemonth=%sysfunc(intnx(month,%sysfunc(today()),-1),date9.);
%put &datemonth;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 6,785

Re: Macro Dates

Posted in reply to Brandon16

Assuming you balance the parentheses:

 

%let Dartmouth =%sysfunc(intnx(month,%sysfunc(today()), -1),date9.);

 

You still haven't told us why the program should return a particular day of the month instead of the 1st day of the month.  What reason is there for picking the 8th or the 12th instead of the 1st?

 

Taking a wild stab at the reason, that it should be the same day of the month as when you run the program, here is something you can add:

 

%let Dartmouth =%sysfunc(intnx(month,%sysfunc(today()),-1, same),date9.);

Contributor
Posts: 42

Re: Macro Dates

Posted in reply to Brandon16
The dataset is named by date depending on when it was created which is why the day can change.
Super User
Posts: 6,785

Re: Macro Dates

Posted in reply to Brandon16

For that, you don't need a program.  There is an automatic macro variable (you already have it) named &SYSDATE9 that contains that exact information.

Contributor
Posts: 42

Re: Macro Dates

Posted in reply to Brandon16
I still can't get it to work. When I key in the %let date manually, it works as expected. As soon as I revert to the Macro, it doesn't match. No idea why.
Super User
Posts: 10,280

Re: Macro Dates

Posted in reply to Brandon16

Then post the log of the method that does not work as expected. Use the {i} icon for this.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Super User
Posts: 8,127

Re: Macro Dates

Posted in reply to Brandon16

Are you really creating a macro variable named Dartmouth ? 

Make sure you reference the same macro variable you create.

If you are running the %LET inside of a macro then make sure to define the macro variable as global before creating it or else it will be local and disappear when the macro finishes running.

 

Super User
Posts: 13,583

Re: Macro Dates

Posted in reply to Brandon16

Brandon16 wrote:
Hi,

Currently within my SAS code there is a date macro

%let datemonth =04jan2018

Next month it could read

%let datemonth =12feb2018

So the day of the month changes. My macro for automation doesn't seem to work for this reason I think when I write this

%let Dartmouth =%sysfunc(intnx(month,%sysfunc(today())),-1),date9.));

Any ideas how to correct the macro please?

You should show how code is currently actually using the macro variable DATEMONTH (likely not Dartmouth).

The %sysfunc approach is going to yield the unformatted numeric version of the date such as 21227 for 12Feb2018, not the DATE9 version.

 

Also

Doesn't work is awful vague.

Are there errors in the log?: Post the code and log in a code box opened with the {i} to maintain formatting of error messages.

No output? Post any log in a code box.

Unexpected output? Provide input data in the form of a dataset, the actual results and the expected results. Data should be in the form of a data step. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

Contributor
Posts: 42

Re: Macro Dates

Posted in reply to Brandon16
Dartmouth was a typo sorry.

I have a proc tabulate and class displays as expected when NOT using the automated Macro, however when I try to use the automated Macro, the class is aggregated up as a total
Super User
Posts: 10,280

Re: Macro Dates

Posted in reply to Brandon16

CODE.AND.LOG.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Ask a Question
Discussion stats
  • 10 replies
  • 172 views
  • 0 likes
  • 5 in conversation