Announcement

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

  • Duplication of observations in panel data

    Dear All,

    it might be a stupid question, but I am stuck. Suppose I have the following data:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str3 countrycode str34 country double year
    "ALB" "Albania"             1950
    "DZA" "Algeria"             1950
    "AGO" "Angola"              1950
    "AIA" "Anguilla"            1950
    "ATG" "Antigua and Barbuda" 1950
    "ARG" "Argentina"           1950
    "ARM" "Armenia"             1950
    "ABW" "Aruba"               1950
    "AUS" "Australia"           1950
    "AUT" "Austria"             1950
    "ALB" "Albania"             1951
    "DZA" "Algeria"             1951
    "AGO" "Angola"              1951
    "AIA" "Anguilla"            1951
    "ATG" "Antigua and Barbuda" 1951
    "ARG" "Argentina"           1951
    "ARM" "Armenia"             1951
    "ABW" "Aruba"               1951
    "AUS" "Australia"           1951
    "AUT" "Austria"             1951
    end
    I would like to replicate the above set of countries to generate 10 additional observations for 1952, say:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str3 countrycode str34 country double year
    "ALB" "Albania"             1950
    "DZA" "Algeria"             1950
    "AGO" "Angola"              1950
    "AIA" "Anguilla"            1950
    "ATG" "Antigua and Barbuda" 1950
    "ARG" "Argentina"           1950
    "ARM" "Armenia"             1950
    "ABW" "Aruba"               1950
    "AUS" "Australia"           1950
    "AUT" "Austria"             1950
    "ALB" "Albania"             1951
    "DZA" "Algeria"             1951
    "AGO" "Angola"              1951
    "AIA" "Anguilla"            1951
    "ATG" "Antigua and Barbuda" 1951
    "ARG" "Argentina"           1951
    "ARM" "Armenia"             1951
    "ABW" "Aruba"               1951
    "AUS" "Australia"           1951
    "AUT" "Austria"             1951
    "ALB" "Albania"             1952
    "DZA" "Algeria"             1952
    "AGO" "Angola"              1952
    "AIA" "Anguilla"            1952
    "ATG" "Antigua and Barbuda" 1952
    "ARG" "Argentina"           1952
    "ARM" "Armenia"             1952
    "ABW" "Aruba"               1952
    "AUS" "Australia"           1952
    "AUT" "Austria"             1952
    end
    How can I do it?

    Thanks in advance for your help.

    Dario

  • #2
    The simplest solution that springs to my mind is to create those additional observations in a different -.dta- fil and then -append- it to the master -.dta-.
    Kind regards,
    Carlo
    (Stata 18.0 SE)

    Comment


    • #3
      here is an alternative:
      Code:
      expand 2 if year==1950
      replace year=1952 in 21/30

      Comment


      • #4
        Rich's code get my vote. In this direction, I contribute other solution, which does not require manually counting 21/30.

        Code:
        local count = _N
        expand 2 if year ==1950
        replace year = 1952 if _n>`count'

        Comment


        • #5
          Romalpa's code nicely exploits the fact that the result of expand is extra observations added to the end of the dataset. That being so, you don't need to test every observation to see if it qualifies. Hence consider

          Code:
          local Np1 = _N + 1  
          expand 2 if year == 1950
          replace year = 1952 in `Np1'/L

          Comment


          • #6
            Thanks all for the suggestions. I finally managed to solve the issue in the following way:

            Code:
            expand 2 if year==1950, gen(index)
            replace year=1952 if index==1
            drop index
            sort country year

            Comment

            Working...
            X