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

# Re: st: Accumulated mean and standard deviation

 From Matthew White To statalist@hsphsun2.harvard.edu Subject Re: st: Accumulated mean and standard deviation Date Sun, 8 Jan 2012 16:27:54 -0500

```Hi Pedro,

Again, the mean is just one line:

by day: generate mean = sum(x) / _n

For the SD, I think the most efficient option is to use a program. See
-help byable-:

syntax varname, Generate(name)
if _byindex() == 1 {
confirm new variable `generate'
quietly generate `generate' = .
}

if !_by() {
local start 1
local end = _N
}
else {
local start = _byn1()
local end   = _byn2()
}

forvalues i = `start'/`end' {
quietly summarize `varlist' in `start'/`i'
quietly replace `generate' = r(sd) in `i'
}
end

Then:

by day: runningsd x, generate(sd)

Best,
Matt

On Sun, Jan 8, 2012 at 3:23 PM, Pedro Nakashima
<nakashimapedrolist@gmail.com> wrote:
> Hi guys
>
> Unfortunately, I forgot one important point for my analysis.
>
> Given that I have the same kind of data for several days and I need to
> calculate these statistics for each of these days, I have to do
> something like "bysort days: 'all those commands' "
>
> Do you know how can I do that?
>
> One more time, best regards
> Pedro
>
> 2012/1/8 Pedro Nakashima <nakashimapedrolist@gmail.com>:
>> Perfect!
>>
>> Thank you Matthew and Sunil!
>>
>> Best,
>> Pedro.
>>
>>> Does this achieve what you want?
>>>
>>> gen mean=.
>>> gen sd=.
>>>
>>> local k=_N
>>> forv i=1/`k' {
>>> qui su x in  1/`i'
>>> qui replace mean=r(mean) in `i'
>>> qui replace sd=r(sd) in `i'
>>> }
>>>
>>> Best,
>>> Sunil
>>> *
>>> *   For searches and help try:
>>> *   http://www.stata.com/help.cgi?search
>>> *   http://www.stata.com/support/statalist/faq
>>> *   http://www.ats.ucla.edu/stat/stata/
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/

--
Matthew White
Data Coordinator
Innovations for Poverty Action
101 Whitney Avenue, New Haven, CT 06510 USA
+1 434-305-9861
www.poverty-action.org

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```