/* findavar.sas Daniel Brockman 070803 Find an unused variable name. */ * tested 070803 ; %macro findavar(dsn,root); options nomlogic nomprint nosymbolgen ; * we believe it works ; /* * When you have a data set and you don't know it's contents, and you * want to use an additional variable with it and without colliding * with a variable in the dataset, then use findavar. * * dsn = data set name * root = a few characters which will be the first characters of * the unique variable name. The first char of root must * be alphabetic, and the remaining characters may be * digits or underscore ("_"). * * findavar returns the name of the new unique variable in global macro * variable greturn. * * Lore: Based on an algorithm suggested by Amit Marwah. * */ %global greturn; %let greturn=error; %let dsid=%sysfunc(open(&dsn)); %let i=1 ; %let check=1 ; %do %while ( not (&check=0)) ; %let greturn=&root.&i ; %let check=%sysfunc(varnum(&dsid,&greturn)); %let i=%eval(&i+1); %end ; %let rc=%sysfunc(close(&dsid)); %put findavar dsn:&dsn new variable name:&greturn ; /* restore */ options mlogic mprint symbolgen ; %mend findavar ;