SUPPORT / SAMPLES & SAS NOTES
 

Support

Sample 26182: Creating Subfolders Using Hash Tables

DetailsAboutRate It
This sample will create subdirectories "on the fly" by walking down the hierarchy and creating the subdirectory if it doesn’t exist. It also demonstrates iterating through a hash table. It is easily adaptable as a macro function.

   /******** BEGIN CODE ********/


   /* Set a couple of macro variables: Parent directory and fiscal year */
   %LET mreports = C:\temp\reports; * parent directory;
   %LET mfy = 2008;                 * fiscal year;

   DATA _null_;

        /* Set the length of  character variables */
	LENGTH d dir $ 80 fsep $ 1;

        /* fsep is character to separate directories */
	RETAIN fsep "\";

        /* Create the full path name (uses v.9 catx() function) something like: c:\temp\reports\09jan2007\FY_2008 */
	d = Catx(fsep,"&mreports",Put(Today(),DATE9.), Catt('FY_',"&mfy"));

        /* _n_ not necessary here, but... */
	IF _n_ EQ 1 THEN DO; * define path hash table ;

                /* Declare the hash table, Declare the iteratator */
		DECLARE hash hdir(hashexp: 3, ordered:"yes");
		DECLARE hiter idir('hdir');

           /* Define the key, p variable*/ 
	   rc = hdir.defineKey('p');

           /* Define the data, dir variable and Define Done */
	   rc = hdir.defineData('dir');
	   rc = hdir.defineDone();

                /* Avoid annoying log messages */
		CALL missing(p,dir);
	END;



        /* Initialize the end of folder pointer, l */
	len = Length(d);

        /* Now break each folder (dir) into the hash table */
	DO UNTIL(len LE 0); * parse the path, put in hash table;

                /* Find next deepest folder (-l starts at end and searches forward) */
		p = Find(d,fsep,"it", -len) ;

                /* Set dir to what’s between the two fsep (“\”) */
		dir = Substr(d, IfN(p=0, 1,p+1), len-p);

                /* If the pointers at the start set the parent, */
		IF p = 0 THEN par = dir;

                /* Otherwise add the folder to the hash table */
		ELSE rc = hdir.add(key: p, data: dir);

                /* And decrement for next folder */
		len = p-1;
	END;



        /* Fird the first folder */
	rc = idir.first();

      	DO WHILE(rc=0);

                /* Try to create it using DCreate() */
		newdir = Trimn(dcreate(dir, par));

                /* If it already exists (newdir is blank), set it yourself */
		IF newdir = " " THEN newDir = Catx('\',par,dir);

                /* Set parent to the new directory */
		par = newdir;

		/* And go again */
                rc = idir.next();

        /* And done. */
	END;

   RUN;


   /******** END CODE ********/

Reference Documentation:

Links for the new functions:

page divider

About the Author
Richard Wright began writing SAS code over 25 years ago keying code and data on punch cards as a student at University of Oregon. Since then he has worked at various agencies for the state of Texas. He is certified in Base and Advanced SAS.


These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.