DATA Step, Macro, Functions and more

Calling a macro

Reply
Contributor
Posts: 40

Calling a macro

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....

Contributor
Posts: 40

Re: Calling a macro

"&firstday"d

Super Contributor
Posts: 578

Re: Calling a macro

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;

Super User
Super User
Posts: 7,942

Re: Calling a macro

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.

Super Contributor
Posts: 578

Re: Calling a macro

I meant to say that it would not resolve to a date.

Super User
Super User
Posts: 7,042

Re: Calling a macro

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 ;

Ask a Question
Discussion stats
  • 5 replies
  • 270 views
  • 3 likes
  • 4 in conversation