I have no idea how to solve this problem. Could anyone help me to debug? thanks.
code:
%macro rmhistory( t);
proc import datafile="D:\Users\emma\Documents\DDS\RMTable\file.txt" out=file replace;
run;
DATA rm_all;
RUN;
%do i=1 %to &t;
data _null_;
set file;
if _n_=&i then do;
call symput('ss', aa);
end;
run;
%put &ss &i;
proc import datafile="D:\Users\emma\Documents\DDS\RMTable\&ss" out=rm&i replace;
SHEET="RMTable";
run;
data rmyyyymm&i;
set rm&i;
FORMAT yyyymm $6.;
yyyymm = substr(&ss,1,6);
run;
data rm_all;
set rm_all rmyyyymm&i;
run;
%end;
%mend rmhistory;
%rmhistory(1);
error message:
MPRINT(RMHISTORY): data rmyyyymm1;
MPRINT(RMHISTORY): set rm1;
MPRINT(RMHISTORY): FORMAT yyyymm $6.;
NOTE: Line generated by the macro variable "SS".
1 201801_RMTableV1.xlsx
---------------
22
557
ERROR: DATA STEP Component Object failure. Aborted during the COMPILATION phase.
ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, *, **, +, ',', -, /, <, <=, <>, =, >, ><,
>=, AND, EQ, GE, GT, LE, LT, MAX, MIN, NE, NG, NL, OR, ^=, |, ||, ~=.
ERROR 557-185: Variable _RMTableV1 is not an object.
NOTE: The SAS System stopped processing this step because of errors.
NOTE: DATA statement used (Total process time):
real time 0.03 seconds
cpu time 0.00 seconds
MPRINT(RMHISTORY): yyyymm = substr(201801_RMTableV1.xlsx,1 ,6);
MPRINT(RMHISTORY): run;
MPRINT(RMHISTORY): data rm_all;
MPRINT(RMHISTORY): set rm_all rmyyyymm1;
MPRINT(RMHISTORY): run;
Welcome to the SAS communities 🙂
I believe the problem is the fact that a SAS data set can not begin with a number
EDIT: On second thought, I don't think so. I think you need to replace this
data rmyyyymm&i;
set rm&i;
FORMAT yyyymm $6.;
yyyymm = substr(&ss,1,6);
run;
with this
data rmyyyymm&i;
set rm&i;
FORMAT yyyymm $6.;
yyyymm = substr("&ss",1,6);
run;
Welcome to the SAS communities 🙂
I believe the problem is the fact that a SAS data set can not begin with a number
EDIT: On second thought, I don't think so. I think you need to replace this
data rmyyyymm&i;
set rm&i;
FORMAT yyyymm $6.;
yyyymm = substr(&ss,1,6);
run;
with this
data rmyyyymm&i;
set rm&i;
FORMAT yyyymm $6.;
yyyymm = substr("&ss",1,6);
run;
Since you have a Base SAS and not a macro problem, test your code first without macro logic:
proc import
datafile="D:\Users\emma\Documents\DDS\RMTable\file.txt"
out=file
replace
;
run;
%let i=1;
data _null_;
set file;
if _n_=&i then do;
call symput('ss', aa);
end;
run;
%put &ss &i;
proc import
datafile="D:\Users\emma\Documents\DDS\RMTable\&ss"
out=rm&i
replace
;
sheet = "RMTable";
run;
data rmyyyymm&i;
set rm&i;
format yyyymm $6.;
yyyymm = substr(&ss,1,6);
run;
data rm_all;
set rm_all rmyyyymm&i;
run;
See that I added some consistent visual structure to your code to make it easier to read and understand (Maxim 12).
Without the macro around it, the ERROR message will now appear at the crucial point in your code, and alert you correctly to your mistake, which @PeterClemmensen already pointed out.
Macro development always starts with working SAS code; had you started with that, you would have realized you have to put a string (with quotes) in that position, and would have replace the text within that string with the macro variable (and not just inserted the macro variable without quotes)
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!
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.