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, 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 <>
To   "" <>
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! 

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:

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