Portal | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref

 SAS - How to generate a random number between 1 and n Goto page 1, 2  Next
Author Message
dick scherrer

Moderator Emeritus

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

 Posted: Tue Sep 11, 2012 9:15 am    Post subject: SAS - How to generate a random number between 1 and n Howdy, Ya never know what will come thru the door . . . One of the programming groups asked if i knew how to do this using SAS - i don't. They need to pull a sample from a universe of several million entries. The rules they are to follow (specified by those who receive the sample) is that they are to generate a random number between 1 and n and once they have this initial number, they are to read the data and extract every nth entry. The Rules document mentions SAS several places and that team lead would like to use it for brownie points. I can count on less than 1 hand the "things" i've done with SAS and these were clones of something that was already running. Went to the SAS site and their "hits" are more comprehensive than i need. Our forum has over 500 SAS topics, but only 2 contain "random" . . . What i'd like to do is run a SAS process that takes in a parm or sysin specifying 'n' and "writes" the random number. Thanks, d

Bill O'Boyle

CICS Moderator

Joined: 14 Jan 2008
Posts: 2504
Location: Atlanta, Georgia, USA

 Posted: Tue Sep 11, 2012 9:47 am    Post subject: Reply to: SAS - How to generate a random number between 1 an Dick, I Googled "SAS Random Number Generator Function" and got several hits, one example using the SAS "RAND" function. HTH....
Robert Sample

Global Moderator

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

Posted: Tue Sep 11, 2012 5:13 pm    Post subject:

 Code: //STEP1 EXEC SAS,PARM='SYSPARM=' //RANOUT DD DISP=SHR,DSN=.... //SYSIN DD * DATA _NULL_;      SEED = SUBSTR("&SYSPARM",1)+0 ;      RANVAL = INT(RANUNI(SEED)*N) + 1 ;      FILE RANOUT;      PUT RANVAL ;
This code is not tested but other than possible syntax issues it should meet the need. RANUNI returns a uniform random number between 0 and 1. To scale a random number between 0 and 1 to the range M to N, multiply the random number by N, take the integer value of that (which gives a range of 0 to N) and add M to result so the range becomes M to M+N.
dick scherrer

Moderator Emeritus

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

Posted: Tue Sep 11, 2012 6:36 pm    Post subject: Reply to: SAS - How to generate a random number between 1 an

Thanks guys,

@Bill - yup, found those and several others that left me thinking Whoa all i wanted was one leetle number

@Robert - ran the code you posted and this is what was returned:
 Code: The SAS System 44444444444444444444444444444444444444444444444444444444444E884ECE4EAAA89 0000000000000000000000000000000000000000000000000000000000038502120282354 _________________________________________________________________________ .                                                                        4444444444444444444444444444444444444444444444444444444444444444444444444 B000000000000000000000000000000000000000000000000000000000000000000000000

Multiple runs give the x'4B'. . .

Thanks,

d
Robert Sample

Global Moderator

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

Posted: Tue Sep 11, 2012 6:43 pm    Post subject:

The dot means the value is missing. You need to substitute N in the code with whatever your maximum value is. If you did that, add
 Code: PUT _ALL_ ;
before the FILE RANOUT; statement so I can see which SAS variable is not defined. That will list all the values in the DATA step on your SAS log.
dick scherrer

Moderator Emeritus

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

Posted: Tue Sep 11, 2012 7:01 pm    Post subject:

Hi Robert,

Added the _ALL_ and now i get:
 Code: The SAS System 44444444444444444444444444444444444444444444444444444444444E884ECE4EAAA89 0000000000000000000000000000000000000000000000000000000000038502120282354 _________________________________________________________________________ .                                                                        4444444444444444444444444444444444444444444444444444444444444444444444444 B000000000000000000000000000000000000000000000000000000000000000000000000 _________________________________________________________________________ SEED=55 RANVAL=. N=. _ERROR_=0 _N_=1                                      ECCC7FF4DCDECD744D7446CDDDD67F46D67F4444444444444444444444444444444444444 2554E550915513EB05EB0D59969DE00D5DE10000000000000000000000000000000000000

