IBM Mainframe Forum Index
 
Register
 
IBM Mainframe Forum Index Mainframe: Search Log in to check your private messages Log in
 

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


Goto page Previous  1, 2  
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: 8568
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: 19251
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: 8568
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: 19251
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: 19251
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 All times are GMT + 6 Hours
Goto page Previous  1, 2
Forum Index -> All Other Mainframe Topics
Page 2 of 2

 

Search our Forum:

Similar Topics
Topic Forum Replies
No new posts XML-GENERATE to render edited numeric COBOL Programming 0
No new posts Sort to construct single record from ... SYNCSORT 3
No new posts recursive SQL to generate list DB2 5
No new posts count the number of records from diff... CA Products 4
No new posts Add week number to a file name or rep... JCL & VSAM 2

Back to Top