×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Macro to automate selection of multiple files with similar name

Macro to automate selection of multiple files with similar name

Macro to automate selection of multiple files with similar name

(OP)
data lib.p_claim2;
set
        p_claim.file06_2001_2           
                p_claim.file06_2001_3
                p_claim.file06_2001_4
                p_claim.file06_2001_5
                p_claim.file06_2001_6
                p_claim.file06_2001_7   ;
run;

is there a way with a macro that I can reference these p_claim.files without having to spell out each and every full name of the file?  I can have up to twenty of these at a time.  

RE: Macro to automate selection of multiple files with similar name

A simple loop should suffice..



%macro overall;

data setted;
  set
  %do i = 1 %to 7; /*where 1 is the first number and 7 is the last*/
    p_claim.file06_2001_&i
  %end;
  ;
run;

%mend;

%overall;

RE: Macro to automate selection of multiple files with similar name

(OP)
Thanks for the code.  Now lets say you multiple years such as
p_claim.file06_2001_1
p_claim.file06_2001_2
p_claim.file06_2001_3

p_claim.file06_2002_1
p_claim.file06_2002_2
p_claim.file06_2002_3

 would I just use just use a separate macro for each year scenario?

/*year 2001*/
data mylib.Files;
set %do i = 1%to 3;
mylib.p_claim.file06_2001_&i
%end;
  ;
run;
%mend;

%overall;

/*year 2002*/
data mylib.Files;
set %do i = 1%to 3;
mylib.p_claim.file06_2002_&i
%end;
  ;
run;
%mend;

%overall;

RE: Macro to automate selection of multiple files with similar name

Always remember that macro is used to make repetitive things easier, the repeating bits you can loop and/or parametrize for example. You have the year and the number of consecutive files as variable parts, so assuming the library and file names are fix, just add those two to the macro definition. If library and file name could change, add them as parameters with a default assigned:

CODE

%*--  create a combined table per year.  ;
%macro overall (lib=p_claim, file=file06, year=, maxfile=);
  data setted&YEAR.;
    set
    %do i = 1 %to &MAXFILE.;
      &LIB..&FILE._&YEAR._&I.
    %end;
    ;
  run;
%mend;

%*--  use the macro.  ;
%overall (year=2001, maxfile=3);
%overall (year=2002, maxfile=3);

%*--  use the macro and override a default.  ;
%overall (file=fileABC, year=2003, maxfile=7);

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close