Download nextmemn.sas nextmemn.sasSubmit a comment

%macro nextmemn (mem, memtype=DATA);

  %local base space i memname inrange;

  %if (%scan (&mem,2,.) =)
    %then %let base = WORK.&mem;
    %else %let base = &mem;

  %let space = %length (%scan (&base,2,.));
  %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 (exist (&memname, &memtype))));
    %let i = %eval (&i + 1);
    %let memname = &base.%sysfunc(putn(&i,z&space..));
    %let inrange = %sysfunc (mod (&i, 10**&space)) ;
  %end;

  %if &inrange = 0 %then %do;
    %put ERROR: Name space is full, delete library members named like &base, of type &memtype, and try again;
    _12345678._
  %end;
  %else
    &memname
  ;

%mend;

/*
data %nextmemn(foobarx); stop; run;
data %nextmemn(foobarx); stop; run;
data %nextmemn(foobarx); stop; run;
data %nextmemn(foobarx); stop; run;
data %nextmemn(foobarx); stop; run;
data %nextmemn(foobarx); stop; run;
data %nextmemn(foobarx); stop; run;
data %nextmemn(foobarx); stop; run;
data %nextmemn(foobarx); stop; run;
data %nextmemn(foobarx); stop; run;
*/