Help using Base SAS procedures

How could I write this macro?

Accepted Solution Solved
Reply
Super Contributor
Posts: 338
Accepted Solution

How could I write this macro?

Hi SAS Forum,

I have 35 proc sort rounds (commencing Feb_2010 and ending Dec_2012).

Three out of those 35 are reproduced below.

proc sort data=feb_2010_;

by bank_number account_number ;

run;

proc sort data=mar_2010_ ;

by bank_number account_number ;

run;

proc sort data=apr_2010_ ;

by bank_number account_number ;

run;

  • Writing 35 times is clumsy. So, I tried a parameter macro like below but it doesn't work.

%macro name_1 (mth, yr);

proc sort data=&mth_&yr_;

by bank_number account_number ;

run;

%mend;

%name_1 (mth=feb, yr=2010);

%name_1 (mth=mar, yr=2010);

%name_1 (mth=apr, yr=2010);

Could anyone help me to correct the above macro (or sugggest me a method to cut short writing proc sort steps 35 times.

Thanks

Mirisage


Accepted Solutions
Solution
‎03-06-2013 03:54 AM
Super Contributor
Posts: 345

Re: How could I write this macro?

Two things: first if you want to use named-style parameters add an = after the name, second: because the underscore is a valid char in variable names, you need to add separating periods.

%macro name_1 (mth=, yr=);

proc sort data=&mth._&yr._

by bank_number account_number ;

run;

%mend;


You can increase the level of automation (untested code):


data _null_;

     do year = 2010 to 2012;

          do month = 1 to 12;

               text_month = put(mdy(month, 1, year), MONNAME3.);

               dataset = cats(text_month, '_', year, '_');

               if exist(dataset) then do;

                    call execute(cats('%name_1(mty=', text_month, ', yr=', year, ');'));

               end;

          end;

     end;

run;         

View solution in original post


All Replies
Frequent Contributor
Posts: 81

Re: How could I write this macro?

Hi,

Please find below the updated code:-

%macro name_1 (mon);

proc sort data=&mon._;

by bank_number account_number ;

run;

%mend;

%name_1 (FEB_2010);

Thanks.

/Daman

SAS Super FREQ
Posts: 708

Re: How could I write this macro?

Hi

Find below a macro that will generate the right data set names based on a start and end date, it should help you. It uses the %SYSFUNC function to call DATA Step function for the date calculation as well as for formatting the data set name.

%macro someDate(
  start=
  , end=
)
;
 
%let start_d = %sysevalf("&start"d);
  %let end_d = %sysevalf("&end"d);
  %let nMonths = %sysfunc( intck(MONTH, &start_d, &end_d) );

 
%do i = 0 %to &nMonths;
    %let dsnDate_d = %sysfunc(intnx(MONTH, &start_d, &i));
    %let dsn = %sysfunc(putn(&dsnDate_d, MONNAME3.))_%sysfunc(putn(&dsnDate_d, YEAR4.));
    %put NOTE: i=&i dsn=&dsn;
  %end;

%mend;

%
someDate(
  start=01feb2010
  , end=01dec2012
)
Solution
‎03-06-2013 03:54 AM
Super Contributor
Posts: 345

Re: How could I write this macro?

Two things: first if you want to use named-style parameters add an = after the name, second: because the underscore is a valid char in variable names, you need to add separating periods.

%macro name_1 (mth=, yr=);

proc sort data=&mth._&yr._

by bank_number account_number ;

run;

%mend;


You can increase the level of automation (untested code):


data _null_;

     do year = 2010 to 2012;

          do month = 1 to 12;

               text_month = put(mdy(month, 1, year), MONNAME3.);

               dataset = cats(text_month, '_', year, '_');

               if exist(dataset) then do;

                    call execute(cats('%name_1(mty=', text_month, ', yr=', year, ');'));

               end;

          end;

     end;

run;         

Super Contributor
Posts: 338

Re: How could I write this macro?

Posted in reply to andreas_lds

Hi Andreas, Daman and Bruno,

Many thanks to each one of you.

Hi Bruno,

I am trying to learn your automation code. Before I start googling the meanings of %sysevalf, %sysfunc etc., I just ran your code.

%macro someDate(  start=, end=);

  %let start_d = %sysevalf("&start"d);

  %let end_d = %sysevalf("&end"d);

  %let nMonths = %sysfunc( intck(MONTH, &start_d, &end_d) );

  %do i = 0 %to &nMonths;

    %let dsnDate_d = %sysfunc(intnx(MONTH, &start_d, &i));

    %let dsn = %sysfunc(putn(&dsnDate_d, MONNAME3.))_%sysfunc(putn(&dsnDate_d, YEAR4.));

    %put NOTE: i=&i dsn=&dsn;

  %end;

%mend;

%someDate(start=01feb2010, end=01dec2012)

Could you please let me know what it generates. I could not find the output.

Thank you

Mirisage

SAS Super FREQ
Posts: 708

Re: How could I write this macro?

Hi Mirisage

The macro generates the appropriate string for the data set name you are looking for.

The %SYSEVALF() is used to transform a SAS date constant into a number (internal representation of a date).

The %SYSFUNC() is used to call DATA step functions INTNX() to caluclate the dates needed, which are then formatted using the PUTN(), find below the complete example.

%macro name_1(mon);
  proc sort data=&mon._;
    by bank_number account_number;
  run;
%mend;

%macro someDate(  start=, end=);
 
%let start_d = %sysevalf("&start"d);
  %let end_d = %sysevalf("&end"d);
  %let nMonths = %sysfunc( intck(MONTH, &start_d, &end_d) );

 
%do i = 0 %to &nMonths;
    %let dsnDate_d = %sysfunc(intnx(MONTH, &start_d, &i));
    %let dsn = %sysfunc(putn(&dsnDate_d, MONNAME3.))_%sysfunc(putn(&dsnDate_d, YEAR4.));
    %put NOTE: &sysmacroname Processing dsn=&dsn;

    %
name_1(&dsn)
 
%end;
%mend;

%
someDate(start=01feb2010, end=01dec2012)
Super User
Super User
Posts: 7,039

Re: How could I write this macro?

Posted in reply to andreas_lds

Actually for positional parameters the user has the choice of whether to use the name or not.  If you define the parameters as named (adding the = sign after the parameter name in the macro definition) you are FORCING the user to use the names in the macro call. 

Contributor
Posts: 62

Re: How could I write this macro?

this:

%name_1 (mth=feb, yr=2010)


change into this:


%name_1 (feb,2010)  , and it should work then.

Super User
Super User
Posts: 7,039

Re: How could I write this macro?

If MTH and YR are defined as positional parameter in the macro definition there is no difference in those two macro calls.  If either is defined as a named parameter then the second syntax will generate an error message.

Super Contributor
Posts: 338

Re: How could I write this macro?

  Hi Bruno,

Thank you very much for this description. This is great!

Hi Tom,

Thank you too for further clarifications on positional parameters.

Hi Viollete,

Than you for your inputs which was further clarified by Tom,

Best regards to all,

Mirisage

🔒 This topic is solved and locked.

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

Discussion stats
  • 9 replies
  • 389 views
  • 7 likes
  • 6 in conversation