Announcement

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

  • Conditioning on value of first year for each country in unbalanced panel data

    Hi all!

    I have country/year panel data. I would like to drop some observations according to specific values of some variables but I would like to do so according to the value for the first year in which I have data for the country.
    The problem I am facing is that I have an unbalanced panel so I cannot use a command of the sort:
    Code:
    gen temp=1 if value<x & year==2000
    How can I make sure I base my condition on the value for the first year for each country in my dataset.

    My dataset is of the following form:
    Country Continent Year agprod drought
    ABW LAC 1972 13413513 0
    ABW LAC 1973 15213523 1
    ABW LAC 1974 13513513 1
    ADO WEOFF 1972 353413 0
    ADO WEOFF 1973 234233 0
    Many thanks!

  • #2

    The first year for each country is given by subscript [1] after sorting.

    Code:
    bysort country (year) : gen drought1 = drought[1]
    See https://www.stata-journal.com/articl...article=dm0055 for many more tricks in this territoryl

    Comment


    • #3
      Alexandre:
      provided that I'm not completely clear with what you're after, let's assume, in the following toy-example, that you're interested in -age- at the first year this value is available for the different -panelid- the dataset is composed of:
      Code:
      . use "https://www.stata-press.com/data/r16/nlswork.dta"
      (National Longitudinal Survey.  Young Women 14-26 years of age in 1968)
      
      . bysort idcode (year): gen wanted=age if _n==1
      (23,825 missing values generated)
      
      . list idcode year age wanted if id==1
      
             +------------------------------+
             | idcode   year   age   wanted |
             |------------------------------|
          1. |      1     70    18       18 |
          2. |      1     71    19        . |
          3. |      1     72    20        . |
          4. |      1     73    21        . |
          5. |      1     75    23        . |
             |------------------------------|
          6. |      1     77    25        . |
          7. |      1     78    26        . |
          8. |      1     80    28        . |
          9. |      1     83    31        . |
         10. |      1     85    33        . |
             |------------------------------|
         11. |      1     87    35        . |
         12. |      1     88    37        . |
             +------------------------------+
      
      .
      Kind regards,
      Carlo
      (StataNow 18.5)

      Comment


      • #4
        Thank you both so much for your swift answers!

        As I said, I have panel observations for several years for each country. I would like to select (and eventually drop) countries for which, for example, GDP is under a certain threshold (GDP < x) for the first year in which data is available for that country (so first occurrence in the dataset).

        I think I can come up with something that works using your inputs.

        Comment


        • #5
          Code:
           
           bysort country (year) : drop if GNP[1] < 42

          Comment


          • #6
            Aleksandre:
            it's a good (and rewarding) habit to save a copy of your original dataset before -drop- anything.
            As a famous song goes
            The future's not ours to see
            and you may need your original dataset for further analyses.
            Kind regards,
            Carlo
            (StataNow 18.5)

            Comment

            Working...
            X