LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (May 2007, week 2)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Thu, 10 May 2007 20:13:14 +0000
Reply-To:     toby dunn <tobydunn@HOTMAIL.COM>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         toby dunn <tobydunn@HOTMAIL.COM>
Subject:      Re: 'Local' datasets in Macro?
Comments: To: iw1junk@COMCAST.NET
In-Reply-To:  <051020071959.6431.464379B6000614140000191F220076106405029A06CE9907@comcast.net>
Content-Type: text/plain; format=flowed

Ian ,

Nope not screaming yet the number is large enough to avoid a name collision problem unless one is doing something ungodly that they would need to stop and rethink their abilities as a programmer.

I did have one minor thingy you missed a semi colon when you assiged Macro variable r lol....

%macro q (len=14) ; %local r ; %let r = %sysevalf(10**14 * %sysfunc(ranuni(444))) ; %let len = %sysfunc(min(&len,%length(&r))) ; %substr(&r,1,&len) %mend ;

Toby Dunn

On the other hand, you have different fingers. ~ LCG

The early bird may get the worm, but the second mouse gets the cheese in the trap. ~ LCG

What happens if you get scared half to death, twice? ~ LCG

From: Ian Whitlock <iw1junk@COMCAST.NET> Reply-To: iw1junk@COMCAST.NET To: SAS-L@LISTSERV.UGA.EDU Subject: Re: 'Local' datasets in Macro? Date: Thu, 10 May 2007 19:59:50 +0000

Summary: DATAnn convention does not guarantee unique names. #iw-value=1

In response to the question how to guarantee that work data set names are unique. Don Henderson and others have suggested that the names be left to the default SAS names DATAnn. SAS guarantees that it will not override it's own default named data sets, not that it won't clobber your data sets.

Consider the macro

%macro q ; data ; y = 1 ; run ; %mend q ;

And a consumer who sets up some data

data data2 ; x = "my data" ; run ;

And has the misfortune to call for Q's services twice.

%q %q

DATA2 with the variable X is gone!

The problem is really a more general one, SAS does not have a concept of name spaces. Hence most named things can be clobbered by the careless use of names. In fact the whole problem has nothing to do with macro, but rather large programs which often contain macros.

Long ago on SAS-L Paul Dorfman suggest a random tag for macro variable names based on RANUNI. Consider

%macro q (len=14) ; %local r ; %let r = %sysevalf(10**14 * %sysfunc(ranuni(444))) %let len = %sysfunc(min(&len,%length(&r))) ; %substr(&r,1,&len) %mend ;

The first time Q is called a seed based on the clock time is created. There after SAS manages the seed so one is guaranteed a "random" stream of numbers with successive calls to Q. Hence one might write

%let dsid = %q() ; data w&dsid ; ... run ;

and PROBABLY have a unique name. (I can hear Toby screaming, "NO that is not perfect." If only he would write the program to test how many data set names must be created on average to get a collision of names, and then run it until a collision is realized, we could get on with some greater worries.)

Here is the log for creating a subdirectory of the work directory and then putting a simple data set in it. Unfortunately the work will *NOT* be wiped out when the SAS session is closed, probably due to containing a subdirectory.

179 %let unq = %q() ; 180 %let path = %sysfunc(pathname(work))\&unq ; 181 %***put path=&path ; 182 %let rc = %sysfunc(system( md "&path" )) ; 183 %let ulib = %q(len=7) ; 184 libname q&ulib "&path" ; NOTE: Libref Q2671566 was successfully assigned as follows: Engine: V9 Physical Name: C:\DOCUME~1\IANWHI~1\LOCALS~1\Temp\SAS Temporary Files\_TD1028\57702832556191 185 186 data q&ulib..w ; x = 1 ; run ;

NOTE: The data set Q2671566.W has 1 observations and 1 variables.

Ian Whitlock

_________________________________________________________________ Catch suspicious messages before you open them—with Windows Live Hotmail. http://imagine-windowslive.com/hotmail/?locale=en-us&ocid=TXT_TAGHM_migration_HM_mini_protection_0507


Back to: Top of message | Previous page | Main SAS-L page