/* getdsn.sas Daniel Brockman 081223 Function: Get unique new data set name */ /* * getdsname.sas Daniel Brockman 070506 Get unique new Data Set Name ; */ %macro getdsn(libdsname) ; /* /* Usage: %let myname = %getdsn([lib[.dsname]|dsname]) ; /* /* Where lib is the name of the library in which to find a unique name. /* If dsname is specified, and if a data set by that name exists within /* lib, then dsname becomes the root for a new data set name. /* /* If lib is omitted, then WORK is assumed. /* /* getdsn returns a data set name, unique within the specified library. /* /* The returned name has the form libname.datasetname . /* /* No data set by this name exists. /* /* If an extant data set exists, then getdsn won't return its name. /* /* 081223 db cloned from macro getdsname() /* 080904 db Modified to return lib and dsn separately in g2 & g1 . /* 070804 db Modified to always append random var to root name, so that /* we may create multiple temporary dataset names before creating /* the datasets themselves. /* */ %local lib dsn root nancy sluggo ; /* separate the two parts of the name */ %let lib = %scan(&libdsname,1) ; %let dsn = %scan(&libdsname,2) ; %if (%str(&lib) eq ) %then %do ; /* no name supplied ? */ %let lib = WORK ; /* default to WORK */ %let root = DS ; /* our root is DS */ %end ; %else %if (%str(&dsn) eq ) %then %do ; /* only one part supplied? */ %let root = &lib ; /* its the root name */ %let lib = WORK ; /* library is WORK */ %end ; %else %do ; /* both supplied */ %let root = &dsn ; /* use the supplied name for root */ %end ; %if (%length(&root) gt 8) %then %do; %let root = %substr(&root,1,8); %end; /* we want no more than 8 */ /* chars of the supplied name. */ %let root = &root.&sysdate. ; /* embed todays date in the name */ %let sluggo = %rand8(10) ; /* random number */ %let nancy = &lib..&root.&sluggo ; /* first try */ /* chances are nearly a billion to one we will find a unique name */ %do %while (%sysfunc(exist(&nancy))) ; /* loop till it doesnt exist */ /* gen number */ %let sluggo = %rand8(10) ; /* random number */ %let nancy = &lib..&root.&sluggo. ; /* make new name */ %end ; /* loop till it doesn't exist */ &nancy /* return the unique name */ %mend getdsn ; /* -------------------------------------------------------------- */