IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

SAS - How to generate a random number between 1 and n


IBM Mainframe Forums -> All Other Mainframe Topics
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Fri Nov 02, 2012 7:50 pm
Reply with quote

If you put multiple values in SEEDFILE in the last code I posted, SAS will read each one and generate a random number. Do you need to be able to write each of these random numbers to an output file as well?
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Fri Nov 02, 2012 8:45 pm
Reply with quote

My bad icon_redface.gif
Yup, i've successfully run multiple input records.

However, I need to break out fields on the same input record(s). The value(s) will not always be in position 1.

So far, i've been able to find/use a numeric value that is not in position 1 (@nnn).

Also needed is a way to generate the random number without the leading zeros suppressed icon_confused.gif

'Preciate it!

d
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Fri Nov 02, 2012 9:29 pm
Reply with quote

Using a trailing @@ on the INPUT line such as
Code:
     INPUT SEEDVAL  @@ ;
will cause SAS to start looking for the second value immediately after the end of the first value. Make sure you do NOT have line numbers in your file, though, or the line numbers will be read as data.

Using a Zn. format for the PUT statement will cause SAS to add leading zeroes. If you want the value to be 8 digits wide, for example, use
Code:
PUT RANVAL Z8. ;
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Fri Nov 02, 2012 10:24 pm
Reply with quote

Excellent - the Zn. did the trick!

Once again, thanks very much.

d
Back to top
View user's profile Send private message
Charles Wolters

New User


Joined: 30 Mar 2011
Posts: 48
Location: United States

PostPosted: Thu Nov 15, 2012 1:33 am
Reply with quote

Dick,

I have used the following approach where BOUND is your N value so here I want to select a random number between 1 and 25. Using the PARM keyword I am feeding the value of BOUND to SAS. The RANUNI function activates the SAS random number generator. The value of 959078 is the seed I have given to random number generator and this is arbitrary. The random number is written to the SAS log and I am not sure this is where you want it.

Charles Wolters

//S688CLWX JOB XXXUNKA9,S688CLW,
// MSGCLASS=R,NOTIFY=&SYSUID
// SET BOUND=25
//STEP1 EXEC SAS,PARM='SYSPARM="&BOUND"'
//SYSIN DD *
***PASS INTERVAL FOR RANDOM NUMBER GENERATION ;
DATA _NULL_ ;
RANNUM = INT(&SYSPARM * RANUNI(959078)) + 1 ;
CALL SYMPUT('MYNUM',PUT(RANNUM,8.)) ;
RUN ;
%PUT SELECTED RANDOM NUMBER = &MYNUM ;
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Thu Nov 15, 2012 2:17 am
Reply with quote

Hi Charles,

Thanks for the reply icon_smile.gif

The following is what i (thanks to the suppport from Robert and Vasanth) have running now:
Code:
DATA SEEDFILE;                                       
     INFILE SEEDFILE;                                 
     INPUT @014 SEEDVAL;                             
     SEED = INT(MOD (TIME(), SUBSTR(SEEDVAL,1)+0)) ; 
     RANVAL = INT(RANUNI(SEED)*SEEDVAL) + 1 ;         
     FILE RANOUT;                                     
     PUT ' RANDACT  ' RANVAL Z7. ; 

This does not use the "parm" but rather an input file and the random number is written to the RANOUT DD statement.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> All Other Mainframe Topics Goto page 1, 2  Next

 


Similar Topics
Topic Forum Replies
No new posts Pulling a fixed number of records fro... DB2 2
No new posts Substring number between 2 characters... DFSORT/ICETOOL 2
No new posts Generate random number from range of ... COBOL Programming 3
No new posts Increase the number of columns in the... IBM Tools 3
No new posts Random read in ESDS file by using RBA JCL & VSAM 6
Search our Forums:

Back to Top