Help using Base SAS procedures

Can I use the same macro name in two different places in the same code?

Reply
Super Contributor
Posts: 338

Can I use the same macro name in two different places in the same code?

Hi SAS Forun,

I have hard coded the same date in 2 different places in a lengthy code (see yellow highlights). 

data k.Arr_Histry_filtrd_haphaz_rmvd;

    set tt;

    by bank_number account_number current_date;

    if first.account_number

      then _f1 = (current_date ne '28FEB2010'd and Arrears_Band

                    ne 'Current') ;

        retain _f1;

    if _f1 ne 1 then output;

drop _:;

run;

After the above code, I have several other code pieces below.

………………………………….

………………………………….

………………………………….

………………………………….

………………………………….

Then I get another code like below.

data t1 k.write_off_History_GE_FEB2010; /*you should use a.write_off_History_GE_FEB2010 file throughout and not a.write_off_History*/

set k.write_off_History;

if post_date <'28FEB2010'd then output t1;

else output k.write_off_History_GE_FEB2010;

run;

Question:

I want to turn the above hard coding into a macro.

Could any macro expert please let me know if the following macro is logically OK?

Can I use the same macro name, i.e. %macro first_1(mth), in two different places in the same code?

%let t_1=28FEB2010;

%macro first_1(mth);

data k.Arr_Histry_filtrd_haphaz_rmvd;

    set tt;

    by bank_number account_number current_date;

    if first.account_number

      then _f1 = (current_date ne '&mth'd and Arrears_Band

                    ne 'Current') ;

        retain _f1;

    if _f1 ne 1 then output;

drop _:;

run;

%mend first_1;

%first_1(mth=&t_1);

After the above code, I have several other code pieces below.

………………………………….

………………………………….

………………………………….

………………………………….

………………………………….

Then I get another code like below.

%macro first_1(mth);

data t1 k.write_off_History_GE_FEB2010; /*you should use a.write_off_History_GE_FEB2010 file throughout and not a.write_off_History*/

set k.write_off_History;

if post_date <'&mth'd then output t1;

else output k.write_off_History_GE_FEB2010;

run;

%mend first_1;

%first_1(mth=&t_1);

Thank you for your help

Mirisage

Super User
Posts: 17,868

Re: Can I use the same macro name in two different places in the same code?

You can always test it...

But a few comments, once you create a macro variable you can use it as many times as you.

And you can only resolve macro variables within double quotes, not single quotes. Well you can within single quotes but you need a different function for that.

Super User
Posts: 10,516

Re: Can I use the same macro name in two different places in the same code?

The

'&mth'd

should be

"&mth"d

Ask a Question
Discussion stats
  • 2 replies
  • 127 views
  • 0 likes
  • 3 in conversation