I used 55 for the "seed" value.
Robert Sample

Global Moderator

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

Posted: Tue Sep 11, 2012 8:07 pm    Post subject:

If you want random numbers in the range 1 to 20, change
 Code: RANVAL = INT(RANUNI(SEED)*N) + 1 ;
to
 Code: RANVAL = INT(RANUNI(SEED)*20) + 1 ;
 Code: N = 20 ;
before this statement.
dick scherrer

Moderator Emeritus

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

Posted: Tue Sep 11, 2012 9:30 pm    Post subject: Reply to: SAS - How to generate a random number between 1 an

Hi Robert,

Here's what did the deed:
 Code: //SASPROC  EXEC PROC=SAS,                    //         PARM.SAS='SYSPARM=200'            //RANOUT   DD SYSOUT=*                      //SAS.SYSIN DD *                            DATA _NULL_;                                     SEED = SUBSTR("&SYSPARM",1)+0 ;             RANVAL = INT(RANUNI(SEED)*209) + 1 ;         FILE RANOUT;                                 PUT RANVAL ;                                 PUT _ALL_  ;                            //

'Preciate it

Just anticipatin' . . . What if they want the random number to change even if they specify the same seed? As it is now (which is what i expected) running the same seed multpile times returns the same random number.

d
Robert Sample

Global Moderator

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

Posted: Tue Sep 11, 2012 10:02 pm    Post subject:

 Quote: What if they want the random number to change even if they specify the same seed?
The whole point of the seed is that the random number sequence won't change unless the seed changes. I would accomplish what you are saying by doing something like
 Code: SEED = INT(MOD (TIME(), SEED)) ;
as the line before RANVAL=. This will cause the seed value to be dependent upon the time of day as well as the parameter, so you will get different random numbers even though the parameter is not changed.
vasanthz

Global Moderator

Joined: 28 Aug 2007
Posts: 1690
Location: Tiruppur, India

Posted: Tue Sep 11, 2012 11:30 pm    Post subject:

Hi,
Here is a code similar to Robert's idea using 0 as seed,
When we use 0 as seed, the RANUNI function automatically takes time as the seed and generated random numbers which are truly random.
 Code: %let sysparm = 20; /* When this parameter is passed via JCL, remove this line. */ data wells; RANVAL=ceil(ranuni(0)*&sysparm) ; /* FILE RANOUT; */ put RANVAL; run;

Output when the above program was run in a loop for 20 times:
 Code: 16 7 8 12 9 13 16 14 8 18 2 16 4 14 3 13 9 13 4 8

Hope it helps,
dick scherrer

Moderator Emeritus

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

Posted: Wed Sep 12, 2012 1:31 am    Post subject: Reply to: SAS - How to generate a random number between 1 an

Hi Guys,

@Robert
made a slight mod to latest SEED=
 Code: SEED = INT(MOD (TIME(), SUBSTR("&SYSPARM",1)+0)) ;

When i used the posted line, i was back to the x'4B'. With the mod, it appears to work as advertised

@Vasanth
 Code: DATA WELLS;                                           RANVAL=CEIL(RANUNI(0)*&SYSPARM) ;               FILE RANOUT; */                                 PUT RANVAL;                                     PUT _ALL_  ;
This also generates different random numbers from the same parm.

I'll talk with the people with this "opportunity" and explain they are welcome to both the "same value" code or the "different each run code".

Thanks to you both!

When the scope "creeps" i may be back

d
dick scherrer

Moderator Emeritus

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

 Posted: Thu Nov 01, 2012 12:22 am    Post subject: Reply to: SAS - How to generate a random number between 1 an Yup, scope has creeped and i'm looking for what (i hope) is a very trivial bit of info. First, thanks again for all of the help earlier. 'Tis working just as was desired. However, now the requirement has changed a bit and instead of using sysparm, the value needs to be read from a data file and used in the same way "sysparm" was used originally. Suggestions? Also, i've been to the SAS site and they sure do have the documentation available - an overwhelming amount. With my extremely limited SAS exposure, i have/had no idea which 1 or few manuals might be good for a SAS beginner (me). Is there a for-pay book that anyone might recommend? Looks like i'd best prepare for more of these . . . d
