Need help creating randomly named dataset

Need help creating randomly named dataset

Post by rserme » Wed, 23 Aug 2006 03:34:33


I want to create a randomly named dataset so that I can run multiple
programs at the same time which currently call on modifying the same
dataset...

SITEBUTTON:
submit;
data random ;
x = ranuni(0) ;

x=roun(x*10);
run;
endsubmit;
submit continue;
proc sql NOPRINT;
select x into :num1 from work.random;
endsubmit;
submit;
data sasuser.classMAP&num1; set sasuser.tog;
...

The sasuser.classMAP&num1 always comes out with spaces after classMAP
and before random number.

Any suggestions on how to fix this or how to have multiple sessions
running with SAS/AF would be greatly appreciated.

-Thanks
 
 
 

Need help creating randomly named dataset

Post by Mterjeso » Wed, 23 Aug 2006 03:53:21

Hi,

I presume you might need a "d" added to
the round() function call.

The SELECT INTO will leave spaces in the
returned value in the macro variable.
The easiest way to trim leading and
trailing spaces after a SELECT INTO is
to just use the default behavior of the
%LET right afterwards. e.g.



data random ;
x = ranuni(0) ;
x = round(x*10);
run;

proc sql NOPRINT;
select x into :num1 from work.random;
quit;
%let num1=&num1; * trims leading/trailing spaces ;

%put >&num1<;




Depending upon when you run and how you
repeatedly run this, any chance of the same
initial seed getting the same value? If you
need all to be unique you may consider the
suffix to be a timestamp including milliseconds
with all the punctuation taken out.
e.g. DD:HH:MM:SS.SSS as DDHHMMSSSSS




Hope this is helpful.


Mark Terjeson
Senior Programmer Analyst, IM&R
Russell Investment Group


Russell
Global Leaders in Multi-Manager Investing







-----Original Message-----
From: SAS(r) Discussion [mailto: XXXX@XXXXX.COM ] On Behalf Of
XXXX@XXXXX.COM
Sent: Monday, August 21, 2006 11:35 AM
To: XXXX@XXXXX.COM
Subject: Need help creating randomly named dataset


I want to create a randomly named dataset so that I can run multiple
programs at the same time which currently call on modifying the same
dataset...

SITEBUTTON:
submit;
data random ;
x = ranuni(0) ;

x=roun(x*10);
run;
endsubmit;
submit continue;
proc sql NOPRINT;
select x into :num1 from work.random;
endsubmit;
submit;
data sasuser.classMAP&num1; set sasuser.tog;
...

The sasuser.classMAP&num1 always comes out with spaces after classMAP
and before random number.

Any suggestions on how to fix this or how to have multiple sessions
running with SAS/AF would be greatly appreciated.

-Thanks

 
 
 

Need help creating randomly named dataset

Post by clinton.ri » Wed, 23 Aug 2006 04:54:03

As Mark points out, random numbers can be tricky. You might want to consider using the user id and the SAS system start date/time as part of the data set name:


%put &sysuserid &sysdate &systime;
%let new_dsn = work.mydata_&sysuserid._&sysdate._%sysfunc(compress(&systime,":"));
data &new_dsn;
run;


I would think the odds of a single user starting 2 SAS sessions at the same time would be remote at best if not impossible. If you are really concerned about it you should check for the existence of the file (using the exist function) and add an additional suffix to make it unique.

Clint

-----Original Message-----
From: SAS(r) Discussion [mailto: XXXX@XXXXX.COM ]On Behalf Of
Terjeson, Mark (IM&R)
Sent: Monday, August 21, 2006 2:53 PM
To: XXXX@XXXXX.COM
Subject: Re: Need help creating randomly named dataset


Hi,

I presume you might need a "d" added to
the round() function call.

The SELECT INTO will leave spaces in the
returned value in the macro variable.
The easiest way to trim leading and
trailing spaces after a SELECT INTO is
to just use the default behavior of the
%LET right afterwards. e.g.



