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

st: Re: systematically creating new observations


From   "Michael Blasnik" <[email protected]>
To   <[email protected]>
Subject   st: Re: systematically creating new observations
Date   Fri, 18 Jun 2004 14:21:01 -0400

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
>
> Research Assistant
> Center For Research On Education Outcomes
> Stanford University
> Stanford, CA 94305
>
> Phone: (650) 725-6169


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