title: How to generate random numbers from log-normal distribution with a given mean and SD in SAS?
body: <p>Wenping (Wendy) Zhang <a href="http://www2.sas.com/proceedings/sugi31/198-31.pdf" rel="nofollow">points out</a>
  that the SAS <code>RAND</code> <a href="http://support.sas.com/documentation/cdl/en/lefunctionsref/63354/HTML/default/p0fpeei0opypg8n1b06qe4r040lv.htm" rel="nofollow">function</a> "basically gives "standard" distribution".</p>
  
  <p>The author describes an interesting SAS <code>%rndnmb</code> macro to generate data from “non-standard” distributions. Unfortunately the code in unavailable. So, I dared to do it by myself.</p>
  
  <p>If I understand correctly the Wikipedia <a href="http://en.wikipedia.org/wiki/Log-normal_distribution" rel="nofollow">says</a> that <em>y</em> is from the log-normal distribution if<br>
  <em>y = exp^(mu + sigma * Z)</em>.<br>
  The following formulas connect the <em>mean</em> and <em>variance</em> of the non-logarithmized sample values:<br>
  <em>mu = ln((mean^2)/(sqrt(variance + mean^2))</em><br>
  and<br>
  <em>sigma = sqrt(ln(1 + (variance)/(mean^2)))</em>.</p>
  
  <p>If that correct, my <em>y</em> will be drawn from log-normal distribution when<br>
  Z is from standard normal distribution Z with mu' = 0, sigma' = 1.</p>
  
  <p>Finally, is it correct that <em>y</em> is from lognormal distribution with <em>mean</em> and <em>variance</em> if<br>
  <em>y = exp^(ln((mean^2)/(sqrt(variance + mean^2)) + sqrt(ln(1 + (variance)/(mean^2))) * Z)</em>
  ?</p>
  
  <p>My SAS code is:<br>
  <code>/*I use StdDev^2 notation instead of variance here. */</code><br>
  <code>DATA nonStLogNorm;</code><br>
  <code>nonStLN = exp(1)**(log((mean**2)/(sqrt(StdDev^2 + mean**2)) +
  sqrt(log(1 + (StdDev^2)/(mean**2))) * rand('UNIFORM'));</code><br>
  <code>RUN;</code></p>
  
  <p>References:<br>
  <code>RAND</code> function by Rick Wicklin:
  <a href="http://blogs.sas.com/content/iml/2013/07/10/stop-using-ranuni/" rel="nofollow">http://blogs.sas.com/content/iml/2013/07/10/stop-using-ranuni/</a>
  <a href="http://blogs.sas.com/content/iml/2011/08/24/how-to-generate-random-numbers-in-sas/" rel="nofollow">http://blogs.sas.com/content/iml/2011/08/24/how-to-generate-random-numbers-in-sas/</a></p>