data random ;
x = ranuni(0) ;
x = round(x*10);
run;

proc sql NOPRINT;
select x into :num1 from work.random;
quit;
%let num1=&num1; * trims leading/trailing spaces ;

%put >&num1<;




Depending upon when you run and how you
repeatedly run this, any chance of the same
initial seed getting the same value? If you
need all to be unique you may consider the
suffix to be a timestamp including milliseconds
with all the punctuation taken out.
e.g. DD:HH:MM:SS.SSS as DDHHMMSSSSS




Hope this is helpful.


Mark Terjeson
Senior Programmer Analyst, IM&R
Russell Investment Group


Russell
Global Leaders in Multi-Manager Investing







-----Original Message-----
From: SAS(r) Discussion [mailto: XXXX@XXXXX.COM ] On Behalf Of
XXXX@XXXXX.COM
Sent: Monday, August 21, 2006 11:35 AM
To: XXXX@XXXXX.COM
Subject: Need help creating randomly named dataset


I want to create a randomly named dataset so that I can run multiple
programs at the same time which currently call on modifying the same
dataset...

SITEBUTTON:
submit;
data random ;
x = ranuni(0) ;

x=roun(x*10);
run;
endsubmit;
submit continue;
proc sql NOPRINT;
select x into :num1 from work.random;
endsubmit;
submit;
data sasuser.classMAP&num1; set sasuser.tog;
...

The sasuser.classMAP&num1 always comes out with spaces after classMAP
and before random number.

Any suggestions on how to fix this or how to have multiple sessions
running with SAS/AF would be greatly appreciated.

-Thanks
 
 
 

Need help creating randomly named dataset

Post by jim2sta » Wed, 23 Aug 2006 15:49:39

Hi Rsermer,

An old piece of code that I developed in the ancient days of SAS 6.12,
that may not have been programmed most optimal, but that still works, is:

%*~~~~~~~~~~~~~~~~~~~~;
%* Temporary FileName ;
%*____________________;

%LET TempDir = %QUOTE(%SYSGET(TEMP)); %* Get contents of TEMP
var;
%IF ( &TempDir NE ) %THEN
%DO; %* If TEMP var with dir
found;
%IF ( %SUBSTR(&TempDir,%LENGTH(&TempDir),1) NE \ ) %THEN
%DO;
%LET TempDir = &TempDir.\; %* Add trailing \ to dir;
%END;
%END;
%FmtFile:
%LET FileName = ___%SYSFUNC(TIME());%* Add random time to FileName;
%IF ( %SYSFUNC ( FILEEXIST(&TempDir.&FileName) ) ) %THEN GOTO FmtFile;

It generates a random file name plus extension for a non-existent file in
the Windows TEMP directory or in the current directory. If you remove the
period in the name using COMPRESS you can use it, with additional
adaptations on checking for such an existing dataset, as a long random
dataset name.

Regards - Jim.

On Mon, 21 Aug 2006 11:34:33 -0700, XXXX@XXXXX.COM < XXXX@XXXXX.COM >
 
 
 

Need help creating randomly named dataset

Post by peter.craw » Wed, 23 Aug 2006 21:06:00

erhaps it isn't so much trouble.....

when these steps are started within the same session, use no
data set name on the data statement! You get a session-controlled
unique data set name in the dataN series. (which can be identified
after the "run;" by %let that_one = &syslast ; ) For doc, see
http://support.sas.com/onlinedoc/913/getDoc/en/lrdict.hlp/a000188132.htm#a0
02503650 (which may wrap. If so, try http://tinyurl.com/hmbfz )

If these steps are not in the same session, there would be no name
contention even if their names are the same (while your sessions
keep unique control of the library path ... like work )

Perhaps I just don't understand ... but can't see any other need
for this randomisation, than uniqueness to support concurrent
activity.
I can see the situation when two steps could start at exactly the
same time. Where for example, these steps are waiting for some
event like the existence of a file.
I think a random dataset name is a sledge hammer to crack a nut!

Good Luck

peter

On Mon, 21 Aug 2006 15:54:03 -0400, Rickards, Clinton (GE Money)
< XXXX@XXXXX.COM > wrote:

consider using the user id and the SAS system start date/time as part of
the data set name:
(&systime,":"));
same time would be remote at best if not impossible. If you are really
concerned about it you should check for the existence of the file (using
the exist function) and add an additional suffix to make it unique.
 
 
 

