Help using Base SAS procedures

Generating Random Numbers

Reply
N/A
Posts: 0

Generating Random Numbers

Is there a function that will generate a random number with decimal places within a certain range?

For example, I need to generate a random number between -12.345 and 67.890. This number must have 3 decimal places, and can be positive or negative, as long as it's within the range given.


Much obliged! :-)
Suzy
Regular Contributor
Posts: 165

Re: Generating Random Numbers

Something like this would work.

var=round(ranuni(0)*(12.345+67.890),0.001)-12.345;

To generalize it you would need to create a macro or use proc fcmp.
N/A
Posts: 0

Re: Generating Random Numbers

Thank you, Rick, that's awesome!


Suzy
Super Contributor
Super Contributor
Posts: 3,174

Re: Generating Random Numbers

Future reference, consider searching the SAS support http://support.sas.com/ website which has SAS-hosted product documentation and supplemental technical / conference reference material.

Scott Barry
SBBWorks, Inc.


Suggested Google advanced search argument, this topic/post:

generate random numbers site:sas.com
Regular Contributor
Posts: 184

Re: Generating Random Numbers

Note that this approach makes the endpoints of the range half as likely to be realized as the internal points.

Rick Wicklin's blog post looks at continuous ranges and integer ranges; here we have non-integer but discrete ranges.

RickM wrote:

Something like this would work. 

var=round(ranuni(0)*(12.345+67.890),0.001)-12.345;

To generalize it you would need to create a macro or use proc fcmp.

SAS Super FREQ
Posts: 3,478

Re: Generating Random Numbers

See also my blog post, "How to generate random numbers in SAS." 

New Contributor
Posts: 2

Re: Generating Random Numbers

Hi Rick! Do you a sample simulation code to generate multilevel data (nested data)? An example of this is a cluster-randomized trial (CRT) design where the treatment is implemented at the group level while the outcome is measured at the individual level?

Thanks,

Aye

SAS Super FREQ
Posts: 3,478

Re: Generating Random Numbers

There are examples like this in my book Simulating Data with SAS. For example, see Section 7.5 and Section 11.3.

Ask a Question
Discussion stats
  • 7 replies
  • 2069 views
  • 0 likes
  • 6 in conversation