BookmarkSubscribeRSS Feed
julianaram
Calcite | Level 5

Hi! I'm trying to create leads for the variable "annualearn" and I want to call them earn_t&i. For that, I am writing the following macro:

 

%macro forwardearn;
%do i=1 %to 7;
data analysis30;
set analysis30;
if eof&i=0 then
set analysis30 (firstobs=&i+1 keep=annualearn rename=(annualearn=earn_t&i)) end=eof&i;
else earn_t&i=.;
run;
%end;
%mend;
%forwardearn;

 

And I get the error:  ERROR 22-7: Invalid option name +. , which I think it refers to the + in firstobs=&i+1 (only + in the macro). 

 

Does anyone have an idea of what I am doing wrong??

3 REPLIES 3
Astounding
PROC Star

The syntax for FIRSTOBS= must take a number, not an expression.  Have the macro processor resolve the expression into a number before the DATA step "sees" it:

 

firstobs=%eval(&i+1)

julianaram
Calcite | Level 5

This was super helpful. Now it works. Thanks!

Kurt_Bremser
Super User

You should also avoid running multiple steps:

%macro forward;
data analysis30_a;
merge
  analysis30
%do i = 1 %to 7;
  analysis30 (firstobs=&i+1 keep=annualearn rename=(annualearn=earn_t&i))
%end;
;
run;
%mend;
%forward

In my experience, the merge without by sets variables to missing when one of the datasets gets past eof.