Need help creating randomly named dataset

Post by Scott Bas » Wed, 23 Aug 2006 21:45:23


By default SAS is formatting num using the best. format before copying it to
the macro variable, so it has leading spaces. Use the "trick" of "select x
into :num separated by ' ' " (even though you're just returning a single
number) to eliminate leading and trailing spaces.

However, you can skip the dataset and proc sql altogether by coding:

%let x=%sysfunc(round(%sysfunc(ranuni(0))*10));
%put &x;

Read up on %sysfunc for more details.

HTH,
Scott


Not sure what you mean by "multiple sessions running with SAS/AF"; can you
clarify what you mean and perhaps someone else can answer?
 
 
 

Need help creating randomly named dataset

Post by minio » Wed, 23 Aug 2006 23:07:16


A data statement on its own will create work.data1, work.data2, etc.
e.g. :

1 data ;
2 set sashelp.class ;
3 run ;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.DATA1 has 19 observations and 5 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds


4 data ;
5 set sashelp.class ;
6 run ;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.DATA2 has 19 observations and 5 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds


7 data ;
8 set sashelp.class ;
9 run ;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.DATA3 has 19 observations and 5 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds
 
 
 

Need help creating randomly named dataset

Post by jim2sta » Wed, 23 Aug 2006 23:25:35

i friends,

Every separate SAS session, whether interactive (yes, two or more GUI's of
SAS can be started!) or batch create their own random subdirectory name in
the SAS work directory, under which each session stores its own temporary
datasets and format catalogs. So, even if names are the same between
sessions, these are in different subdirectories under the work directory.

So, the $1000 question is: what purpose is served by the random dataset
names? Are those to be stored in permanent storage, i.e. non-work
subdirectories (explicitly named libraries) or not?

Regards - Jim.
On Tue, 22 Aug 2006 07:07:16 -0700, minion < XXXX@XXXXX.COM > wrote:

it to
of "select x
you
 
 
 

Need help creating randomly named dataset

Post by Mterjeso » Thu, 24 Aug 2006 00:06:10

i Again,

If your program is going to be run sequentially,
and you would like a simple/clean suffix number
that is a straightforward sequence number, then
you could check the directory to see what the
largest number is and increment it.

You can run the following two steps over and over
and watch your sequence number grow.....





%let MyPath=c:\temp;
%let MyFile=apple;



* Get Next Sequence Number From Directory ;
filename dir "&MyPath";
data _null_;
length memname $200;
retain largest 0;
did = dopen('dir');
do i = 1 to dnum(did);
memname = dread(did,i);
if index(memname,'.sas7bdat')
and substr(memname,1,length("&MyFile")) eq "&MyFile" then
do;
substr(memname,length("&MyFile"),1) = '.';
largest = max(largest,input(scan(memname,2,'.'),best.));
end;
end;
rc = dclose(did);
call symput('NextSeqNum',compress(put(largest+1,best.)));
run;
%put NextSeqNum is >&NextSeqNum<;


* make another dataset with sequence number ;
libname mylib "&MyPath";
data mylib.apple&NextSeqNum;
a=1;
run;





Hope this is helpful.


Mark Terjeson
Senior Programmer Analyst, IM&R
Russell Investment Group


Russell
Global Leaders in Multi-Manager Investing









