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]

From |
Eric Booth <ebooth@ppri.tamu.edu> |

To |
"<statalist@hsphsun2.harvard.edu>" <statalist@hsphsun2.harvard.edu> |

Subject |
Re: st: Insert sum of column values by id as new observation |

Date |
Thu, 23 Dec 2010 06:06:31 +0000 |

<> Here's one approach: ***************! clear inp id wk1ad wk2ad 1 10 10 1 20 10 2 10 20 2 10 20 2 20 10 3 10 20 4 99 100 4 80 80 4 50 50 4 1 2 end bys id: g i = _n preserve collapse (sum) wk1ad wk2ad, by(id) sa "tot.dta", replace restore append using "tot.dta" sort id i **add some labels** recode i (.=.a) lab def total .a "TOTALS", modify lab val i total order i /* if you don't want the "totals" line when there is one obs */ drop if i==1 & i[_n+1]==.a ***************! - Eric __ Eric A. Booth Public Policy Research Institute Texas A&M University ebooth@ppri.tamu.edu Office: +979.845.6754 On Dec 22, 2010, at 11:24 PM, Mike Kim wrote: > My data looks like the following and I would like to create sum of wk1ad > wk2ad... by id and insert as new observations. > > id wk1ad wk2ad ... > 1 10 10 > 1 20 10 > 2 10 20 > 2 10 20 > 2 20 10 > 3 10 20 > ..... > > The resulting data I want to have is the following. How can I do this work > in Stata? > > id wk1ad wk2ad ... > 1 10 10 > 1 20 10 > 1 30 20 <- newly created obs > 2 10 20 > 2 10 20 > 2 20 10 > 2 40 50 <- newly created obs > 3 10 20 * * 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/

**Follow-Ups**:**Re: st: Insert sum of column values by id as new observation***From:*Nick Cox <njcoxstata@gmail.com>

**References**:**st: Insert sum of column values by id as new observation***From:*"Mike Kim" <kalisperos@gmail.com>

- Prev by Date:
**st: Insert sum of column values by id as new observation** - Next by Date:
**Re: st: how to generate ranked variable for each year in panel data** - Previous by thread:
**st: Insert sum of column values by id as new observation** - Next by thread:
**Re: st: Insert sum of column values by id as new observation** - Index(es):