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

# Re: st: Going through each observation of a variable

 From David Kantor <[email protected]> To [email protected] 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 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))

}
```
```
```
It is not clear what you are trying to do and why you are sorting on a random order 500 times. (And even without the random ordering, there is usually no need to step through the observations.)
```Just a few minor points to start with:
```
You indicated that variables price1 and price2 have 500 observations. It is more accurate to say that your dataset has 500 observations.
```The commands
scalar P1=price1
scalar P2=price2
take their values from the first observation.
The variable select is unused.
----

```
Putting those matters aside, is all you want just the mean and standard deviation of price1*os1+price2*os2 ?
```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/
```