BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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
7 REPLIES 7
RickM
Fluorite | Level 6
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.
deleted_user
Not applicable
Thank you, Rick, that's awesome!


Suzy
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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
Howles
Quartz | Level 8

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.

Rick_SAS
SAS Super FREQ

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

ayecon0325
Calcite | Level 5

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

Rick_SAS
SAS Super FREQ

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 4453 views
  • 0 likes
  • 6 in conversation