Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macro to automate selection of multiple files with similar name 1

Status
Not open for further replies.

sap1958

Technical User
Oct 22, 2009
138
US
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.
 
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;
 
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;
 
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);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top