Bookmark and Share

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


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

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


From   Eric Booth <[email protected]>
To   "<[email protected]>" <[email protected]>
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
[email protected]
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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index