Download nextobjn.sas nextobjn.sasSubmit a comment

%macro nextobjn (entry, objtype=SOURCE);

  %local base space i objname inrange;

  %if (%scan (&entry,3,.) =)
    %then %let base = WORK.&entry;
    %else %let base = &entry;

  %let space = %length (%scan (&base,3,.));
  %if &space >= 8 %then %do;
    %let base = %substr (&base,1,%eval(%length(&base)-%length(space)-7));
    %let space = 1;
  %end;
  %else
    %let space = %eval (8 - &space);

  %let i = 0;
  %let inrange = 1;
  %do %until ((not &inrange) OR (not %sysfunc (cexist (&objname..&objtype))));
    %let i = %eval (&i + 1);
    %let objname = &base.%sysfunc(putn(&i,z&space..));
    %let inrange = %sysfunc (mod (&i, 10**&space)) ;

%put &base &objname;

  %end;

  %if &inrange = 0 %then %do;
    %put ERROR: Name space is full, delete catalog entries named like &base, of type &objtype, and try again;
    _12345678._._
  %end;
  %else
    &objname..&objtype
  ;

%mend;

/*
filename x catalog "%nextobjn(work.test.program)";data _null_; file x; put 'x'; run;
filename x catalog "%nextobjn(work.test.program)";data _null_; file x; put 'x'; run;
filename x catalog "%nextobjn(work.test.program)";data _null_; file x; put 'x'; run;
filename x catalog "%nextobjn(work.test.program)";data _null_; file x; put 'x'; run;
filename x catalog "%nextobjn(work.test.program)";data _null_; file x; put 'x'; run;
filename x catalog "%nextobjn(work.test.program)";data _null_; file x; put 'x'; run;
filename x catalog "%nextobjn(work.test.program)";data _null_; file x; put 'x'; run;
filename x catalog "%nextobjn(work.test.program)";data _null_; file x; put 'x'; run;
filename x catalog "%nextobjn(work.test.program)";data _null_; file x; put 'x'; run;
filename x catalog "%nextobjn(work.test.program)";data _null_; file x; put 'x'; run;
*/