Announcement

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

  • Combine or reshape panel data

    Hi,

    I have a dataset on ownership structure of different types of houses in Swedish regions between the years 2013-2018. I have reshaped the data to get one variable for "year" (before, the number of houses in each category was listed under five year-specific variables for each year 2013-2018). However, this leaves me with repeated observations for each year, region and type of house. More specifically, each row now contains the region, the type of house, the year, and data for the number of houses in one of the owner categories (starting with Yr*). And it continues like this for each of the owner categories. Can I combine the rows in some way, so that for each region, house type and year I get all the numbers of houses per category of owner on the same row?

    Or, alternatively, does anyone know how to use the command -reshape- in a better way than I do to get data in panel form with variables for the ownership categories? I used the instructions from this article when doing my reshape: https://www.stata-journal.com/sjpdf....iclenum=dm0031.

    Thanks a lot.

    1. See a data example below from after I reshaped it, it looks a bit messy because of the long variable names but you see the structure of the missing values:

    [
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float id str20 region str15 house_type int year long(Yrallmannyttiga_bostadsforetag Yrbrf Yrcoop_association Yrindividual Yrna Yrother_owner Yrpublic Yrswedish_noted_companies)
    1 "0114 Upplands Väsby" "small_house"     2013   .   . .    . . . 25   .
    1 "0114 Upplands Väsby" "small_house"     2014   .   . .    . . . 21   .
    1 "0114 Upplands Väsby" "small_house"     2015   .   . .    . . . 20   .
    1 "0114 Upplands Väsby" "small_house"     2016   .   . .    . . . 13   .
    1 "0114 Upplands Väsby" "small_house"     2017   .   . .    . . . 13   .
    1 "0114 Upplands Väsby" "small_house"     2018   .   . .    . . . 12   .
    2 "0114 Upplands Väsby" "small_house"     2013 142   . .    . . .  .   .
    2 "0114 Upplands Väsby" "small_house"     2014 142   . .    . . .  .   .
    2 "0114 Upplands Väsby" "small_house"     2015 142   . .    . . .  .   .
    2 "0114 Upplands Väsby" "small_house"     2016 142   . .    . . .  .   .
    2 "0114 Upplands Väsby" "small_house"     2017 136   . .    . . .  .   .
    2 "0114 Upplands Väsby" "small_house"     2018 161   . .    . . .  .   .
    3 "0114 Upplands Väsby" "small_house"     2013   .   . 0    . . .  .   .
    3 "0114 Upplands Väsby" "small_house"     2014   .   . 0    . . .  .   .
    3 "0114 Upplands Väsby" "small_house"     2015   .   . 0    . . .  .   .
    3 "0114 Upplands Väsby" "small_house"     2016   .   . 0    . . .  .   .
    3 "0114 Upplands Väsby" "small_house"     2017   .   . 0    . . .  .   .
    3 "0114 Upplands Väsby" "small_house"     2018   .   . 0    . . .  .   .
    4 "0114 Upplands Väsby" "small_house"     2013   . 353 .    . . .  .   .
    4 "0114 Upplands Väsby" "small_house"     2014   . 355 .    . . .  .   .
    4 "0114 Upplands Väsby" "small_house"     2015   . 409 .    . . .  .   .
    4 "0114 Upplands Väsby" "small_house"     2016   . 460 .    . . .  .   .
    4 "0114 Upplands Väsby" "small_house"     2017   . 530 .    . . .  .   .
    4 "0114 Upplands Väsby" "small_house"     2018   . 644 .    . . .  .   .
    5 "0114 Upplands Väsby" "small_house"     2013   .   . . 5610 . .  .   .
    5 "0114 Upplands Väsby" "small_house"     2014   .   . . 5642 . .  .   .
    5 "0114 Upplands Väsby" "small_house"     2015   .   . . 5665 . .  .   .
    5 "0114 Upplands Väsby" "small_house"     2016   .   . . 5725 . .  .   .
    5 "0114 Upplands Väsby" "small_house"     2017   .   . . 5761 . .  .   .
    5 "0114 Upplands Väsby" "small_house"     2018   .   . . 5795 . .  .   .
    6 "0114 Upplands Väsby" "small_house"     2013   .   . .    . . .  .  49
    6 "0114 Upplands Väsby" "small_house"     2014   .   . .    . . .  .  45
    6 "0114 Upplands Väsby" "small_house"     2015   .   . .    . . .  .  93
    6 "0114 Upplands Väsby" "small_house"     2016   .   . .    . . .  .  93
    6 "0114 Upplands Väsby" "small_house"     2017   .   . .    . . .  . 142
    6 "0114 Upplands Väsby" "small_house"     2018   .   . .    . . .  . 125
    7 "0114 Upplands Väsby" "small_house"     2013   .   . .    . . 5  .   .
    7 "0114 Upplands Väsby" "small_house"     2014   .   . .    . . 4  .   .
    7 "0114 Upplands Väsby" "small_house"     2015   .   . .    . . 4  .   .
    7 "0114 Upplands Väsby" "small_house"     2016   .   . .    . . 5  .   .
    7 "0114 Upplands Väsby" "small_house"     2017   .   . .    . . 4  .   .
    7 "0114 Upplands Väsby" "small_house"     2018   .   . .    . . 5  .   .
    8 "0114 Upplands Väsby" "small_house"     2013   .   . .    . 2 .  .   .
    8 "0114 Upplands Väsby" "small_house"     2014   .   . .    . 0 .  .   .
    8 "0114 Upplands Väsby" "small_house"     2015   .   . .    . 0 .  .   .
    8 "0114 Upplands Väsby" "small_house"     2016   .   . .    . 0 .  .   .
    8 "0114 Upplands Väsby" "small_house"     2017   .   . .    . 0 .  .   .
    8 "0114 Upplands Väsby" "small_house"     2018   .   . .    . 0 .  .   .
    9 "0114 Upplands Väsby" "apartment_house" 2013   .   . .    . . .  0   .
    9 "0114 Upplands Väsby" "apartment_house" 2014   .   . .    . . .  0   .
    end
    2. Before I did the reshaping, the data looked like this:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str20 region str15 house_type str28 owner_category long(Yr2013 Yr2014 Yr2015 Yr2016 Yr2017 Yr2018) float id
    "0114 Upplands Väsby" "small_house"     "public"                         25   21   20   13   13   12  1
    "0114 Upplands Väsby" "small_house"     "allmannyttiga_bostadsforetag"  142  142  142  142  136  161  2
    "0114 Upplands Väsby" "small_house"     "coop_association"                0    0    0    0    0    0  3
    "0114 Upplands Väsby" "small_house"     "brf"                           353  355  409  460  530  644  4
    "0114 Upplands Väsby" "small_house"     "individual"                   5610 5642 5665 5725 5761 5795  5
    "0114 Upplands Väsby" "small_house"     "swedish_noted_companies"        49   45   93   93  142  125  6
    "0114 Upplands Väsby" "small_house"     "other_owner"                     5    4    4    5    4    5  7
    "0114 Upplands Väsby" "small_house"     "na"                              2    0    0    0    0    0  8
    "0114 Upplands Väsby" "apartment_house" "public"                          0    0    0    0    0    0  9
    "0114 Upplands Väsby" "apartment_house" "allmannyttiga_bostadsforetag" 5060 4784 5144 5033 4438 4365 10
    "0114 Upplands Väsby" "apartment_house" "coop_association"                0    0    0    0    0    0 11
    "0114 Upplands Väsby" "apartment_house" "brf"                          5251 5617 5555 5994 6048 6472 12
    "0114 Upplands Väsby" "apartment_house" "individual"                     13   13   13   13   13   13 13
    "0114 Upplands Väsby" "apartment_house" "swedish_noted_companies"      1108  922 1015  876 1750 1691 14
    "0114 Upplands Väsby" "apartment_house" "other_owner"                     0  216  216  216    0    0 15
    "0114 Upplands Väsby" "apartment_house" "na"                              0    0    0    0    0    0 16
    "0114 Upplands Väsby" "other_house"     "public"                          3    2    2    2    2    2 17
    "0114 Upplands Väsby" "other_house"     "allmannyttiga_bostadsforetag"   76   76    4    4    4    4 18
    "0114 Upplands Väsby" "other_house"     "coop_association"                0    0    0    0    0    0 19
    "0114 Upplands Väsby" "other_house"     "brf"                            41   36   36   15   16   16 20
    "0114 Upplands Väsby" "other_house"     "individual"                      8    8    7    6    5    5 21
    "0114 Upplands Väsby" "other_house"     "swedish_noted_companies"        15   15   15   15   17   18 22
    "0114 Upplands Väsby" "other_house"     "other_owner"                     5    5    4    4    2    2 23
    "0114 Upplands Väsby" "other_house"     "na"                              0    0    0    0    0    0 24
    "0115 Vallentuna"      "small_house"     "public"                         28   27   24   28   29   28 25
    "0115 Vallentuna"      "small_house"     "allmannyttiga_bostadsforetag"    0    0    0    0    0    0 26
    "0115 Vallentuna"      "small_house"     "coop_association"                0    0    0    0    0    0 27
    "0115 Vallentuna"      "small_house"     "brf"                           612  597  637  714  703  705 28
    "0115 Vallentuna"      "small_house"     "individual"                   7201 7299 7392 7728 7856 7932 29
    "0115 Vallentuna"      "small_house"     "swedish_noted_companies"        71   65   72   86   80   78 30
    "0115 Vallentuna"      "small_house"     "other_owner"                    99   98  104   10   13   13 31
    "0115 Vallentuna"      "small_house"     "na"                              3   25    0    0    0    0 32
    "0115 Vallentuna"      "apartment_house" "public"                         12   12   12   12   12   12 33
    "0115 Vallentuna"      "apartment_house" "allmannyttiga_bostadsforetag"   72   72   72   72   72   72 34
    "0115 Vallentuna"      "apartment_house" "coop_association"                0    0    0    0    0    0 35
    "0115 Vallentuna"      "apartment_house" "brf"                          3549 3550 3618 3710 3710 3732 36
    "0115 Vallentuna"      "apartment_house" "individual"                     33   20   20   20   20   20 37
    "0115 Vallentuna"      "apartment_house" "swedish_noted_companies"       132  161  112  112  119  276 38
    "0115 Vallentuna"      "apartment_house" "other_owner"                    73   73   73   73   73   73 39
    "0115 Vallentuna"      "apartment_house" "na"                              0    0    0    0    0    0 40
    "0115 Vallentuna"      "other_house"     "public"                          1    1    0    0    0    0 41
    "0115 Vallentuna"      "other_house"     "allmannyttiga_bostadsforetag"    0    0    0    0    0    0 42
    "0115 Vallentuna"      "other_house"     "coop_association"                0    0    0    0    0    0 43
    "0115 Vallentuna"      "other_house"     "brf"                            22   22   22   22   22   22 44
    "0115 Vallentuna"      "other_house"     "individual"                     15   18   18   17   16   19 45
    "0115 Vallentuna"      "other_house"     "swedish_noted_companies"        17    1    2    2    2    2 46
    "0115 Vallentuna"      "other_house"     "other_owner"                    16   16    5    5    6    6 47
    "0115 Vallentuna"      "other_house"     "na"                              0    0    0    0    0    0 48
    "0117 Österåker"     "small_house"     "public"                          8    4    4    4    4    4 49
    "0117 Österåker"     "small_house"     "allmannyttiga_bostadsforetag"   83   83   83   83   83   83 50
    end
    Thank you.

  • #2
    I don't quite get how you got to the first data you show. It is not the obvious result of any -reshape-ing of the data you started with. Anyway, whatever you did, it led you down a wrong path. Go back to your original data, and then you can get to what you want with two -reshape-s.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str20 region str15 house_type str28 owner_category long(Yr2013 Yr2014 Yr2015 Yr2016 Yr2017 Yr2018) float id
    "0114 Upplands Väsby" "small_house"     "public"                         25   21   20   13   13   12  1
    "0114 Upplands Väsby" "small_house"     "allmannyttiga_bostadsforetag"  142  142  142  142  136  161  2
    "0114 Upplands Väsby" "small_house"     "coop_association"                0    0    0    0    0    0  3
    "0114 Upplands Väsby" "small_house"     "brf"                           353  355  409  460  530  644  4
    "0114 Upplands Väsby" "small_house"     "individual"                   5610 5642 5665 5725 5761 5795  5
    "0114 Upplands Väsby" "small_house"     "swedish_noted_companies"        49   45   93   93  142  125  6
    "0114 Upplands Väsby" "small_house"     "other_owner"                     5    4    4    5    4    5  7
    "0114 Upplands Väsby" "small_house"     "na"                              2    0    0    0    0    0  8
    "0114 Upplands Väsby" "apartment_house" "public"                          0    0    0    0    0    0  9
    "0114 Upplands Väsby" "apartment_house" "allmannyttiga_bostadsforetag" 5060 4784 5144 5033 4438 4365 10
    "0114 Upplands Väsby" "apartment_house" "coop_association"                0    0    0    0    0    0 11
    "0114 Upplands Väsby" "apartment_house" "brf"                          5251 5617 5555 5994 6048 6472 12
    "0114 Upplands Väsby" "apartment_house" "individual"                     13   13   13   13   13   13 13
    "0114 Upplands Väsby" "apartment_house" "swedish_noted_companies"      1108  922 1015  876 1750 1691 14
    "0114 Upplands Väsby" "apartment_house" "other_owner"                     0  216  216  216    0    0 15
    "0114 Upplands Väsby" "apartment_house" "na"                              0    0    0    0    0    0 16
    "0114 Upplands Väsby" "other_house"     "public"                          3    2    2    2    2    2 17
    "0114 Upplands Väsby" "other_house"     "allmannyttiga_bostadsforetag"   76   76    4    4    4    4 18
    "0114 Upplands Väsby" "other_house"     "coop_association"                0    0    0    0    0    0 19
    "0114 Upplands Väsby" "other_house"     "brf"                            41   36   36   15   16   16 20
    "0114 Upplands Väsby" "other_house"     "individual"                      8    8    7    6    5    5 21
    "0114 Upplands Väsby" "other_house"     "swedish_noted_companies"        15   15   15   15   17   18 22
    "0114 Upplands Väsby" "other_house"     "other_owner"                     5    5    4    4    2    2 23
    "0114 Upplands Väsby" "other_house"     "na"                              0    0    0    0    0    0 24
    "0115 Vallentuna"      "small_house"     "public"                         28   27   24   28   29   28 25
    "0115 Vallentuna"      "small_house"     "allmannyttiga_bostadsforetag"    0    0    0    0    0    0 26
    "0115 Vallentuna"      "small_house"     "coop_association"                0    0    0    0    0    0 27
    "0115 Vallentuna"      "small_house"     "brf"                           612  597  637  714  703  705 28
    "0115 Vallentuna"      "small_house"     "individual"                   7201 7299 7392 7728 7856 7932 29
    "0115 Vallentuna"      "small_house"     "swedish_noted_companies"        71   65   72   86   80   78 30
    "0115 Vallentuna"      "small_house"     "other_owner"                    99   98  104   10   13   13 31
    "0115 Vallentuna"      "small_house"     "na"                              3   25    0    0    0    0 32
    "0115 Vallentuna"      "apartment_house" "public"                         12   12   12   12   12   12 33
    "0115 Vallentuna"      "apartment_house" "allmannyttiga_bostadsforetag"   72   72   72   72   72   72 34
    "0115 Vallentuna"      "apartment_house" "coop_association"                0    0    0    0    0    0 35
    "0115 Vallentuna"      "apartment_house" "brf"                          3549 3550 3618 3710 3710 3732 36
    "0115 Vallentuna"      "apartment_house" "individual"                     33   20   20   20   20   20 37
    "0115 Vallentuna"      "apartment_house" "swedish_noted_companies"       132  161  112  112  119  276 38
    "0115 Vallentuna"      "apartment_house" "other_owner"                    73   73   73   73   73   73 39
    "0115 Vallentuna"      "apartment_house" "na"                              0    0    0    0    0    0 40
    "0115 Vallentuna"      "other_house"     "public"                          1    1    0    0    0    0 41
    "0115 Vallentuna"      "other_house"     "allmannyttiga_bostadsforetag"    0    0    0    0    0    0 42
    "0115 Vallentuna"      "other_house"     "coop_association"                0    0    0    0    0    0 43
    "0115 Vallentuna"      "other_house"     "brf"                            22   22   22   22   22   22 44
    "0115 Vallentuna"      "other_house"     "individual"                     15   18   18   17   16   19 45
    "0115 Vallentuna"      "other_house"     "swedish_noted_companies"        17    1    2    2    2    2 46
    "0115 Vallentuna"      "other_house"     "other_owner"                    16   16    5    5    6    6 47
    "0115 Vallentuna"      "other_house"     "na"                              0    0    0    0    0    0 48
    "0117 Österåker"     "small_house"     "public"                          8    4    4    4    4    4 49
    "0117 Österåker"     "small_house"     "allmannyttiga_bostadsforetag"   83   83   83   83   83   83 50
    end
    
    rename Yr* _*
    reshape long _, i(region house_type owner_category) j(year)
    drop id
    reshape wide _, i(region house_type year) j(owner_category) string
    rename _* n_*

    Comment


    • #3
      Thank you very much Clyde - that was easier than I thought. Very thankful!

      Comment

      Working...
      X