BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Defense
Obsidian | Level 7

Any one know how to simulate a dataset by given parameters below:

Given parameters: mean=0.195, max=0.26, min=0.13,

Request: random generate a 8 observation dataset with normal distribution.

I try to use rand, but it sounds no option for max and min

 

data sixnum;
do i=1 to 6;
random=rand("normal", mean, STDDEV );
output;
end;
run;

 

 

Thanks 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

You can calculated P value for MIN and MAX value(take them as percentile).
After that ,sample from PValue_MIN and PValue_MAX , mapping these P value back to percentile.

Check @Rick_SAS 's blog
https://blogs.sas.com/content/iml/2013/07/24/the-truncated-normal-in-sas.html

 

 

/*
You can calculated P value for MIN and MAX value(take them as percentile).
After that ,sample from PValue_MIN and PValue_MAX , mapping these P value back to percentile.

Check @Rick_SAS 's blog
https://blogs.sas.com/content/iml/2013/07/24/the-truncated-normal-in-sas.html
*/

%let mean=0.195 ;
%let max=0.26 ;
%let min=0.13 ;
data have;
call streaminit(123);
cdf_min = cdf("Normal", &min., &mean., 0.03);  
cdf_max = cdf("Normal", &max., &mean., 0.03); 
do i=1 to 1000;
  quantile=quantile("Normal", cdf_min + (cdf_max - cdf_min)*rand('uniform'), &mean., 0.03 );
  output;
end;
run;

proc univariate data=have normal;
var quantile;
histogram quantile/normal;
run;

Ksharp_0-1675251717069.png

 

View solution in original post

18 REPLIES 18
Defense
Obsidian | Level 7
My code:
data sixnum;
do i=1 to 8;
random=rand("normal", mean, STDDEV );
output;
end;
run;
PaigeMiller
Diamond | Level 26

@Defense wrote:

Any one know how to simulate a dataset by given parameters below:

Given parameters: mean=0.195, max=0.26, min=0.13,

Request: random generate a 8 observation dataset with normal distribution.

I try to use rand, but it sounds no option for max and min


Do you have any guidance as to what distribution is required here? It can't be normal with a minimum and maximum.

 

You could cheat and use a normal distribution where the standard deviation is so large that you will virtually never get a value below the min or above the max.

 

--
Paige Miller
DanObermiller
SAS Employee

@PaigeMiller is correct. A normal distribution can take values from negative infinity to positive infinity. You can get "close" to your maximum and minimum by choosing a standard deviation of the range divided by 6. If you do that, keep in mind that you may still get values above your maximum and/or below your minimum.

StatDave
SAS Super FREQ

The normal distribution only has the mean and standard deviation as parameters. The best you could do would be to choose a standard deviation that will tend to keep the extremes within the min and max that you want. The following uses a sixth of the range as the standard deviation and generates 10,000 observations. Different. seed values in STREAMINIT, different sample sizes, and different proportions of the range will give different results. You'll want to experiment.

data rand;
call streaminit(3525);
mean=.195; std=(.26-.13)/6;
do i=1 to 10000;
random=rand("normal", mean, std );
output;
end;
run;
proc means mean min max std; 
var random; 
run;

Defense
Obsidian | Level 7

Thanks for replying. Not sure why using "sixth of the range as the standard deviation".

 

In addition, I am trying the similar:

 

data symm(keep=y);
%let

data symm(keep=y);
%let st=0.0012;
seed = 11907665 ;
do i = 1 to 8;
y = rand("Normal", 0.19,&st );

if 0.26>=y>=0.13 then
output;
end;
run;

;

seed = 11907665 ;
do i = 1 to 8;
y = rand("Normal", 0.19,&st );

if 0.26>=y>=0.13 then
output;
end;
run;

 

I will try st=1/6(max-min), 1/4........1/8 until the output in 0.26>=y>=0.13 that range. Is that make sense

 

Reeza
Super User
99.9% of data is within plus or minus 3 standard deviations (6 sigma) in a Normal Distribution.
Defense
Obsidian | Level 7

Thanks😁😀

FreelanceReinh
Jade | Level 19

Hello @Defense,

Glad to see that you've got great answers from the experts. Then it would be fair and help later readers if you marked the most helpful reply as the accepted solution, not your own "thank you" post. Could you please change that? It's very easy: Select a different post as the solution after clicking "Not the Solution" in the option menu (see icon below) of the current solution.
show_option_menu.png

Defense
Obsidian | Level 7

I like to do that. But there are only three icons "Add tag", "Quick reply" and "Reply".  No"Not the Solution" icon. Pleae let me know how...Thank

FreelanceReinh
Jade | Level 19

@Defense wrote:

I like to do that. But there are only three icons "Add tag", "Quick reply" and "Reply".  No"Not the Solution" icon. Pleae let me know how...Thank


Click the icon (three horizontal bars) shown in the screenshot in my previous post. This opens the option menu, which should contain the item "Not the Solution" among others.

PaigeMiller
Diamond | Level 26

@Defense wrote:

Thanks😁😀


Hello @Defense , you should not mark your own answer, which reads "Thanks" as the correct answer. You need to mark someone else's answer as correct, whichever one you think is correct.

--
Paige Miller
Ksharp
Super User

You can calculated P value for MIN and MAX value(take them as percentile).
After that ,sample from PValue_MIN and PValue_MAX , mapping these P value back to percentile.

Check @Rick_SAS 's blog
https://blogs.sas.com/content/iml/2013/07/24/the-truncated-normal-in-sas.html

 

 

/*
You can calculated P value for MIN and MAX value(take them as percentile).
After that ,sample from PValue_MIN and PValue_MAX , mapping these P value back to percentile.

Check @Rick_SAS 's blog
https://blogs.sas.com/content/iml/2013/07/24/the-truncated-normal-in-sas.html
*/

%let mean=0.195 ;
%let max=0.26 ;
%let min=0.13 ;
data have;
call streaminit(123);
cdf_min = cdf("Normal", &min., &mean., 0.03);  
cdf_max = cdf("Normal", &max., &mean., 0.03); 
do i=1 to 1000;
  quantile=quantile("Normal", cdf_min + (cdf_max - cdf_min)*rand('uniform'), &mean., 0.03 );
  output;
end;
run;

proc univariate data=have normal;
var quantile;
histogram quantile/normal;
run;

Ksharp_0-1675251717069.png

 

Defense
Obsidian | Level 7

This is a great solution. Is 0.03 come from (max-min)/4=S.D?

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 18 replies
  • 3609 views
  • 8 likes
  • 9 in conversation