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.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 482 views
  • 1 like
  • 3 in conversation