Announcement

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

  • Repeated time values within panel

    Hello, I am trying to set a panel data but I keep getting this message:
    Code:
    . xtset id year
    repeated time values within panel
    r(451);
    My data is collected quarterly from 2015-2018 and only once in the baseline year 2014 (having the label 0) for every household.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float id int year byte quarter
    1 2016 3
    1 2016 1
    1 2016 2
    1 2016 4
    2 2018 2
    2 2016 3
    2 2015 2
    2 2017 1
    2 2015 1
    2 2015 4
    end
    label values id id
    label def id 1 "A106", modify
    label def id 2 "a001", modify
    I tried some solutions suggested in this forum such as duplicates report but I don't want to delete any observation. I also tried this: https://www.statalist.org/forums/for...hin-panel-r451 which is:
    Code:
    xtset id
    It worked when I identified the panel only by the id but I wonder if it is correct since it ignores the time identifier part.

    Would appreciate any help!

  • #2
    You can plainly see that you do in fact have duplicates observations for id and year. In fact, you have four observations for each combination of id and year: one for each quarter. And therein lies the problem and the solution. The difficulty is that year is not an appropriate time variable here, because your data is actually quarterly. So you need to first combine year and quarter into a quarterly date variable and then use that as the time variable.

    Code:
    gen qdate= yq(year, quarter)
    assert missing(qdate) == missing(year, quarter)
    format qdate %tq
    
    xtset id qdate
    Probably you should drop the variables year and quarter at this point unless you know that you will need to separately use them. The variable qdate is the complete time variable for this data set.

    Added: If you will be working with panel data regularly in the future, you really need to learn about Stata date variables and the various functions that create and transform them. You can start with -help datetime-, but rather than just reading the help file, you should click on the blue link to the PDF manual entry and read the entire chapter. It's a lengthy read, and you will not remember everything. But it will give you a foundational knowledge of how dates work in Stata, which is indispensable for working with data involving dates. Then, when you face difficulties, you will have the background needed to find the particular functions or formats you need, and can refresh your memory on the syntax in the help files for those particular things. The time you invest doing this will be amply repaid.
    Last edited by Clyde Schechter; 10 Nov 2022, 22:54.

    Comment


    • #3
      Thank you so much for the precious advice. I will read the pdf thoroughly.

      I have tried the code you used. The assert statement was false so I had to recode qdate for the year 2014 which is annual data. Then tried the xtset again but still the same problem (r451).
      After running the duplicates report of id and qdate it appeared that there were 22 observations that were duplicated, in which the data of 11 households were collected twice in the same quarter. So I had to delete them.
      Now it works perfectly!

      Thank you so much for the help.

      Thank you for existing (statalist).

      Comment

      Working...
      X