BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
KevinC_
Fluorite | Level 6

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

12 REPLIES 12
art297
Opal | Level 21

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

  %month_end(i);


Reeza
Super User

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

snoopy369
Barite | Level 11

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).

KevinC_
Fluorite | Level 6

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!

snoopy369
Barite | Level 11

Ah indeed, corrected.

art297
Opal | Level 21

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;

snoopy369
Barite | Level 11

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

put i worddate.;

in the macro.

Reeza
Super User

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

KevinC_
Fluorite | Level 6

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!

Tal
Pyrite | Level 9 Tal
Pyrite | Level 9

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?

art297
Opal | Level 21

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.

Tal
Pyrite | Level 9 Tal
Pyrite | Level 9

thank you

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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