Announcement

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

  • How can I create a variable whose value is the first available value for each country for another variable?

    Hello everybody,

    I am having the following problem: I want to create a variable that equals the Gini coefficient for the first observation for each country where its Gini coefficient is included in the dataset. So if the first observation for countryxxxx to include the Gini was in 1985 and its Gini that year was 35, every observation for countryxxxx would equal 35 for the new variable. Can you let me know how to do this? My best attempt to so far is the following:

    gen gini2 = gini if period == 1
    replace gini2=gini2[_n-1] if gini2==.

    But it doesn’t work because often the first time the Gini coefficient is recorded for a country it’s not for the first observation for that country. For example countryxxxx has data starting in 1960 but not until many years later did they start recording their Gini.

    Any guidance would be greatly appreciated! Thanks



  • #2
    FAQ offering systematic discussion at https://www.stata.com/support/faqs/d...t-occurrences/

    Some pertinent discussion also at https://www.statalist.org/forums/for...hin-a-variable

    Such sources show that there are several ways to do it. Here's another:

    Code:
    egen whenfirst = min(cond(!missing(gini), period, .)), by(country)
    
    egen valuefirst = total(cond(period == whenfirst, gini, .)), by(country)
    See also https://www.stata-journal.com/sjpdf....iclenum=dm0055 for a review of related technique.

    Comment


    • #3
      This worked like a charm, truly fantastic!! Thank you

      Comment

      Working...
      X