Bookmark and Share

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

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

Re: st: Conditional Variable means to new observation

From   Phil Schumm <>
To   <>
Subject   Re: st: Conditional Variable means to new observation
Date   Wed, 4 Sep 2013 15:26:04 -0500

On Sep 4, 2013, at 2:44 PM, Nickolas Lyell <> wrote:
> I am still stuck with this problem.  I would like to make a new observation that encompasses Large counties.  I already have a dummy for large counties and would just like to sum all of each year's observations where Large County is equal to 1.
> I want to do this so I can analyze all large counties the same way I am looking at each observation.  I don't plan on doing any regressions on this dataset, merely creating new variables to help me understand the data and I am using Stata because the programmatic approach of a do file makes sense to me when dealing with these very large files.
> Could anyone please help.

While Maarten is in general correct (i.e., you should not try to use Stata like you would a spreadsheet program), it sounds to me like what you want is essentially to create a (possibly large) data table with a row of totals at the bottom.  If so, Stata can certainly oblige (and if you are not already facile with another programming language -- and even if you are -- it would not be unreasonable to use Stata for this task).  Here is probably the simplest approach:

        ds _all, has(type numeric)
        collapse (sum) `r(varlist)' if LgMdSm==2
        tempfile totals
        save `"`totals'"'
    append using `"`totals'"'

Note that the call to -ds- is merely to avoid trying to sum any string variables you might have, which will generate an error.  In addition, may wish to add to this something like

    gen byte totals = (_n==_N)

so that you can easily identify (and exclude) this row later on, if necessary.  But as Maarten said, if you are going to go on to analyze these data in Stata, it would be best to delete this additional row(s) first, to avoid it causing problems.

Finally, note that this approach is quite general, as it will accommodate any of the types of summaries that -collapse- will generate.  And you may want to use -export excel- to move the resulting data table to Excel for visual inspection, where features such as splits will allow you to hold the total row fixed while scrolling through the rows of data.

-- Phil

*   For searches and help try:

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