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

st: Re: how to calculate a function (sum ) of observations without creating a new variable ?


From   "Scott Merryman" <[email protected]>
To   <[email protected]>
Subject   st: Re: how to calculate a function (sum ) of observations without creating a new variable ?
Date   Sat, 16 Aug 2003 22:00:08 -0500

-tabstat- is probably the easiest way to display a sum

. use "C:\Stata8\auto.dta", clear
(1978 Automobile Data)

. tabstat price if mpg>20, stat(sum)

    variable |       sum
-------------+----------
       price |    192611
------------------------

Also, take a look at the saved results for -summarize-

Scott




----- Original Message -----
From: "mikhail bontch-osmolovski" <[email protected]>
To: <[email protected]>
Sent: Saturday, August 16, 2003 8:33 PM
Subject: st: how to calculate a function (sum ) of observations without creating
a new variable ?


> Dear users of statalist.
> This is a long question about functions in Stata .
> I would like to know what is the best way to  calculate a function of
> the data without creating a variable, i.e.  without using gen or egen
> function.
> Situation is simple: I have a big dataset of 8 mlns of observations and
> I need to calculate weighted sums of
> observations under certain conditions,  I need:  sum(wt) if b
> Obvious way would be to:
> 1.  egen c=sum(wt) if b
> 2.  su c, or di c
> 3. drop c.
> However, this way is clumsy since I have to create additional 8 mln of
> observation of c, all equal to each other,
> so it takes memory which is very limited in my case and extra time (it
> takes very long time) .
> As I understand egen sum works by first running gen sum and than
> replacing all observations with the last one.
> I could not find anything online, so I wrote a simple program which
> calculates sum of a variable called wt:
>
> scalar a=0
> local n=1
> while `n'<=_N {
>     scalar a=a+wt[`n']
>     local n=`n'+1
>            }
> display a
>
> I could not believe my eyes, this simle program ran 3 times LONGER then
> egen a=sum(wt).
> Later I used scalar n instead of local n to save interpretation time,
> but it stilled run 3 times longer than egen.
> So I was forced to go back to egen, but it is estetically unpleasant to
> creat 8 mln observations when you  need just a constant and  often low
> memory does even allow me to have an extra variable. I  am using Windows
> XP, Stata 8.1.
>
> So I wonder if you know a good way to calculate a sum of variables and,
> in general, and function of variables under certain conditions, like you
> can do in Excel, without creating an extra variable?, why egen is faster
> than plain sum ?  Is this the good case for having a plugin which is
> faster ? It is hard to believe that Stata has no commands for such a
> simple operation.
>
> Misha Bonch-Osmolovski
> UNC-Chapel Hill, Econ grad
> ps. display which is also called hand calcualtor does not allow for if
> condition, so it does not work
>
>
> *
> *   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/


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