/* getdsname.sas Daniel Brockman 070506 Get unique new Data Set Name */ %macro getdsname(libdsname) ; /* * Usage: %getdsname([lib[.dsname]|dsname]) ; * * Where lib is the name of the library in which to find a unique name. * If dsname is specified, then dsname will be returned, if no such data * set exists within the library. * 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. */ %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 ; %let nancy = &lib..&root ; /* first we try what we have */ %if (%length(&root) gt 14) %then %do; %let root = %substr(&root,1,14); * if we have to use the root again,; %end; * then we want no more than 14 ; * chars of it.; %let sluggo = 6345789 ; /* init */ /* 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 */ %mend getdsname ; /* ----------------------------------------------- */ ;