Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

st: Going through each observation of a variable


From   Derya Karaci <dkaraci@yahoo.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   st: Going through each observation of a variable
Date   Fri, 7 Jun 2013 07:52:29 -0700 (PDT)

Dear Statalist users, 

I am newbie in Stata programming and I am stuck on something that is probably very simple - but I could not find the answer on the list server.

I would like to compute the mean and standard deviation of an expression (P1*os1+P2*os2) that is computed for prices under different scenarios. I have 500 scenarios, so the variables price1 and price2 have 500 observations. 

I came up with the program below, which works fine, but it chooses random values of price1 and price2 variables. I would like like the program go through each observation of price1 price2 one by one. How would 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))

}


*
*   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/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index