-----Original Message-----
From: SAS(r) Discussion [mailto: XXXX@XXXXX.COM ] On Behalf Of
Peter Crawford
Sent: Tuesday, August 22, 2006 5:06 AM
To: XXXX@XXXXX.COM
Subject: Re: Need help creating randomly named dataset


perhaps it isn't so much trouble.....

when these steps are started within the same session, use no data set
name on the data statement! You get a session-controlled unique data set
name in the dataN series. (which can be identified
after the "run;" by %let that_one = &syslast ; ) For doc, see
http://support.sas.com/onlinedoc/913/getDoc/en/lrdict.hlp/a000188132.htm
#a0
02503650 (which may wrap. If so, try http://tinyurl.com/hmbfz )

If these steps are not in the same session, there would be no name
contention even if their names are the same (while your sessions keep
unique control of the library path ... like work )

Perhaps I just don't understand ... but can't see any other need for
this randomisation, than uniqueness to support concurrent activity. I
can see the situation when two steps could start at exactly the same
time. Where for example, these steps are waiting for some event like the
existence of a file. I think a random dataset name is a sledge hammer to
crack a nut!

Good Luck

peter

On Mon, 21 Aug 2006 15:54:03 -0400, Rickards, Clinton (GE Money)
< XXXX@XXXXX.COM > wrote:

consider using the user id and the SAS system start date/time as part of
the data set name:
(&systime,":"));
same time would be remote at best if not impossible. If you are really
concerned about it you should check for the existence of the file (using
the exist function) and add an additional suffix to make it unique.
 
 
 

Need help creating randomly named dataset

Post by martin.u.m » Thu, 24 Aug 2006 01:25:19

Provided you run your program instances as separate (batch) sessions,
instead of deriving something from time, just use &sysjobid. as a unique
identifier?

MM

On Mon, 21 Aug 2006 15:54:03 -0400, Rickards, Clinton (GE Money)


consider using the user id and the SAS system start date/time as part of
the data set name:
(&systime,":"));
same time would be remote at best if not impossible. If you are really
concerned about it you should check for the existence of the file (using
the exist function) and add an additional suffix to make it unique.
 
 
 

Need help creating randomly named dataset

Post by tobydun » Thu, 24 Aug 2006 01:34:48

am not too sure but I dont think SAS will let you modify the same data set
with more than one step at a time. But lets say you could, you still have
to worry about what gets updated when from what program not to mention how
SAS will handle any column constraints, indexs, and the sort order.



Toby Dunn

When everything is coming at you all at once, your in the wrong lane.

A truly happy person is someone who can smile and enjoy the scenery on a
detour.





From: Martin Mathis < XXXX@XXXXX.COM >
Reply-To: Martin Mathis < XXXX@XXXXX.COM >
To: XXXX@XXXXX.COM
Subject: Re: Need help creating randomly named dataset
Date: Tue, 22 Aug 2006 12:25:19 -0400

Provided you run your program instances as separate (batch) sessions,
instead of deriving something from time, just use &sysjobid. as a unique
identifier?

MM

On Mon, 21 Aug 2006 15:54:03 -0400, Rickards, Clinton (GE Money)
< XXXX@XXXXX.COM > wrote:

