SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ani7
Obsidian | Level 7

I am trying to randomize the numbers in my dataset for a few different variables. The macro that I am using to do this is currently:

%macro RandBetween(min, max);
   (&min + ((1+&max-&min)*abs(rand("normal"))))
%mend;

I think my understanding of a normal distribution isn't correct since %RandBetween(0,1) seems to be returning numbers greater than 1 as well. This obviously will return an incorrect 'percentage' variable since I want that to have a maximum value of 1 (100%). Consequently, other variables that are generated like this:

avg_opioid = %RandBetween(0,&max_avg_opioid.);

also seem to have values greater than their max (presumably because the rand("normal") function is returning numbers greater than 1. I have also tried rand("normal",0.5,0.5) but that doesn't seem to help either. At this point, I think my understanding of the normal distribution may be skewed.

 

How do I go about limiting the return of rand("normal") to a min and max of 0 and 1 respectively?

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

The "problem" with the normal distribution is that it does not have defined lower/upper bounds. Theoretically, any value is possible, only with diminishing probability the farther away from the mean it is.

The more data points you have, the higher the probability of exceeding any wanted minimum/maximum.

So, when running this:

data test;
do i = 1 to 1000;
  x1 = rand('normal',.5,.1);
  output;
end;
run;

I stayed well between 0 and 1, but with 10 million iterations I exceeded the bounds.

 

Are you sure you don't want uniform distribution?

View solution in original post

6 REPLIES 6
Kurt_Bremser
Super User

After the macro is resolved, you get

(0 + ((1+1-0)*abs(rand("normal"))))

so you'll get numbers between 0 and 2.

Change your macro to this:

%macro RandBetween(min, max);
   (&min + ((&max-&min)*abs(rand("normal"))))
%mend;
Ani7
Obsidian | Level 7
I tried this and it still returns numbers greater than 1. Looking at the documentation of the rand function here (http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/a001466748.htm#a002505417), it looks like the rand('NORMAL') function can return values greater than 1.
PeterClemmensen
Tourmaline | Level 20

Are you sure you want to create a 'percentage variable' using the normail distribution? A N(0,1) distribution is not restricted to values between 0 and 1. It is a normal distribution with mean 0 and variance 1 ..

 

If you do not actually need the normail, then simply do this to get a value between 0 and 1

 

data _null_;
x=rand('uniform');
put x;
run;
Ani7
Obsidian | Level 7

Unfortunately, for the purposes of what I am trying to do with the data, I need it to be a normal distribution.

Kurt_Bremser
Super User

The "problem" with the normal distribution is that it does not have defined lower/upper bounds. Theoretically, any value is possible, only with diminishing probability the farther away from the mean it is.

The more data points you have, the higher the probability of exceeding any wanted minimum/maximum.

So, when running this:

data test;
do i = 1 to 1000;
  x1 = rand('normal',.5,.1);
  output;
end;
run;

I stayed well between 0 and 1, but with 10 million iterations I exceeded the bounds.

 

Are you sure you don't want uniform distribution?

Ani7
Obsidian | Level 7

Awesome, this was exactly what I needed. It understand that the asymptotic nature of the bell curve doesn't allow for bounds but that is okay since 1 or 2 observations over 100% out of 100,000 is reasonable. Thanks!

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 15174 views
  • 3 likes
  • 3 in conversation