DATA Step, Macro, Functions and more

macro variables not resolving the way I intend

Accepted Solution Solved
Reply
Valued Guide
Posts: 860
Accepted Solution

macro variables not resolving the way I intend

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


Accepted Solutions
Solution
‎06-30-2015 04:44 PM
Super User
Posts: 19,817

Re: macro variables not resolving the way I intend

Posted in reply to Steelers_In_DC

You're missing a semicolon after the last SET statement is my guess...

Try adding a %put %str(Smiley Wink; after your loop or another explicit semicolon.

View solution in original post


All Replies
Super User
Posts: 19,817

Re: macro variables not resolving the way I intend

Posted in reply to Steelers_In_DC

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;

Valued Guide
Posts: 860

Re: macro variables not resolving the way I intend

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.

Solution
‎06-30-2015 04:44 PM
Super User
Posts: 19,817

Re: macro variables not resolving the way I intend

Posted in reply to Steelers_In_DC

You're missing a semicolon after the last SET statement is my guess...

Try adding a %put %str(Smiley Wink; after your loop or another explicit semicolon.

Valued Guide
Posts: 860

Re: macro variables not resolving the way I intend

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;

Super Contributor
Super Contributor
Posts: 3,174

Re: macro variables not resolving the way I intend

Posted in reply to Steelers_In_DC

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.   

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 310 views
  • 3 likes
  • 3 in conversation