turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- SAS Procedures
- /
- Generating Random Numbers

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-18-2010 10:21 AM

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

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-18-2010 10:44 AM

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.

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-18-2010 10:58 AM

Thank you, Rick, that's awesome!

Suzy

Suzy

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-18-2010 12:10 PM

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

Scott Barry

SBBWorks, Inc.

Suggested Google advanced search argument, this topic/post:

generate random numbers site:sas.com

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

10-12-2011 05:04 PM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

10-12-2011 09:09 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-21-2013 02:35 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-24-2013 09:24 AM

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