08-18-2014 08:08 AM
data monthly_data ;* add rows for months with no spend ;
set summary1 ;
by id ;
priordate = lag( date ) ;
if first.id then priordate= date ;
spend = . ;
target = date ;
date = priordate ; if last.id then target=&firstday. ;
do while( intck( 'month', date, target) > 1) ;
date= intnx( 'month', date, 1) ;
keep id spend date ;
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....
08-18-2014 09:19 AM
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.
08-18-2014 09:32 AM
Actually, the macro will resolve correct:
%let firstday = put(intnx('month',today(),0),DATE9.);
So the final code passed to compiler looks like:
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.
08-18-2014 09:40 AM
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 ;