data monthly_data ;* add rows for months with no spend ;
set summary1 ;
by id ;
priordate = lag( date ) ;
if first.id then priordate= date ;
output ;
spend = . ;
target = date ;
date = priordate ; if last.id then target=&firstday. ;
output ;
do while( intck( 'month', date, target) > 1) ;
date= intnx( 'month', date, 1) ;
output ;
end ;
keep id spend date ;
run ;
Allright if I insert the code marked in brown I try to make the last.id be equal to a macro which I have
firstday = put(intnx('month',today(),0),DATE9.);
However this results in a syntax error....
"&firstday"d
I don't think the using "put(intnx('month',today(),0),DATE9.)"d will work either. In this case, I think the SAS macro is just functioning as a text replacement and would put exactly what you typed into the data step. I don't think using a macro gains you anything in this case.
%macro firstday();
intnx('month',today(),0)
%mend;
Actually, the macro will resolve correct:
%let firstday = put(intnx('month',today(),0),DATE9.);
data a;
target=&firstday.;
output;
run;
So the final code passed to compiler looks like:
data a;
target=put(intnx('month',today(),0),DATE9.);
output;
run;
Would need to see the log warnings/errors, however, what is target as its not defined. I also agree with DBailey, I don't see value in this.
I meant to say that it would not resolve to a date.
So your question is more about referencing a macro variable than calling a macro.
If you create the macro variable before the data step using the code like this:
%let firstday = %sysfunc(intnx(month,%sysfunc(today()),0),DATE9.);
Then the value of the macro variable will look something like: 01AUG2014
Now if you try to type that into an assignment statement like:
if last.id then target=01AUG2014 ;
Then SAS will not like that as you are neither assigning a number or a text string or even referencing another variable since variable names cannot start with digits. So what you want to do is set TARGET to the DATE that is meant by that string. So you want to use a date literal.
if last.id then target="01AUG2014"d ;
And now the line in your program should be:
if last.id then target="&firstday"d ;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.