I have a macro that should collect data through last month for the year. Eventually I'll be adding a section to add last year plus this year until last month but I'm not there yet. The code I have is:
Options symbolgen mlogic mfile;
%macro new_tran;
data new_tran;
set
%do i = 01 %to &num_month;
%let val=%sysfunc(putn(&i,z2.));
newtran.pid_defaults_final_&&val&year;
%end;
run;
%mend;
%new_tran;
A portion of the log that I'm getting hung up is below, it looks like the macro variables are resolving with a space in between them. I tried to use sysfunc with catx but that was not successful. Any help is appreciated:
MPRINT(NEW_TRAN): set newtran.pid_defaults_final_01 2015;
ERROR: File NEWTRAN.PID_DEFAULTS_FINAL_01.DATA does not exist.
MLOGIC(NEW_TRAN): %DO loop index variable I is now 2; loop will iterate again.
MLOGIC(NEW_TRAN): %LET (variable name is VAL)
2 The SAS System 09:21 Tuesday, June 30, 2015
SYMBOLGEN: Macro variable I resolves to 2
SYMBOLGEN: && resolves to &.
SYMBOLGEN: Macro variable YEAR resolves to 2015
SYMBOLGEN: Macro variable VAL resolves to 02
NOTE: Line generated by the macro variable "VAL".
33 newtran.pid_defaults_final_02
You're missing a semicolon after the last SET statement is my guess...
Try adding a %put %str(;); after your loop or another explicit semicolon.
For set statements you can use short cuts with the colon operator or - symbol.
*Set all tables that start with pid_defaults
Set newtran.pid_defaults: ;
Your code generates an error to me, since it's looking for a macro variable val012015 with the double && symbol.
This doesn't generate any spaces for me:
%macro new_tran (num_month=12, year=2015);
%do i = 01 %to &num_month;
%let val=%sysfunc(putn(&i,z2.));
%put pid_defaults_final_&val.&year;
%end;
%mend;
%new_tran;
I added strip to where I initiate the macro variable and I get the same error that you do, making the change to &var.&year seemed to work. Any reason why the &&var&year doesn't work the way I am intending? But after looking at the output I am only getting one dataset as input where I expect 5.
I get an error when getting to the second input dataset:
newtran.pid_defaults_final_022015
_________________________________
557
ERROR: DATA STEP Component Object failure. Aborted during the COMPILATION phase.
ERROR 557-185: Variable newtran is not an object.
I've checked and the input is valid, it works without the macro, but I'm not sure how to correct the macro.
You're missing a semicolon after the last SET statement is my guess...
Try adding a %put %str(;); after your loop or another explicit semicolon.
That did it, now that it's there it seems obviously necessary but I didn't think it through. Thanks for the help:
%macro new_tran;
data new_tran;
set
%do i = 01 %to &num_month;
%let val=%sysfunc(putn(&i,z2.));
newtran.pid_defaults_final_&val.&year
%end;
;
run;
%mend;
%new_tran;
Your SAS macro variable &YEAR is right-justified and has leading blanks resulting in an invalid concatenated macro variable with the %LET. Consider how/where &YEAR is assigned and if using a %SYSFUNC(PUTN(...)) then add a -L appending as the second argument to PUT, indicating a left-justified result/assignment). Otherwise share your SAS-log result as it appears your SET statement is not formatted properly (surmised based on the ERROR).
Scott Barry
SBBWorks, Inc.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.