Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

From |
David Kantor <kantor.d@att.net> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: Going through each observation of a variable |

Date |
Fri, 07 Jun 2013 12:09:06 -0400 |

At 10:52 AM 6/7/2013, Derya wrote:

Dear Statalist users,I am newbie in Stata programming and I am stuck on something that isprobably very simple - but I could not find the answer on the list server.I would like to compute the mean and standard deviation of anexpression (P1*os1+P2*os2) that is computed for prices underdifferent scenarios. I have 500 scenarios, so the variables price1and price2 have 500 observations.I came up with the program below, which works fine, but it choosesrandom values of price1 and price2 variables. I would like like theprogram go through each observation of price1 price2 one by one. Howwould I do that?Any help will be appreciated! Thanks a lot! Derya gen k=0 gen wmean=0 gen wsum=0 gen wsqdev=0 gen wsd=0 forv k=1/500 { gen r = uniform() sort r gen select =_n==1 scalar P1=price1 scalar P2=price2 drop r select gen Y_`k'=P1*os1+P2*os2 replace k=`k' replace wsum=wsum+Y_`k' replace wmean=wsum/`k' replace wsqdev=wsqdev+((wmean-Y_`k')^2) replace wsd=sqrt(wsqdev/(k-1)) }

Just a few minor points to start with:

The commands scalar P1=price1 scalar P2=price2 take their values from the first observation. The variable select is unused. ----

Then generate a variable containing that value, and summarize it: gen newvariable = price1*os1+price2*os2 summ newvariable HTH --David * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/faqs/resources/statalist-faq/ * http://www.ats.ucla.edu/stat/stata/

**Follow-Ups**:**Re: st: Going through each observation of a variable***From:*Derya Karaci <dkaraci@yahoo.com>

**References**:**st: Going through each observation of a variable***From:*Derya Karaci <dkaraci@yahoo.com>

- Prev by Date:
**Re: st: Expansion for Choice set 0/1** - Next by Date:
**st: twoway contour colors?** - Previous by thread:
**st: Going through each observation of a variable** - Next by thread:
**Re: st: Going through each observation of a variable** - Index(es):