Announcement

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

  • Can you combine data vertically?

    I am looking at data on methods of recycling for towns in the UK for five years, however my data on methods is in the wrong format.
    In order to prepare my data for panel analysis I need to remove the 'method' column and just have one observation (one row) for each town each year. To start this I created dummy variables for whether that town uses that method.

    There are several methods of recycling, but not every town uses every method.

    I currently have something that looks like this

    town year method dummymethod1 dummymethod2 dummymethod3

    Adur 2012 1 1 0 0

    Adur 2012 2 0 0 0

    Adur 2012 3 0 0 1

    .........


    Brent2012 1 0 0 0

    Brent 2012 2 0 1 0

    Brent 2012 3 0 0 1

    ......

    (Z 2016 3 )

    Where I have a new line for each method, and the dummymethod# will take the value 1 if that town uses the method in that row. eg Adur in 2012 uses method 1 and 3, Brent in 2012 uses methods 2 and 3
    However I want just one line for each town in each time period that captures all the method columns dummies (for that town in that year) in one row. This could basically mean vertically summing the columns, giving either 0 or 1(if the town uses that method) value for each town and year and having it all in one row.

    Can anyone help me with how to do this?

    *In the picture I have browsed just four dummies for simplicity but there are 30 methods and therefore 30 dummies for each town each year. I want just one observation for East Hampshire in 2015*
    Attached Files

  • #2
    Perhaps this will start you on your way. I have based my code on the sample data in your text, since Stata cannot read the picture of your data.
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str5 town int year byte method
    "Adur"  2012 1
    "Adur"  2012 3
    "Brent" 2012 2
    "Brent" 2012 3
    end
    
    rename method methnum
    generate method = 1
    reshape wide method, i(town year) j(methnum)
    recode method* (missing=0)
    list
    Code:
    . rename method methnum
    
    . generate method = 1
    
    . reshape wide method, i(town year) j(methnum)
    (note: j = 1 2 3)
    
    Data                               long   ->   wide
    -----------------------------------------------------------------------------
    Number of obs.                        4   ->       2
    Number of variables                   4   ->       5
    j variable (3 values)           methnum   ->   (dropped)
    xij variables:
                                     method   ->   method1 method2 method3
    -----------------------------------------------------------------------------
    
    . recode method* (missing=0)
    (method1: 1 changes made)
    (method2: 1 changes made)
    (method3: 0 changes made)
    
    . list
    
         +--------------------------------------------+
         |  town   year   method1   method2   method3 |
         |--------------------------------------------|
      1. |  Adur   2012         1         0         1 |
      2. | Brent   2012         0         1         1 |
         +--------------------------------------------+
    With that said, a piece of advice to improve your future posts and the answers you receive.

    Even the best descriptions of data are no substitute for an actual example of the data provided using the dataex command.

    If you are running version 15.1 or a fully updated version 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 and 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.

    When asking for help with code, always show example data. When showing example data, always use dataex.

    Comment


    • #3
      I tried this and it didn't work as "methnum not unique within code year quarter"?

      As you can see I have around 30 lines for each year and quarter.

      The reason I did not dataex is because my data is more confusing than I made out.

      I have years and quarters for each town(Adur) with code(E0700......), and the methods are split into 3 types. I then have the dummies for each method.

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str43 name str9 code int year float quarter str28 methnum float(method3 dummy32 dummy33 dummy34 dummy35)
      "Adur District Council" "E07000223" 2012 1 "3Non Reusable Sacks"          1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "3Non Reusable Sacks"          1 1 0 0 0
      "Adur District Council" "E07000223" 2012 1 "3Other"                       1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "3Other"                       1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "3Reusable Sacks"              1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "3Reusable Sacks"              1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "3Wheeled bin 120-180 litres"  1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "3Wheeled bin 120-180 litres"  1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "3Wheeled bin 181-240 litres"  1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "3Wheeled bin 181-240 litres"  1 0 0 0 1 ***The only line where a methoddummy occurs. 
      "Adur District Council" "E07000223" 2012 1 "3Wheeled bin 241litres +"     1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "3Wheeled bin 241litres +"     1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "3Wheeled bin <120 litres"     1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "3Wheeled bin <120 litres"     1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "2Kerbside box 35-50 litres"   1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "2Kerbside box 35-50 litres"   1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "2Kerbside box <35 litres"     1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "2Kerbside box <35 litres"     1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "2Kerbside box >50 litres"     1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "2Kerbside box >50 litres"     1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "2No method of containment"    1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "2No method of containment"    1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "2Non Reusable Sacks"          1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "2Non Reusable Sacks"          1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "2Other"                       1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "2Other"                       1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "2Reusable Sacks"              1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "2Reusable Sacks"              1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "2Wheeled bin 120-180 litres"  1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "2Wheeled bin 120-180 litres"  1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "2Wheeled bin 181-240 litres"  1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "2Wheeled bin 181-240 litres"  1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "2Wheeled bin 241litres +"     1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "2Wheeled bin 241litres +"     1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "2Wheeled bin <120 litres"     1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "2Wheeled bin <120 litres"     1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "1Communal bin"                1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "1Communal bin"                1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "1No method of containment"    1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "1No method of containment"    1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "1Other method of containment" 1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "1Other method of containment" 1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "1Plastic Sacks"               1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "1Plastic Sacks"               1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "1Refuse bins"                 1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "1Refuse bins"                 1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "1Wheeled Bin 100-150 litres"  1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "1Wheeled Bin 100-150 litres"  1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "1Wheeled Bin 151-250 litres"  1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "1Wheeled Bin 151-250 litres"  1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "1Wheeled Bin 251-350 litres"  1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "1Wheeled Bin 251-350 litres"  1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "1Wheeled bin >350 litres"     1 0 0 0 0
      "Adur District Council" "E07000223" 2012 1 "1Wheeled bin >350 litres"     1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "3Non Reusable Sacks"          1 1 0 0 0
      "Adur District Council" "E07000223" 2012 2 "3Non Reusable Sacks"          1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "3Other"                       1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "3Other"                       1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "3Reusable Sacks"              1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "3Reusable Sacks"              1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "3Wheeled bin 120-180 litres"  1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "3Wheeled bin 120-180 litres"  1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "3Wheeled bin 181-240 litres"  1 0 0 0 1
      "Adur District Council" "E07000223" 2012 2 "3Wheeled bin 181-240 litres"  1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "3Wheeled bin 241litres +"     1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "3Wheeled bin 241litres +"     1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "3Wheeled bin <120 litres"     1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "3Wheeled bin <120 litres"     1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "2Kerbside box 35-50 litres"   1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "2Kerbside box 35-50 litres"   1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "2Kerbside box <35 litres"     1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "2Kerbside box <35 litres"     1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "2Kerbside box >50 litres"     1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "2Kerbside box >50 litres"     1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "2No method of containment"    1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "2No method of containment"    1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "2Non Reusable Sacks"          1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "2Non Reusable Sacks"          1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "2Other"                       1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "2Other"                       1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "2Reusable Sacks"              1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "2Reusable Sacks"              1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "2Wheeled bin 120-180 litres"  1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "2Wheeled bin 120-180 litres"  1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "2Wheeled bin 181-240 litres"  1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "2Wheeled bin 181-240 litres"  1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "2Wheeled bin 241litres +"     1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "2Wheeled bin 241litres +"     1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "2Wheeled bin <120 litres"     1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "2Wheeled bin <120 litres"     1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "1Communal bin"                1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "1Communal bin"                1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "1No method of containment"    1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "1No method of containment"    1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "1Other method of containment" 1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "1Other method of containment" 1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "1Plastic Sacks"               1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "1Plastic Sacks"               1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "1Refuse bins"                 1 0 0 0 0
      "Adur District Council" "E07000223" 2012 2 "1Refuse bins"                 1 0 0 0 0
      end
      Any ideas how I can get around this?

      Thank you again

      Comment


      • #4
        I am guessing you want to collapse your data.
        I assume you have dummies for each of your methods.
        This might be what you want:
        Code:
        collapse (firstnm) code (max) method3 dummy*, by(name year quarter methnum)
        Read the entry on collapse to find out what other sort of ways you could collapse each of your varibales, e.g., take the sum, mean etc, of numeric variables. https://www.stata.com/manuals13/dcollapse.pdf


        Edit:
        Or, if you mean you want one row per area/year/quarter, with just a list of dummies for methods used, you can do:
        Code:
        collapse (firstnm) code (max) method3 dummy*, by(name year quarter)
        
        which results in:
        
        . list
        
             +------------------------------------------------------------------------------------------------------+
             |                  name   year   quarter        code   method3   dummy32   dummy33   dummy34   dummy35 |
             |------------------------------------------------------------------------------------------------------|
          1. | Adur District Council   2012         1   E07000223         1         1         0         0         1 |
          2. | Adur District Council   2012         2   E07000223         1         1         0         0         1 |
             +------------------------------------------------------------------------------------------------------+
        
        .
        Last edited by Jorrit Gosens; 15 Feb 2019, 03:49.

        Comment


        • #5
          Got it!! Thank you!!

          Comment

          Working...
          X