Announcement

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

  • automatically creating year variable for panel data

    Hi, I am a beginner in Stata. recently I spent a lot of day in creating a panel data, I have a huge dataset, part of my data looks like that, it includes the starting year and the ending year, but I need a year variable, still don't know how to create a year variable
    clear
    input long group id float(min_year max_year)...(some other variables)
    1 109 2015 2018
    1 109 2015 2018
    2 234 2005 2007
    2 234 2005 2007
    2 234 2005 2007
    3 240 2008 2009
    3 252 2010 2012

    end
    [/CODE]

    I want the data looks like that,it will be unbalanced panel data


    clear
    input long id float(min_year max_year) Year...(some other variables)
    1 109 2015 2018 2015
    1 109 2015 2018 2016
    1 109 2015 2018 2017
    1 109 2015 2018 2018
    2 234 2005 2007 2005
    2 234 2005 2007 2006
    2 234 2005 2007 2007

    3 240 2008 2009 2008
    3 240 2008 2009 2009
    3 252 2010 2012 2010
    4 252 2010 2012 2011
    4 252 2010 2012 2012
    end
    [/CODE]

    many thanks!!!

  • #2
    Baby Lucy That may be your real name, but otherwise please do read and act on https://www.statalist.org/forums/help#realnames

    To give you a good answer we need to know why you have apparent duplicates in the dataset.

    Comment


    • #3
      Hi,nick
      sorry about the real name,I come from Thailand,my name is baby lucy Ramida Jiranorraphat。Baby Lucy is my nickname get from English class。


      About my data, I need those duplicates to merge with another dataset.

      another dataset has some same variables: Year and id.it looks like that
      id year v1 (some other variables)
      109 2015 8900
      109 2018 8700
      234 2006 3699
      234 2007 6599

      I want to merge together, finally then look like this.

      input long group id float(min_year max_year) Year v1..(some other variables)
      1 109 2015 2018 2015 8900
      1 109 2015 2018 2016 .
      1 109 2015 2018 2017 .
      1 109 2015 2018 2018 8700
      2 234 2005 2007 2005 .
      2 234 2005 2007 2006 3699
      2 234 2005 2007 2007 6599

      Comment


      • #4
        Your example has changed so my question in #2 isn't really answered.

        This works for the example given:


        Code:
        clear
        input id year v1
        109 2015 8900
        109 2018 8700
        234 2006 3699
        234 2007 6599
        end
        
        tsset id year
        tsfill
        
        list, sepby(id)
        
             +-------------------+
             |  id   year     v1 |
             |-------------------|
          1. | 109   2015   8900 |
          2. | 109   2016      . |
          3. | 109   2017      . |
          4. | 109   2018   8700 |
             |-------------------|
          5. | 234   2006   3699 |
          6. | 234   2007   6599 |
             +-------------------+
        But merge can work fine here, I guess, even faced with gaps or other omissions. Did you try it?
        Last edited by Nick Cox; 20 Aug 2019, 05:17.

        Comment


        • #5

          yes! that's exactly the result I am looking for.
          many thanks!

          Comment

          Working...
          X