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

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

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

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

9 REPLIES 9
damanaulakh88
Obsidian | Level 7

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

BrunoMueller
SAS Super FREQ

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
)
andreas_lds
Jade | Level 19

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;         

Mirisage
Obsidian | Level 7

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

BrunoMueller
SAS Super FREQ

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)
Tom
Super User Tom
Super User

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. 

viollete
Calcite | Level 5

this:

%name_1 (mth=feb, yr=2010)


change into this:


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

Tom
Super User Tom
Super User

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.

Mirisage
Obsidian | Level 7

  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

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
  • 9 replies
  • 1189 views
  • 7 likes
  • 6 in conversation