BookmarkSubscribeRSS Feed
Mirisage
Obsidian | Level 7

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

2 REPLIES 2
Reeza
Super User

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.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1166 views
  • 0 likes
  • 3 in conversation