Robert Sample

Global Moderator

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

 Posted: Thu Nov 01, 2012 12:30 am    Post subject: Look for The Little SAS Book: A Primer which is a good starting point, albeit a pricy starting point. There's a SAS for Dummies book on Amazon but I haven't looked at it so I'm not sure how good it is.
dick scherrer

Moderator Emeritus

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

 Posted: Thu Nov 01, 2012 12:38 am    Post subject: Reply to: SAS - How to generate a random number between 1 an Thanks Robert, The Amazon price is now below \$40. Any suggestion on how to use a value from an input file instead of sysparm? d
Robert Sample

Global Moderator

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

Posted: Thu Nov 01, 2012 12:38 am    Post subject:

 Code: DATA WELLS;                                           INFILE SEEDFILE ;      INPUT SEEDVAL ;      RANVAL=CEIL(RANUNI(0)*SEEDVAL) ;               FILE RANOUT; */                                 PUT RANVAL;                                     PUT _ALL_
will work as long as SEEDVAL is something SAS can recognize as a number. JCL would be (using 12345 for the seed)
 Code: //SEEDFILE DD * 12345 /*
although SEEDFILE could point to a file, too.
vasanthz

Global Moderator

Joined: 28 Aug 2007
Posts: 1690
Location: Tiruppur, India

 Posted: Thu Nov 01, 2012 12:53 am    Post subject: Hi D, "Little SAS book" is one of the good SAS tutorials. I have the "SAS for Dummies" (black and yellow cover with a cartoon guy's face in front) but the book keeps beating around the bush and does not really gets into serious programming whatsoever, lengthy as well. I have attached one of the PDF(50 actual pages & available free) after which little SAS book can be more useful. . The PDF would say "Advanced SAS tutorial" but trust me, its very basic and interesting with examples. It has some information relating to PC SAS, but PC sas and mainframe SAS are the same in terms of program. Many of my collegues have found it very useful for beginning. Hope it helps some more people.
dick scherrer

Moderator Emeritus

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

 Posted: Thu Nov 01, 2012 1:16 am    Post subject: Reply to: SAS - How to generate a random number between 1 an Thanks again! Worked like a charm! Both with a dd * and an actual dsn. Later, d
dick scherrer

Moderator Emeritus

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

 Posted: Thu Nov 01, 2012 1:18 am    Post subject: Reply to: SAS - How to generate a random number between 1 an Thanks Vasanth, I've downloaded the pdf and will check it out. d
Robert Sample

Global Moderator

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

 Posted: Thu Nov 01, 2012 1:20 am    Post subject: Great -- glad to hear it worked.
dick scherrer

Moderator Emeritus

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

 Posted: Fri Nov 02, 2012 7:06 pm    Post subject: Reply to: SAS - How to generate a random number between 1 an Heee's Baaaack . . . . One (hopefully) last "opportunity" on this (Ha). My "users" stopped by yesterday and 'splained that what they would really like is the ability to process multiples. I spent time last night looking thru our posts and the internet and did not find an example of SAS code that read an qsam file (with multiple fields). I seem to recall that at least one of our topics had a SAS external file definition that had individual fields, but i was unable to find it. Over the weekend, i'll order "the little sas book" and download some more "stuff". d
 All times are GMT + 6 HoursGoto page 1, 2  Next
 Page 1 of 2

Search our Forum:

 Topic Author Forum Replies Posted Similar Topics xml-generate jzhardy COBOL Programming 0 Fri Sep 18, 2020 12:05 pm Sort to construct single record from ... Deepti_R SYNCSORT 3 Wed Nov 13, 2019 12:14 am recursive SQL to generate list jzhardy DB2 5 Sun Apr 07, 2019 8:38 am count the number of records from diff... janmejay CA Products 4 Mon Mar 25, 2019 11:29 pm Add week number to a file name or rep... tejdeep0274 JCL & VSAM 2 Thu Jan 03, 2019 6:49 am

 © 2003-2020 IBM MAINFRAME Software Support Division
 Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us