>As Mark points out, random numbers can be tricky. You might want to
consider using the user id and the SAS system start date/time as part of
the data set name:
>
>
>%put &sysuserid &sysdate &systime;
>%let new_dsn = work.mydata_&sysuserid._&sysdate._%sysfunc(compress
(&systime,":"));
>data &new_dsn;
>run;
>
>
>I would think the odds of a single user starting 2 SAS sessions at the
same time would be remote at best if not impossible. If you are really
concerned about it you should check for the existence of the file (using
the exist function) and add an additional suffix to make it unique.
>
>Clint
>
>-----Original Message-----
>From: SAS(r) Discussion [mailto: XXXX@XXXXX.COM ]On Behalf Of
>Terjeson, Mark (IM&R)
>Sent: Monday, August 21, 2006 2:53 PM
>To: XXXX@XXXXX.COM
>Subject: Re: Need help creating randomly named dataset
>
>
>Hi,
>
>I presume you might need a "d" added to
>the round() function call.
>
>The SELECT INTO will leave spaces in the
>returned value in the macro variable.
>The easiest way to trim leading and
>trailing spaces after a SELECT INTO is
>to just use the default behavior of the
>%LET right afterwards. e.g.
>
>
>
>data random ;
> x = ranuni(0) ;
> x = round(x*10);
>run;
>
>proc sql NOPRINT;
> select x into :num1 from work.random;
>quit;
>%let num1=&num1; * trims leading/trailing spaces ;
>
>%put >&num1<;
>
>
>
>
>Depending upon when you run and how you
>repeatedly run this, any chance of the same
>initial seed getting the same value? If you
>need all to be unique you may consider the
>suffix to be a timestamp including milliseconds
>with all the punctuation taken out.
>e.g. DD:HH:MM:SS.SSS as DDHHMMSSSSS
>
>
>
>
>Hope this is helpful.
>
>
>Mark Terjeson
>Senior Programmer Analyst, IM&R
>Russell Investment Group
>
>
>Russell
>Global Leaders in Multi-Manager Investing
>
>
>
>
>
>
>
>-----Original Message-----
>From: SAS(r) Discussion [mailto: XXXX@XXXXX.COM ] On Behalf Of
> XXXX@XXXXX.COM
>Sent: Monday, August 21, 2006 11:35 AM
>To: XXXX@XXXXX.COM
>Subject: Need help creating randomly named dataset
>
>
>I want to cre
 
 
 

Need help creating randomly named dataset

Post by davidlcass » Thu, 24 Aug 2006 05:02:59


In addition to other people's suggestions, let me make a couple points.

Your 'random dataset' approach is not very sound. You only get 11 possible
values out (0 to 10), and 9 of them have a likelihood of 10% . That means
that at any given time, the chance that the *next* data set will have the
same name as the current one is about 1 in 10. To be precise,

prob (nextnum = currentnum) =

prob(nextnum = currentnum | currentnum=1)*prob(currentnum=1) +
prob(nextnum = currentnum | currentnum=2)*prob(currentnum=2) +
prob(nextnum = currentnum | currentnum=3)*prob(currentnum=3) +
prob(nextnum = currentnum | currentnum=4)*prob(currentnum=4) +
prob(nextnum = currentnum | currentnum=5)*prob(currentnum=5) +
prob(nextnum = currentnum | currentnum=6)*prob(currentnum=6) +
prob(nextnum = currentnum | currentnum=7)*prob(currentnum=7) +
prob(nextnum = currentnum | currentnum=8)*prob(currentnum=8) +
prob(nextnum = currentnum | currentnum=9)*prob(currentnum=9) +

prob(nextnum = currentnum | currentnum=0)*prob(currentnum=0) +
prob(nextnum = currentnum | currentnum=10)*prob(currentnum=10) =

9 * .10 * .10 +
2 * .05 * .05

= .09 + .005 = .095

And that happens every time you do this. So, if you do this 20 times
in your programs, the odds of never over-writing your data due to having
the same data set name twice in a row is:

(1 - .095)**20 = 0.1358

so, the likelihood that one of your data sets gets trashed by this process
is now 86 percent. On each run. This is double-plus-ungood.

So your idea is fundamentally unsound.

Then, your approach is not good either. If you are running multiple
sessions hitting the same database, you need to look into database
and record locking issues. If you run separate sessions, the data sets
are in separate directories anyway, so the purpose of this escapes me.
If you are running a single SAS session and hitting a database multiple
times for the same reason, you are probably in need of a process
re-design.

Perhaps, if you write back to SAS-L (not to me personally) and explain
what your larger goal is, then someone might be able to give you some
better advice.

HTH,
David
_________________________________________________________________
Windows Live Spaces is here! It easy to create your own personal Web site.
http://www.yqcomputer.com/