BookmarkSubscribeRSS Feed
Kiteulf
Quartz | Level 8

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

5 REPLIES 5
Kiteulf
Quartz | Level 8

"&firstday"d

DBailey
Lapis Lazuli | Level 10

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;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

DBailey
Lapis Lazuli | Level 10

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

Tom
Super User Tom
Super User

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 ;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 1016 views
  • 3 likes
  • 4 in conversation