Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: observations with subtotals (was: systematically creating new observations)


From   "Michael Blasnik" <[email protected]>
To   <[email protected]>
Subject   st: observations with subtotals (was: systematically creating new observations)
Date   Fri, 18 Jun 2004 15:02:29 -0400

I just realized a way that might be better, especially if you want these
subtotals for many variables.  It still needs some type of group variable
(what to susbtotal), and subgroup variable, and then a varlist of other
variables you want the sums of like var1 var2 etc...

collapse (sum) var1 var2 var3 , by(groupvar)
gen subgroupvar="All"
sort groupvar
save subtotals
use mydata
append using subtotals

This will give you an extra observation for each group with the sums you
want.  Of course, if you try to sort it by groupvar subgroupvar then the
"All" subtotal will probably end up first or near the top at least.  A way
around this would be to use value labels and then define the "All" group to
have a value one larger than any other and label that value "All".

Michael Blasnik
[email protected]

----- Original Message ----- 
From: "Michael Blasnik" <[email protected]>
To: <[email protected]>
Sent: Friday, June 18, 2004 2:21 PM
Subject: st: Re: systematically creating new observations


> It actually isn't that easy/trivial.  If you have a group variable (not
> shown in your example, but you mention there are many groups) and a
subgroup
> var (e.g., gender in your example), then you could:
>
> egen groupsum=sum(points), by(groupvar)
> bysort groupvar (subgroupvar): gen byte last=_n==_N
> expand 2 if last
> bysort groupvar (subgroupvar): replace points=groupsum if _n==_N
> bysort groupvar (subgroupvar): replace subgroupvar="All" if _n==_N
> drop groupsum last
>
> This approach calculates the sums you seem to want for each group, flags
the
> last observation in each group and adds an observation to the end and then
> plugs in that sum and the title "All" into that new observation.  If you
> have other variables, they will be copied to the All group from the prior
> observation (the one that was expanded).
>
> Michael Blasnik
> [email protected]
>
> ----- Original Message ----- 
> From: "daphna" <[email protected]>
> To: <[email protected]>
> Sent: Friday, June 18, 2004 1:59 PM
> Subject: st: systematically creating new observations
>
>
> > Hello-
> >
> > I am wondering if anyone can recommend a way to systematically create
> > observations.
> >
> > Suppose I have a set of observations that looks something like this:
> >
> > #Points
> > Female415896
> > Male420897
> >
> >
> > What I want to do is create a new observation (not a new variable),
called
> > ALL so that the data would look something like this:
> >
> > #Points
> > Female415896
> > Male420897
> > All8351793
> >
> >
> > Of course, I need to do this more than once.  I have to do it for
> thousands
> > of groups.
> >
> > Is there a systematic way to add such an observation?
> >
> > It seems pretty obvious but I cannot seem to figure it out.
> >
> > Thanks so much for your help.
> >
> > D
>
> > =====================================================================
> >
> > Daphna Bassok
> >


*
*   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