/* getdsname.sas Daniel Brockman 070506 Get unique new Data Set Name */ %macro getdsname(libdsname) ; options nomlogic nomprint nosymbolgen ; * we believe it works ; /* * Usage: %getdsname([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. * * getdsname returns a data set name, unique within the specified * library, in global variable greturn . * * The returned name has the form libname.datasetname . * * No data set by this name exists. * * If an extant data set exists, then getdsname won't return its name. * * 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 14) %then %do; %let root = %substr(&root,1,14); %end; * we want no more than 14 ; * chars of the supplied name.; %let sluggo = %substr(%sysfunc(ranuni(-6)),3,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 = %substr(%sysfunc(ranuni(-6)),3,10) ; %let nancy = &lib..&root.&sluggo. ; /* make new name */ %end ; /* loop till it doesn't exist */ %let greturn = &nancy ; /* return the unique name */ %put getdsname L69 new dataset name:&greturn ; /* restore */ options mlogic mprint symbolgen ; %mend getdsname ; /* ----------------------------------------------- */ ;