Announcement

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

  • #16
    Thanks for the reply. Below a code and at the end i commented one (two) issue.

    Code:
            clear
            *input dataset
            input int year quarter str34 origin long(totalpersonspendingstartyear appliedduringyear rejected)
            2003 1 "China"                      0   4   0
            2003 2 "China"                      4   0   0
            2003 3 "China"                      4   2   0
            2003 4 "China"                      2   0   0
            2003 1 "Japan"                      1   1   1    
            2003 2 "Japan"                      1   1   1
            2003 3 "Japan"                      1   1   1
            2003 4 "Japan"                      1   1   1
            2004 1 "Japan"                      3   1   1
            2004 2 "Japan"                      1   6   1
            2003 3 "Korea"                      1   1   1
            2003 4 "Korea"                      1   1   1
            2004 1 "Korea"                      1   1   1
            2004 2 "Korea"                      1   1   1
            2005 3 "Taiwan"                     1   1   1
            2005 4 "Taiwan"                     1   1   1
            2006 1 "Taiwan"                     1   1   1
            2006 2 "Taiwan"                     1   1   1
            2004 2 "Laos"                       1   1   1
            2004 3 "Laos"                       1   1   1
            2004 4 "Laos"                       1   1   1
            2005 1 "Laos"                       1   1   1
            end
    
            *cleansing
            capture noisily drop date_yq
            gen date_yq = yq(year, quarter)
            format %tq date_yq
    
            *rename variable 
            rename origin country
    
            *move date_yq
            move date_yq country
    
            //your code starts from here
    
            expand 3, gen(expandob) //adding two more versions of every observation currently in your dataset
            sort country expandob date_yq //sorting by origin, then whether it's an observation we've just created, then by year
    
            ds date_yq country expandob, not 
            local toempty `r(varlist)' //storing all the variables that you want to be empty in a local
            foreach var of local toempty{ 
                replace `var' = . if expandob == 1 //making these variables empty
            }
    
            foreach var of varlist date_yq country{
                bysort country: replace date_yq = date_yq[_n-1]+1 if expandob == 1 //replacing the years so they're sequential from the most recent year for each country in your original data 
            }
    
            drop expandob //dropping the expandob variable
    
            sort country date_yq
    
            *problems:
            * - each country should have as end date 2019q3, not different. +
            * It is fine if the starting date is different
            * - I should fill in also year quarter (easy to do)

    Comment


    • #17
      Fixes for the problems:
      Code:
              *cleansing
              capture noisily drop date_yq
              gen date_yq = yq(year, quarter)
              format %tq date_yq
      
              *rename variable
              rename origin country
      
              *move date_yq
              move date_yq country
      
              //your code starts from here
              by country (date_yq), sort: gen last_quarter = date_yq[_N]
              gen int expander = cond(date_yq == last_quarter, tq(2019q3) - last_quarter + 1, ///
                  1)
              expand expander, gen(expandob) //adding two more versions of every observation currently in your dataset
              sort country expandob date_yq //sorting by origin, then whether it's an observation we've just created, then by year
      
              ds date_yq country expandob, not
              local toempty `r(varlist)' //storing all the variables that you want to be empty in a local
              foreach var of local toempty{
                  replace `var' = . if expandob == 1 //making these variables empty
              }
      
              foreach var of varlist date_yq country{
                  bysort country: replace date_yq = date_yq[_n-1]+1 if expandob == 1 //replacing the years so they're sequential from the most recent year for each country in your original data
              }
      
              drop expandob expander //dropping the expandob variable
              replace year = year(dofq(date_yq))
              replace quarter = quarter(dofq(date_yq))
      
              sort country date_yq

      Comment


      • #18
        Thanks a lot!

        Comment


        • #19
          Hi to everyone,

          I would like to ask how I can expand backwards the time by adding more rows but with quarterly time data. Thank you very much for the help.

          Comment


          • #20
            Please fire up the -dataex- command and show example data, and state specifically how far backwards you want to go. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

            Comment


            • #21
              You are right, thanks Clyde.

              I would like to go back until the year 2000q1 for each different observation of the id variable (here there are just 3 different id number) and leaving empty the new rows except from id and qdate variables.


              Here the example:

              Code:
              input str15 id byte quarter int year float qdate double loans
              "ATFEB27753" 1 2014 216                .
              "ATFEB27753" 2 2014 217                .
              "ATFEB27753" 3 2014 218                .
              "ATFEB27753" 4 2014 219                .
              "ATFEB27753" 1 2015 220                .
              "ATFEB27753" 2 2015 221                .
              "ATFEB27753" 3 2015 222                .
              "ATFEB27753" 4 2015 223                .
              "ATFEB27753" 1 2016 224                .
              "ATFEB27753" 2 2016 225                .
              "ATFEB27753" 3 2016 226                .
              "ATFEB27753" 4 2016 227                .
              "ATFEB27753" 1 2017 228                .
              "ATFEB27753" 2 2017 229 18845935.7967407
              "ATFEB27753" 3 2017 230                .
              "ATFEB27753" 4 2017 231                .
              "ATFEB27753" 1 2018 232                .
              "ATFEB27753" 2 2018 233 20354008.1255245
              "ATFEB27753" 3 2018 234                .
              "ATFEB27753" 4 2018 235                .
              "ATFEB27753" 1 2019 236                .
              "ATFEB27753" 2 2019 237                .
              "ATFEB27753" 3 2019 238                .
              "ATFEB27753" 4 2019 239                .
              "ATFEB27753" 1 2020 240                .
              "ATFEB27753" 2 2020 241                .
              "ATFEB27753" 3 2020 242                .
              "ATFEB27753" 4 2020 243                .
              "ATFEB27753" 1 2021 244                .
              "ATFEB27753" 2 2021 245                .
              "ATFEB28761" 1 2014 216                .
              "ATFEB28761" 2 2014 217                .
              "ATFEB28761" 3 2014 218                .
              "ATFEB28761" 4 2014 219                .
              "ATFEB28761" 1 2015 220                .
              "ATFEB28761" 2 2015 221                .
              "ATFEB28761" 3 2015 222                .
              "ATFEB28761" 4 2015 223                .
              "ATFEB28761" 1 2016 224                .
              "ATFEB28761" 2 2016 225                .
              "ATFEB28761" 3 2016 226                .
              "ATFEB28761" 4 2016 227                .
              "ATFEB28761" 1 2017 228                .
              "ATFEB28761" 2 2017 229 21788429.6154588
              "ATFEB28761" 3 2017 230                .
              "ATFEB28761" 4 2017 231 23274255.1528406
              "ATFEB28761" 1 2018 232                .
              "ATFEB28761" 2 2018 233 22404873.1597519
              "ATFEB28761" 3 2018 234                .
              "ATFEB28761" 4 2018 235 22815545.9918965
              "ATFEB28761" 1 2019 236                .
              "ATFEB28761" 2 2019 237  23154948.224618
              "ATFEB28761" 3 2019 238                .
              "ATFEB28761" 4 2019 239 23337393.9741108
              "ATFEB28761" 1 2020 240                .
              "ATFEB28761" 2 2020 241 23242847.2211626
              "ATFEB28761" 3 2020 242                .
              "ATFEB28761" 4 2020 243 25634405.0044388
              "ATFEB28761" 1 2021 244                .
              "ATFEB28761" 2 2021 245                .
              "ATFEB29927" 1 2014 216                .
              "ATFEB29927" 2 2014 217                .
              "ATFEB29927" 3 2014 218                .
              "ATFEB29927" 4 2014 219                .
              "ATFEB29927" 1 2015 220                .
              "ATFEB29927" 2 2015 221                .
              "ATFEB29927" 3 2015 222                .
              "ATFEB29927" 4 2015 223                .
              "ATFEB29927" 1 2016 224                .
              "ATFEB29927" 2 2016 225                .
              "ATFEB29927" 3 2016 226                .
              "ATFEB29927" 4 2016 227                .
              "ATFEB29927" 1 2017 228                .
              "ATFEB29927" 2 2017 229                .
              "ATFEB29927" 3 2017 230                .
              "ATFEB29927" 4 2017 231                .
              "ATFEB29927" 1 2018 232                .
              "ATFEB29927" 2 2018 233                .
              "ATFEB29927" 3 2018 234                .
              "ATFEB29927" 4 2018 235                .
              "ATFEB29927" 1 2019 236                .
              "ATFEB29927" 2 2019 237                .
              "ATFEB29927" 3 2019 238                .
              "ATFEB29927" 4 2019 239                .
              "ATFEB29927" 1 2020 240                .
              "ATFEB29927" 2 2020 241                .
              "ATFEB29927" 3 2020 242                .
              "ATFEB29927" 4 2020 243                .
              "ATFEB29927" 1 2021 244                .
              "ATFEB29927" 2 2021 245                .
              end
              format %tq qdate
              Last edited by Giuseppe Maffia; 06 Jul 2021, 07:07.

              Comment


              • #22
                The -tsfill- command does most of the work. But first you have to supply it with empty observations that contain the id and qdate = 2000q1.

                Also, the data have to be -xtset- for this, and that requires a numeric variable for id. (You will probably need to -xtset- this data anyway for other analyses, so may as well get it done now.)

                Code:
                levelsof id, local(ids)
                ds id qdate, not
                local other_vars `r(varlist)'
                foreach i of local ids {
                    expand 2 in L
                    replace id = `"`i'"' in L
                    replace qdate = tq(2000q1) in L
                    foreach v of varlist `other_vars' {
                        replace `v' = . in L
                    }
                }
                
                egen long n_id = group(id)
                xtset n_id qdate
                tsfill
                replace year = yofd(dofq(qdate))
                replace quarter = quarter(dofq(qdate))
                Note: I assume that, although you said all other variables should be left missing, that you want year and quarter to correspond to the year and quarter of the qdate in the newly created observations.

                Comment


                • #23
                  Thank you, but I'm sorry that I have tried and this code doesn't fix my problem. I don't know why but it doesn't make any changes to my data. Of course I have xtset the date before.

                  Comment


                  • #24
                    I don't know why either. It works with your example data from #21. See this:
                    Code:
                    . clear
                    
                    . input str15 id byte quarter int year float qdate double loans
                    
                                      id   quarter      year      qdate       loans
                      1. "ATFEB27753" 1 2014 216                .
                      2. "ATFEB27753" 2 2014 217                .
                      3. "ATFEB27753" 3 2014 218                .
                      4. "ATFEB27753" 4 2014 219                .
                      5. "ATFEB27753" 1 2015 220                .
                      6. "ATFEB27753" 2 2015 221                .
                      7. "ATFEB27753" 3 2015 222                .
                      8. "ATFEB27753" 4 2015 223                .
                      9. "ATFEB27753" 1 2016 224                .
                     10. "ATFEB27753" 2 2016 225                .
                     11. "ATFEB27753" 3 2016 226                .
                     12. "ATFEB27753" 4 2016 227                .
                     13. "ATFEB27753" 1 2017 228                .
                     14. "ATFEB27753" 2 2017 229 18845935.7967407
                     15. "ATFEB27753" 3 2017 230                .
                     16. "ATFEB27753" 4 2017 231                .
                     17. "ATFEB27753" 1 2018 232                .
                     18. "ATFEB27753" 2 2018 233 20354008.1255245
                     19. "ATFEB27753" 3 2018 234                .
                     20. "ATFEB27753" 4 2018 235                .
                     21. "ATFEB27753" 1 2019 236                .
                     22. "ATFEB27753" 2 2019 237                .
                     23. "ATFEB27753" 3 2019 238                .
                     24. "ATFEB27753" 4 2019 239                .
                     25. "ATFEB27753" 1 2020 240                .
                     26. "ATFEB27753" 2 2020 241                .
                     27. "ATFEB27753" 3 2020 242                .
                     28. "ATFEB27753" 4 2020 243                .
                     29. "ATFEB27753" 1 2021 244                .
                     30. "ATFEB27753" 2 2021 245                .
                     31. "ATFEB28761" 1 2014 216                .
                     32. "ATFEB28761" 2 2014 217                .
                     33. "ATFEB28761" 3 2014 218                .
                     34. "ATFEB28761" 4 2014 219                .
                     35. "ATFEB28761" 1 2015 220                .
                     36. "ATFEB28761" 2 2015 221                .
                     37. "ATFEB28761" 3 2015 222                .
                     38. "ATFEB28761" 4 2015 223                .
                     39. "ATFEB28761" 1 2016 224                .
                     40. "ATFEB28761" 2 2016 225                .
                     41. "ATFEB28761" 3 2016 226                .
                     42. "ATFEB28761" 4 2016 227                .
                     43. "ATFEB28761" 1 2017 228                .
                     44. "ATFEB28761" 2 2017 229 21788429.6154588
                     45. "ATFEB28761" 3 2017 230                .
                     46. "ATFEB28761" 4 2017 231 23274255.1528406
                     47. "ATFEB28761" 1 2018 232                .
                     48. "ATFEB28761" 2 2018 233 22404873.1597519
                     49. "ATFEB28761" 3 2018 234                .
                     50. "ATFEB28761" 4 2018 235 22815545.9918965
                     51. "ATFEB28761" 1 2019 236                .
                     52. "ATFEB28761" 2 2019 237  23154948.224618
                     53. "ATFEB28761" 3 2019 238                .
                     54. "ATFEB28761" 4 2019 239 23337393.9741108
                     55. "ATFEB28761" 1 2020 240                .
                     56. "ATFEB28761" 2 2020 241 23242847.2211626
                     57. "ATFEB28761" 3 2020 242                .
                     58. "ATFEB28761" 4 2020 243 25634405.0044388
                     59. "ATFEB28761" 1 2021 244                .
                     60. "ATFEB28761" 2 2021 245                .
                     61. "ATFEB29927" 1 2014 216                .
                     62. "ATFEB29927" 2 2014 217                .
                     63. "ATFEB29927" 3 2014 218                .
                     64. "ATFEB29927" 4 2014 219                .
                     65. "ATFEB29927" 1 2015 220                .
                     66. "ATFEB29927" 2 2015 221                .
                     67. "ATFEB29927" 3 2015 222                .
                     68. "ATFEB29927" 4 2015 223                .
                     69. "ATFEB29927" 1 2016 224                .
                     70. "ATFEB29927" 2 2016 225                .
                     71. "ATFEB29927" 3 2016 226                .
                     72. "ATFEB29927" 4 2016 227                .
                     73. "ATFEB29927" 1 2017 228                .
                     74. "ATFEB29927" 2 2017 229                .
                     75. "ATFEB29927" 3 2017 230                .
                     76. "ATFEB29927" 4 2017 231                .
                     77. "ATFEB29927" 1 2018 232                .
                     78. "ATFEB29927" 2 2018 233                .
                     79. "ATFEB29927" 3 2018 234                .
                     80. "ATFEB29927" 4 2018 235                .
                     81. "ATFEB29927" 1 2019 236                .
                     82. "ATFEB29927" 2 2019 237                .
                     83. "ATFEB29927" 3 2019 238                .
                     84. "ATFEB29927" 4 2019 239                .
                     85. "ATFEB29927" 1 2020 240                .
                     86. "ATFEB29927" 2 2020 241                .
                     87. "ATFEB29927" 3 2020 242                .
                     88. "ATFEB29927" 4 2020 243                .
                     89. "ATFEB29927" 1 2021 244                .
                     90. "ATFEB29927" 2 2021 245                .
                     91. end
                    
                    . format %tq qdate
                    
                    .
                    . levelsof id, local(ids)
                    `"ATFEB27753"' `"ATFEB28761"' `"ATFEB29927"'
                    
                    . ds id qdate, not
                    quarter  year     loans
                    
                    . local other_vars `r(varlist)'
                    
                    . foreach i of local ids {
                      2.     expand 2 in L
                      3.     replace id = `"`i'"' in L
                      4.     replace qdate = tq(2000q1) in L
                      5.     foreach v of varlist `other_vars' {
                      6.         replace `v' = . in L
                      7.     }
                      8. }
                    (1 observation created)
                    (1 real change made)
                    (1 real change made)
                    (1 real change made, 1 to missing)
                    (1 real change made, 1 to missing)
                    (0 real changes made)
                    (1 observation created)
                    (1 real change made)
                    (0 real changes made)
                    (0 real changes made)
                    (0 real changes made)
                    (0 real changes made)
                    (1 observation created)
                    (1 real change made)
                    (0 real changes made)
                    (0 real changes made)
                    (0 real changes made)
                    (0 real changes made)
                    
                    .
                    . egen long n_id = group(id)
                    
                    . xtset n_id qdate
                    
                    Panel variable: n_id (strongly balanced)
                     Time variable: qdate, 2000q1 to 2021q2, but with gaps
                             Delta: 1 quarter
                    
                    . tsfill
                    
                    . replace year = yofd(dofq(qdate))
                    (168 real changes made)
                    
                    . replace quarter = quarter(dofq(qdate))
                    (168 real changes made)
                    
                    .
                    . list in 1/25
                    
                         +-----------------------------------------------------+
                         |         id   quarter   year    qdate   loans   n_id |
                         |-----------------------------------------------------|
                      1. | ATFEB27753         1   2000   2000q1       .      1 |
                      2. |                    2   2000   2000q2       .      1 |
                      3. |                    3   2000   2000q3       .      1 |
                      4. |                    4   2000   2000q4       .      1 |
                      5. |                    1   2001   2001q1       .      1 |
                         |-----------------------------------------------------|
                      6. |                    2   2001   2001q2       .      1 |
                      7. |                    3   2001   2001q3       .      1 |
                      8. |                    4   2001   2001q4       .      1 |
                      9. |                    1   2002   2002q1       .      1 |
                     10. |                    2   2002   2002q2       .      1 |
                         |-----------------------------------------------------|
                     11. |                    3   2002   2002q3       .      1 |
                     12. |                    4   2002   2002q4       .      1 |
                     13. |                    1   2003   2003q1       .      1 |
                     14. |                    2   2003   2003q2       .      1 |
                     15. |                    3   2003   2003q3       .      1 |
                         |-----------------------------------------------------|
                     16. |                    4   2003   2003q4       .      1 |
                     17. |                    1   2004   2004q1       .      1 |
                     18. |                    2   2004   2004q2       .      1 |
                     19. |                    3   2004   2004q3       .      1 |
                     20. |                    4   2004   2004q4       .      1 |
                         |-----------------------------------------------------|
                     21. |                    1   2005   2005q1       .      1 |
                     22. |                    2   2005   2005q2       .      1 |
                     23. |                    3   2005   2005q3       .      1 |
                     24. |                    4   2005   2005q4       .      1 |
                     25. |                    1   2006   2006q1       .      1 |
                         +-----------------------------------------------------+
                    (To save space, I've only listed the first 25 observations in the output, but you can easily see that quarters from 2000q1 have been added to the data set.)

                    I can think of a few generic possibilities why you are not getting the results when you run it:

                    1. Perhaps your actual data are materially different from the example data you gave. I think this is unlikely, though, because that would be more likely to trigger error messages than just do nothing.
                    2. Perhaps you did not copy the code correctly. Again, less likely because I would expect error messages, unless the miscopy involved just omitting whole chunks.
                    3. More likely: if you are running this code one line at a time, or in chunks that separate the place where a local macro is defined from the place where it is used, then you could easily end up with nothing happening. For example, if you ran the -ds id qdate, not- command separately from the commands that followed it the local macro r(varlist) would be "forgotten" before you got to the rest of the code. So local macro ids would be empty and the -foreach i of local ids- loop would be skipped over, hence nothing would appear to have happened to your data. Same thing would happen if there was a break in execution between -local ids `r(varlist)'- and the loop. This code must be run in one fell swoop in order to work.

                    If none of these solves your problem, please post back with new example data that reproduces the problem, and also with a copy of the code exactly as you are using it, and I'll try to troubleshoot.

                    Comment


                    • #25
                      Ok really thank you Clyde. Actually I'm running the whole code not separately and it gives me an error like "type mismatch". Anyway I attach the data and the do file that i copied. thanks.
                      Attached Files

                      Comment


                      • #26
                        Sorry, but I do not download or open attachments from people I do not know. Please use the -dataex- command to show example data, and post the relevant parts of the code directly in the Forum editor, between code delimiters. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

                        Comment


                        • #27
                          ok, sorry.

                          here there is another example with more variables and when I run the code at the end it gives me an error: "type mismatch"

                          Code:
                          * Example generated by -dataex-. To install: ssc install dataex
                          clear
                          input float id byte quarter int year float qdate double(loans equity totalasset totalliabilities) float(leverage debtcost lnloans div) double operatingrevenue float liquidityratio_1 str2 CountryISOcode float gloans double CashBalancesatCentralBank
                          1 1 2014 216                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          1 2 2014 217                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          1 3 2014 218                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          1 4 2014 219                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          1 1 2015 220                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          1 2 2015 221                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          1 3 2015 222                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          1 4 2015 223                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          1 1 2016 224                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          1 2 2016 225                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          1 3 2016 226                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          1 4 2016 227                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          1 1 2017 228                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          1 2 2017 229 18845935.7967407 1547618.26026142 29098526.6088992 26991557.4170262  .05318545 .0008512628 16.751808  .00665055 391068.516629934           . "AT" . 1064063.51501942
                          1 3 2017 230                .                .                .                .          .           .         .          .                .   .03656761 "AT" .                .
                          1 4 2017 231                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          1 1 2018 232                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          1 2 2018 233 20354008.1255245  1622698.1360817 30167032.4946499 27961306.0732555  .05379045  .000753607 16.828789 .007363349 428490.990571976           . "AT" . 1019733.50335121
                          1 3 2018 234                .                .                .                .          .           .         .          .                .   .03380291 "AT" .                .
                          1 4 2018 235                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          1 1 2019 236                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          1 2 2019 237                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          1 3 2019 238                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          1 4 2019 239                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          1 1 2020 240                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          1 2 2020 241                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          1 3 2020 242                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          1 4 2020 243                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          1 1 2021 244                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          1 2 2021 245                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          2 1 2014 216                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          2 2 2014 217                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          2 3 2014 218                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          2 4 2014 219                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          2 1 2015 220                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          2 2 2015 221                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          2 3 2015 222                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          2 4 2015 223                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          2 1 2016 224                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          2 2 2016 225                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          2 3 2016 226                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          2 4 2016 227                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          2 1 2017 228                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          2 2 2017 229 21788429.6154588 1959954.17020619 28739456.1841965 26779502.0139903  .06819733 .0022545713 16.896889 .005284382 390826.582342386           . "AT" . 2484593.23755205
                          2 3 2017 230                .                .                .                .          .           .         .          .                .   .08645234 "AT" .                .
                          2 4 2017 231 23274255.1528406 2102059.13585496  30370186.920712 28268126.5855575  .06921456 .0020711457 16.962858 .006741943 466342.842112064           . "AT" . 2400203.81062651
                          2 1 2018 232                .                .                .                .          .           .         .          .                .   .07903158 "AT" .                .
                          2 2 2018 233 22404873.1597519  2074562.2527504 30064449.0811253 27989886.8283749 .069003835 .0021306446  16.92479 .004890813 400886.010131836           . "AT" . 2439986.95560455
                          2 3 2018 234                .                .                .                .          .           .         .          .                .   .08115855 "AT" .                .
                          2 4 2018 235 22815545.9918965 2169023.82798469  30460462.944218 28291440.2612339  .07120784 .0020273046 16.942953 .006339019 431914.827644587           . "AT" . 1982733.37911177
                          2 1 2019 236                .                .                .                .          .           .         .          .                .   .06509203 "AT" .                .
                          2 2 2019 237  23154948.224618 2506088.91911697 30346057.1857624  27839969.404645  .08258367 .0021670659  16.95772  .00630177 431058.291715622           . "AT" . 1820534.10147953
                          2 3 2019 238                .                .                .                .          .           .         .          .                .   .05999244 "AT" .                .
                          2 4 2019 239 23337393.9741108 2278324.55410743 30888635.0257579 28361709.9478242  .07375932 .0016327925 16.965567 .004638402 381090.932372808           . "AT" . 2327360.95772171
                          2 1 2020 240                .                .                .                .          .           .         .          .                .   .07534683 "AT" .                .
                          2 2 2020 241 23242847.2211626  2295005.5486871 32336589.9846405 29797779.4503589 .070972405  .001632624 16.961508  .00542779 411765.951410055           . "AT" . 3818422.21391726
                          2 3 2020 242                .                .                .                .          .           .         .          .                .   .11808364 "AT" .                .
                          2 4 2020 243 25634405.0044388 2511385.10751307 36040231.6055983 33261679.9844146  .06968282  .001811854 17.059446 .005626898 450598.225346804           . "AT" .  4839385.1331234
                          2 1 2021 244                .                .                .                .          .           .         .          .                .    .1342773 "AT" .                .
                          2 2 2021 245                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          3 1 2014 216                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          3 2 2014 217                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          3 3 2014 218                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          3 4 2014 219                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          3 1 2015 220                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          3 2 2015 221                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          3 3 2015 222                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          3 4 2015 223                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          3 1 2016 224                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          3 2 2016 225                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          3 3 2016 226                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          3 4 2016 227                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          3 1 2017 228                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          3 2 2017 229                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          3 3 2017 230                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          3 4 2017 231                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          3 1 2018 232                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          3 2 2018 233                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          3 3 2018 234                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          3 4 2018 235                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          3 1 2019 236                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          3 2 2019 237                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          3 3 2019 238                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          3 4 2019 239                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          3 1 2020 240                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          3 2 2020 241                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          3 3 2020 242                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          3 4 2020 243                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          3 1 2021 244                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          3 2 2021 245                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          4 1 2014 216                .                .                .                .          .           .         .          .                .           . "AT" .                .
                          4 2 2014 217 26549102.1373147 4911923.75407398 52719764.5050198 47806431.2460601  .09317044  .007456632 17.094507 .015375167 1097238.09252799           . "AT" . 109477.009317875
                          4 3 2014 218                .                .                .                .          .           .         .          .                .  .002076584 "AT" .                .
                          4 4 2014 219 23270365.0896225  4328558.8065083 46832932.6268783 42503120.8685515  .09242553  .007093426  16.96269  .01558867 951047.492944002           . "AT" . 108159.365988255
                          4 1 2015 220                .                .                .                .          .           .         .          .                . .0023094723 "AT" .                .
                          4 2 2015 221 21481401.9278097 4099484.90716088 42444551.7807412 38343912.1688408  .09658448  .006562493 16.882698 .018751973 1015299.87696612           . "AT" . 144655.860029697
                          4 3 2015 222                .                .                .                .          .           .         .          .                .  .003408114 "AT" .                .
                          4 4 2015 223 20392774.9257318 4069956.65078497 40606952.9816203 36535872.7925005  .10022807  .006555852  16.83069 .018986344 978122.861679077           . "AT" .  98223.597004056
                          4 1 2016 224                .                .                .                .          .           .         .          .                .  .002418886 "AT" .                .
                          4 2 2016 225 21392300.3891361 4202851.01379275 42863808.0006605 38659811.2604188  .09805127  .005638701 16.878542 .015955612 883169.688780904           . "AT" . 102595.806788921
                          end
                          format %tq qdate
                          
                          levelsof id, local(ids)
                          ds id qdate, not
                          local other_vars `r(varlist)'
                          foreach i of local ids {
                              expand 2 in L
                              replace id = `"`i'"' in L
                              replace qdate = tq(2000q1) in L
                              foreach v of varlist `other_vars' {
                                  replace `v' = . in L
                              }
                          }
                          
                          egen long n_id = group(id)
                          xtset n_id qdate
                          tsfill
                          replace year = yofd(dofq(qdate))
                          replace quarter = quarter(dofq(qdate))

                          thanks anyway.

                          Comment


                          • #28
                            and another thing is that in the dataset every -id- has the same final year 2021q2. In the example with dataex the id number 4 ends in 2016q2 but actually it continues untill 2021q2 as every id. thank you.

                            Comment


                            • #29
                              So you made two major changes to the data, as well as changing the problem specification. The major data changes were using a numeric variable for id, whereas in your original example you used a string, and adding a string variable, CountryISOcode, whereas the original example had only numeric variables. Those are the things that caused the type mismatch error. The change in the problem specification actually makes the solution easier--part of the complication in the code before was to prevent the observations from extending forward in time beyond their original ending dates! So now it's just this:

                              Code:
                              preserve
                              keep id
                              duplicates drop
                              expand tq(2021q2)-tq(2000q1) + 1
                              by id, sort: gen int qdate = tq(1999q4) + _n
                              tabstat qdate, by(id) statistics(min max) format(%tq)
                              tempfile rectangle
                              save `rectangle'
                              
                              restore
                              merge 1:1 id qdate using `rectangle', assert(match using) nogenerate
                              xtset id qdate
                              
                              replace year = yofd(dofq(qdate))
                              replace quarter = quarter(dofq(qdate))

                              Comment


                              • #30
                                thank you, but in the data that I have to use, after the comand -merge-, it appears an error like this: "merge: after merge, not all observations from using or matched
                                (merged result left in memory)" . I don't know why, I'm trying to solve out. As you said the id variable is numeric and I have also others variables as strings.

                                Comment

                                Working...
                                X