Help using Base SAS procedures

Pass a macro variable to a macro?

Accepted Solution Solved
Reply
Regular Contributor
Posts: 173
Accepted Solution

Pass a macro variable to a macro?

Hello,

I have created a macro that gets executed for everyday of the month.  In the code below &beg_date and &end_date represent the first date and last date of a month (i.e. 20130901 and 20130930).  They have already been defined in previous steps.  The problem is with the "i" in the do loop.  The "i" seems to work just fine with the "put i" statement.  However, when I call the macro %month_end it does not reconize the i being paseed. The log says "Macro variable DT resolves to &i.".  I have posted my code below.  If anyone has a different approach (not using the do loop) I would love to hear it.  Any input would be greatly appreciated Smiley Happy

Just a little more information, I am trying to run the macro 30 times and the value of "dt" should be from 20130901 to 20130930.

%macro month_end (dt);

data aaa_&dt.;

XXXXX

run;
%mend;

data _null_;

do i = &beg_date. to &end_date.;

  put i;

  %month_end(&i.);

end;

run;


Accepted Solutions
Solution
‎10-25-2013 03:41 PM
Super User
Posts: 17,819

Re: Pass a macro variable to a macro?

You have no macro variable set up as &i, but a data step variable i (No %do/%to).

Either move the loop to macro code or use call execute to call the macro instead.

EDITED

View solution in original post


All Replies
PROC Star
Posts: 7,363

Re: Pass a macro variable to a macro?

You are trying to pass a non-existent macro variable. I think you meant to type:

  %month_end(i);


Solution
‎10-25-2013 03:41 PM
Super User
Posts: 17,819

Re: Pass a macro variable to a macro?

You have no macro variable set up as &i, but a data step variable i (No %do/%to).

Either move the loop to macro code or use call execute to call the macro instead.

EDITED

Regular Contributor
Posts: 244

Re: Pass a macro variable to a macro?

You're talking about passing the value of a data step variable to a macro, not a macro variable.  You can't pass the value of i (not &i, that doesn't exist in this code).  You can pass the variable name, which then can be used in the macro like a normal variable name - you don't say what your code is doing so no idea if that's helpful or not.  But you can't pass the value stored in i to the macro in the way you're going about it.

What you could do is use CALL EXECUTE to call the macro, if it's doing things outside of a datastep anyway, something like

data _null_;

do i = &beg_date. to &end_date.;

  put i;

  callstr=cats('%month_end(',i,'.);');

  call execute (callstr);

end;

run;

What that would do is run all of the macro iterations after the data step finished running.  If that's not helpful you'll have to describe what the macro is doing (ie, post the macro or some equivalent).

Regular Contributor
Posts: 173

Re: Pass a macro variable to a macro?

Thank you, Authur, Reeza, and Snoopy369,  for your suggestions !

Snoopy369,

I tried your code but is your callstr= cats statement missing a ")"?

Thank you all again!

Regular Contributor
Posts: 244

Re: Pass a macro variable to a macro?

Ah indeed, corrected.

PROC Star
Posts: 7,363

Re: Pass a macro variable to a macro?

and .  You both indicated that one can't pass a variable to a macro and I know that you are both more knowledgeable than I am regarding macros.  However, if one can't do it, why does the following work?:

%let beg_date=01sep2013;

%let end_date=30sep2013;

%macro month_end (dt);

  put &dt. worddate.;

%mend;

data _null_;

  do i = "&beg_date."d to "&end_date."d;

    %month_end(i);

  end;

run;

Regular Contributor
Posts: 244

Re: Pass a macro variable to a macro?

Because you are passing the variable name 'i' to the macro, and then you are executing

put i worddate.;

in the macro.

Super User
Posts: 17,819

Re: Pass a macro variable to a macro?

I didn't say you couldn't, but not a solution I offered Smiley Happy

Regular Contributor
Posts: 173

Re: Pass a macro variable to a macro?

Thank you to all of you for your input! 

I played with all different approaches suggested by you.  I decided to put my do loop inside a macro and pass i as a macro variable.  The code worked beautifully.

Thank you all again!

Super Contributor
Super Contributor
Posts: 440

Re: Pass a macro variable to a macro?

hey guys,

what's with the dots after "beg_date" and "end_date" in your codes ?

i tried the codes and they are running without them too so what  s the use of it.Can someone explain please?

PROC Star
Posts: 7,363

Re: Pass a macro variable to a macro?

See: SAS(R) 9.2 Macro Language: Reference

The periods usually aren't necessary, but may be.  You are always, I think, safest including them.

Super Contributor
Super Contributor
Posts: 440

Re: Pass a macro variable to a macro?

thank you

☑ This topic is SOLVED.

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

Discussion stats
  • 12 replies
  • 326 views
  • 1 like
  • 5 in conversation