Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: RE: Creating aggregated new observations


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: RE: Creating aggregated new observations
Date   Fri, 14 Jul 2006 12:30:39 +0100

Correction of code, as I did not read this 
carefully enough. 

bysort year: egen output_total = total(output) 
egen tag = tag(year) 
l year output_total if tag 

by year: egen output_20212022 = total(output * inrange(code,2021,2022)) 
l year output_20212022 if tag 

Your example and your explanation are contradictory w.r.t.
code 2023, but the same principle applies. 

Nick 
[email protected] 

Nick Cox
 
> Frankly, what you ask for is a bad idea. 
> 
> You want subtotals, but stored in extra observations. 
> 
> This can be done, but you will then need
> to exclude such observations from most
> calculations down the line. 
> 
> I suspect that you are thinking spreadsheet-style, 
> but Stata is not a spreadsheet. 
> 
> What I suggest is something more like 
> 
> gen rounded_code = 20 * floor(code/20) 
> bysort rounded_code : egen total_output = total(output) 
> egen tag = tag(rounded_code) 
> l rounded_code total_output if tag 
> 
> Nick 
> [email protected] 
> 
> [email protected]
> 
> > aggregate new observations.
> > 
> >  Suppose  I have a set of observations as follows
> > 
> >   code         year          output
> >   2011        1981           2
> >   2013        1981           3
> >   2021        1981           4
> >   2022        1981           2
> >   2023        1981           3
> >    .
> >    .
> >  2011         1982           1
> >  2013         1982           2
> >  2021         1982           3
> >  2022         1982           4
> > 
> > 
> >     What I like to do is create aggregated new observations 
> > like this :
> > 
> >  code        year         output
> >  2000        1981         15  - new added obs (2+3+4+2+3)
> >  2011        1981          2
> >  2013        1981          3
> >  2020        1981          9    -new added obs (4+2+3)
> >  2021        1981          4
> >  2022        1981          2
> >  2023        1981          3
> >    ..
> >   ..
> > 2000        1982          10  -new added obs(1+2+3+4)
> > 2011        1982           1
> > 2013        1982           2
> > 2020        1982           7  -new added obs (3+4)
> > 2021        1982           3
> > 2022        1982           4
> > 
> > 
> > How do I add these outputs of both code 2000 (sum of all 5 
> > observations) and
> > code 2020 ( sum of  2021 ,2022), respectively.

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index