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

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 361 views
  • 1 like
  • 3 in conversation