Announcement

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

  • Expand panel by adding on year

    Dear all,

    I have a panel dataset as follow
    where ANNO is my year variable

    I would like to expand the panel by adding ANNO 2011 (in the dataset is from 2012 to 2016). I would love that the variables "CODICECATASTALE" and "name_short" are carryforward in the year 2011 observations, whereas all the other contains missing value for year 2011.
    Many thanks in advance for your help and guidance.


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int ANNO str4 CODICECATASTALE str35 name_short float(IMU_aliq_abit_princ_permille IMU_aliq_ord_permille IMU_detrazione_euro TASI_aliq_abit_princ_permille TASI_aliq_ord_permille TASI_detrazione_euro)
    2014 "A001" "ABANO TERME"           4.8  8.8 200  1.2  1.2  0
    2016 "A001" "ABANO TERME"           4.8  8.8 200  1.2  1.2  0
    2015 "A001" "ABANO TERME"           4.8  8.8 200  1.2  1.2  0
    2013 "A001" "ABANO TERME"             6  8.8 200    .    .  .
    2012 "A001" "ABANO TERME"             6  8.8 200    .    .  .
    2013 "A004" "ABBADIA CERRETO"         4  9.6 200    .    .  .
    2014 "A004" "ABBADIA CERRETO"         4  9.6 200    1    0  0
    2016 "A004" "ABBADIA CERRETO"         4  9.6 200    1    0  0
    2012 "A004" "ABBADIA CERRETO"         4  9.6 200    .    .  .
    2015 "A004" "ABBADIA CERRETO"         4  9.6 200    1    0  0
    2015 "A005" "ABBADIA LARIANA"         4  8.9 200  1.9  .45  0
    2013 "A005" "ABBADIA LARIANA"         4  8.9 200    .    .  .
    2012 "A005" "ABBADIA LARIANA"         4  7.6 200    .    .  .
    2016 "A005" "ABBADIA LARIANA"         4  8.9 200  1.9  .45  0
    2014 "A005" "ABBADIA LARIANA"         4  8.9 200  1.9  .45  0
    2016 "A006" "ABBADIA SAN SALVATORE" 4.5  9.7 200    0    0  0
    2014 "A006" "ABBADIA SAN SALVATORE" 4.5  9.7 200  2.5    0 -1
    2015 "A006" "ABBADIA SAN SALVATORE" 4.5  9.7 200  2.5    0 -1
    2013 "A006" "ABBADIA SAN SALVATORE"   5  9.7 200    .    .  .
    2012 "A006" "ABBADIA SAN SALVATORE"   4  9.4 200    .    .  .
    2016 "A007" "ABBASANTA"               4  7.6 200    2    1  0
    2012 "A007" "ABBASANTA"               5  7.6 200    .    .  .
    2015 "A007" "ABBASANTA"               4  7.6 200    2    1  0
    2013 "A007" "ABBASANTA"               4  7.6 200    .    .  .
    2014 "A007" "ABBASANTA"               4  7.6 200    2    0  0
    2012 "A008" "ABBATEGGIO"              4 10.6 200    .    .  .
    2015 "A008" "ABBATEGGIO"              4 10.6 200  2.5    0  0
    2013 "A008" "ABBATEGGIO"              4 10.6 200    .    .  .
    2014 "A008" "ABBATEGGIO"              4 10.6 200  2.5    0  0
    2016 "A008" "ABBATEGGIO"              4 10.6 200    2    0  0
    2014 "A010" "ABBIATEGRASSO"           6 10.6 200  2.5    0  0
    2016 "A010" "ABBIATEGRASSO"           6 10.6 200    0    0  0
    2012 "A010" "ABBIATEGRASSO"           4 10.6 200    .    .  .
    2013 "A010" "ABBIATEGRASSO"           4 10.6 200    .    .  .
    2015 "A010" "ABBIATEGRASSO"           6 10.6 200  2.5    0  0
    2013 "A012" "ABETONE"                 4  9.3 200    .    .  .
    2015 "A012" "ABETONE"                 4  9.3 200    1    1  0
    2012 "A012" "ABETONE"                 4  8.3 200    .    .  .
    2016 "A012" "ABETONE"                 4  9.3 200    1    1  0
    2014 "A012" "ABETONE"                 4  9.3 200    1    1  0
    2015 "A013" "ABRIOLA"                 4  7.6 200    0    0  0
    2012 "A013" "ABRIOLA"                 4  7.6 200    .    .  .
    2016 "A013" "ABRIOLA"                 4  7.6 200    0    0  0
    2014 "A013" "ABRIOLA"                 4  7.6 200    0    0  0
    2013 "A013" "ABRIOLA"                 4  7.6 200    .    .  .
    2014 "A014" "ACATE"                   6  9.6 200    1    1  0
    2016 "A014" "ACATE"                   6 10.6 200    0   .8  0
    2013 "A014" "ACATE"                   4  7.6 200    .    .  .
    2015 "A014" "ACATE"                   6  9.6 200    1    1  0
    2012 "A014" "ACATE"                   2  7.6 200    .    .  .
    2015 "A015" "ACCADIA"                 3  7.9 200 2.25 2.25  0
    2013 "A015" "ACCADIA"                 3  7.6 200    .    .  .
    2012 "A015" "ACCADIA"                 3  7.6 200    .    .  .
    2014 "A015" "ACCADIA"                 3  7.9 200 2.25 2.25  0
    2016 "A015" "ACCADIA"                 3  7.9 200 2.25 2.25  0
    2015 "A016" "ACCEGLIO"                4  9.6 200    0    0  0
    2014 "A016" "ACCEGLIO"                4  9.6 200    0    0  0
    2016 "A016" "ACCEGLIO"                4  9.6 200    0    0  0
    2012 "A016" "ACCEGLIO"                4  7.6 200    .    .  .
    2013 "A016" "ACCEGLIO"                4  9.6 200    .    .  .
    2014 "A017" "ACCETTURA"               4  7.6 200    1    1  0
    2016 "A017" "ACCETTURA"               4  7.6 200    1    1  0
    2015 "A017" "ACCETTURA"               4  7.6 200    1    1  0
    2013 "A017" "ACCETTURA"               4  7.6 200    .    .  .
    2012 "A017" "ACCETTURA"               4  7.6 200    .    .  .
    2014 "A018" "ACCIANO"                 4  7.6 200    2    2  0
    2016 "A018" "ACCIANO"                 4  7.6 200    2    2  0
    2012 "A018" "ACCIANO"                 4  7.6 200    .    .  .
    2015 "A018" "ACCIANO"                 4  7.6 200    2    2  0
    2013 "A018" "ACCIANO"                 4  7.6 200    .    .  .
    2016 "A019" "ACCUMOLI"                6 10.6 200    0    0  0
    2012 "A019" "ACCUMOLI"                6   10 200    .    .  .
    2013 "A019" "ACCUMOLI"                6 10.6 200    .    .  .
    2014 "A019" "ACCUMOLI"                6 10.6 200    0    0  0
    2015 "A019" "ACCUMOLI"                6 10.6 200    0    0  0
    2013 "A020" "ACERENZA"                4 10.6 200    .    .  .
    2014 "A020" "ACERENZA"                4 10.6 200  1.5    0  0
    2016 "A020" "ACERENZA"                4 10.6 200    0    0  0
    2015 "A020" "ACERENZA"                4 10.6 200    2    0  0
    2012 "A020" "ACERENZA"                4 10.6 200    .    .  .
    2016 "A023" "ACERNO"                  6 10.6 200    0    0  0
    2015 "A023" "ACERNO"                  6 10.6 200  3.3    0 -1
    2013 "A023" "ACERNO"                  6 10.6 200    .    .  .
    2012 "A023" "ACERNO"                  6  8.6 200    .    .  .
    2014 "A023" "ACERNO"                  6 10.6 200  3.3    0 -1
    2013 "A024" "ACERRA"                  4 10.6 200    .    .  .
    2014 "A024" "ACERRA"                  4 10.6 200  3.3    0  0
    2016 "A024" "ACERRA"                  4 10.6 200  2.8    0  0
    2012 "A024" "ACERRA"                  4 10.6 200    .    .  .
    2015 "A024" "ACERRA"                  4 10.6 200  3.3    0  0
    2015 "A025" "ACI BONACCORSI"          4    9 200  2.5  1.6 -1
    2016 "A025" "ACI BONACCORSI"          4    9 200    2  1.6  0
    2014 "A025" "ACI BONACCORSI"          4    9 200  2.5  1.6  0
    2013 "A025" "ACI BONACCORSI"          4    9 200    .    .  .
    2012 "A025" "ACI BONACCORSI"          4  7.6 200    .    .  .
    2016 "A026" "ACI CASTELLO"            4 10.6 200    0    0  0
    2014 "A026" "ACI CASTELLO"            4 10.6 200  2.5    0  0
    2015 "A026" "ACI CASTELLO"            4 10.6 200  2.5    0  0
    2012 "A026" "ACI CASTELLO"            4 10.5 200    .    .  .
    2013 "A026" "ACI CASTELLO"            4 10.6 200    .    .  .
    end

  • #2
    Code:
    isid CODICECATASTALE ANNO, sort
    assert ANNO >= 2012
    by CODICECATASTALE (ANNO): gen expander = 2 if _n == 1
    expand expander
    by CODICECATASTALE (ANNO), sort: replace ANNO = 2011 if _n == 1
    isid CODICECATASTALE ANNO, sort
    ds ANNO CODICECATASTALE name_short, not
    foreach v of varlist `r(varlist)' {
        replace `v' = . if ANNO == 2011
    }

    Comment


    • #3
      Hi Clyde Schechter . That works really well. thank you so much.

      May I ask you one more advice. I have a another panel dataset which I have dates from 2011 to 2015 (year variable).
      My identifier is again "name_short".

      In this case I want to expand the dataset till 2016. Where some variables (e.g. a, b, c) are carried forward, others (e.g. x, y, z) will have missing values for the new 2016 year.

      May I kindly ask you how can I do it?
      Sorry for not posting a -dataex- but the amount of variables is too much to be extracted.

      Many thanks for your kindness and patience.
      Last edited by Marco Errico; 04 Jun 2023, 02:43.

      Comment


      • #4
        Why not ask for 2017 2018 ... 2022 too?

        If you have no data whatsoever for 2016, what is the rationale for doing this?

        Comment


        • #5
          Hi Nick Cox ,

          I have to fuzzy match two dataset based on name and years. In one of them I have data till 2016, in the other I have not (and the panel finishes in 2015). I would love to marge both of them and incorporate the data for 2016 from the dataset that I have.
          Sorry I have not been clear in explaining my request

          Comment


          • #6
            Expanding the data set on the end of the time series instead of the beginning is scarcely different. Similarly, having some additional variables to carry forward values is also a minor modification.

            Code:
            isid CODICECATASTALE ANNO, sort
            assert ANNO <= 2015
            by CODICECATASTALE (ANNO): gen expander = 2 if _n == _N
            expand expander
            by CODICECATASTALE (ANNO), sort: replace ANNO = 2016 if _n == _N
            isid CODICECATASTALE ANNO, sort
            local carry_fowards var1 var2 var3 var4 // LIST ALL CARRY FORWARDS HERE
            ds ANNO CODICECATASTALE name_short `carry_forwards', not
            foreach v of varlist `r(varlist)' {
                replace `v' = . if ANNO == 2016
            }
            Last edited by Clyde Schechter; 04 Jun 2023, 09:44.

            Comment


            • #7
              Truly thank you, Clyde Schechter .

              I will implement it. Thanks for your advice.

              Comment

              Working...
              X