Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • copy one variable's values across another group variable

    Hi Everyone,

    I am using Stata 12 and have a dataset with 539,168 observations (8 years * 58 counties * 83 ages (18 to 100) * 2 genders * 7 races).
    The years are 2007 to 2014. I would like to copy the 2014 values for the variable Population to the other seven years by county, age, gender and race. The code works below, but takes a very long time because it summarizes every 2014 observation (all 67,396 observations). Is there better code someone could recommend (e.g., bysort Year CountyCode Age sex RaceCode...)?

    Thanks,
    Brent Fulton
    UC Berkeley

    *8 years * 58 counties * 83 ages (18 to 100) * 2 genders * 7 races
    *539168
    di 8*58*83*2*7
    des, short
    *58 counties * 83 ages (18 to 100) * 2 genders * 7 races
    *67396
    di 58*83*2*7


    clonevar pop14=Population
    foreach c of numlist 1/58{
    di "c is now `c' *************************************"
    di "`c(current_date)' `c(current_time)'"
    foreach a of numlist 18/100{
    foreach g of numlist 1/2{
    foreach r of numlist 1/7{
    di "c is `c', a is `a', g is `g', r is `r'"
    su Population if CountyCode==`c' & Age==`a' & sex==`g' & RaceCode==`r' & Year==2014
    replace pop14=r(mean) if CountyCode==`c' & Age==`a' & sex==`g' & RaceCode==`r' & Year!=2014
    }
    }
    }
    }


  • #2
    Here is a very irritating answer. This should be one line:

    Code:
    bysort CountyCode Age sex RaceCode (Year): generate pop14 = Population[_N]
    See http://www.stata-journal.com/sjpdf.h...iclenum=dm0055 for a more general discussion.

    Comment


    • #3
      Withdrawn, Nick beat me by two minutes, and noticed one thing I overlooked.

      Comment


      • #4
        Elegant...thank you Nick.
        Best
        Brent

        Comment


        • #5
          Hi, this is probably obvious, but how would I create a new variable using one specific panel id's data by date. That is, the new variable would have one panel's data "stacked", or repeated across all other panel members, by date. I see this as a generalization of the original question here, but I don't immediately see how to do it. Thanks a lot.

          Comment

          Working...
          X