Announcement

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

  • Help with National Travel Survey UK

    I am working with Stata for the first time and I have been tasked with finding data on 'supercommuters'. I am working with data from the UK's National Travel Survey wave 6 dataset.

    Basically, I have to find those commuters that have travelled over 90 minutes (in the table that is shown as 9 consecutive primary activities (pri) listed as 'travelling'). I have come accross some issues that I do not understnad how to solve.
    1. Respondents (mainid) may have two dirary orders (diaryord), and I want to close this down to focus on only one of their responses
    2. I am trying to find those candidates that have travelled for 9 consecutive periods but I am finding in understanding how to find these individuals
    The time variable seems to be tricky as they have listed each time period (pri = primary activity) as its each individual variables.

    - The value label I am interested in are from 111 to 116. [The ones listed as Travelling]

    - Each time unit is its own variable (e.g. pri1, pri2, pri3)

    - Is there a way that I could find those individuals that have value label ranging from 111 to 116 for 9+ consecutive pri (e.g. pri1 to pri9; or pri112 to pri 121)

    Any help in understanding this would be much appreciated. Thanks!

  • #2
    This is a run problem. You did not provide example data. From your description of the problem I have guessed what your data might look like. So here is some code that first creates a demonstration data set that, I hope, resembles your data in relevant respects, and then solves your problem:

    Code:
    //  CREATE DEMONSTRATION DATA SET
    clear*
    set seed 1234
    set obs 100
    gen int id = _n
    forvalues i = 1/200 {
        gen pri`i' = runiformint(107, 120)
    }
    
    //  SOLUTION BEGINS HERE
    reshape long pri, i(id) j(seq)
    gen byte travelling = inrange(pri, 111, 116)
    
    by id (seq), sort: gen run = sum(travelling != travelling[_n-1])
    by id run (seq), sort: gen run_length = _N
    by id (run seq): egen byte supercommuter = max((run_length >= 9) & travelling)
    
    keep id seq pri supercommuter
    
    // OPTIONAL RESTORATION OF ORIGINAL LAYOUT
    reshape wide
    The final -reshape wide- command restores the original organization of the data. I show it as optional because, most Stata data management and analysis commands work best (or only) with the long data layout that this code creates at the beginning of the solution. So you may want to retain the long data for the rest of your work with it--that will probably make your life easier. But if this is your final step, or if you know for a fact that the rest of your work with it will rely mostly on the few Stata commands that work best with wide data, go ahead with the final -reshape-.

    Now, if I have guessed wrong about the organization of the data, we will have both wasted our time.

    In the future, to avoid that possibility, show example data when posting. The most helpful way to do that is by using Stata's -dataex- command. If you are running version 16 or later, 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


    • #3
      Oh, and here's another way to do it without having to -reshape- the data. Yes, I did urge you to adopt the long layout in #2, but if there you would end up reverting to wide layout anyway, it will be simpler to do it all in wide, which is possible in this case. With the same demonstration data set, you can do this:

      Code:
      forvalues i = 1/200 {
          gen byte travel`i' = inrange(pri`i', 111, 116)
      }
      egen whole_thing = concat(travel*)
      gen byte supercommuter = !!strpos(whole_thing, 9*"1")
      drop travel* whole_thing
      One word of caution with this approach: if your data already contains variables whose names begin with "travel," then choose some other name for these intermediate travel* variables so that the already existing travel* variables don't get wrongly included in the -egen, concat()- calculation.

      Comment


      • #4
        Cross-posted at https://www.reddit.com/r/stata/comme...urvey_dataset/

        Please note that it's a request here (FAQ Advice) and a rule on Reddit's Stata to tell people about cross-posting. The intent is to avoid duplication of effort and to allow people interested in the question to find out about answers elsewhere.

        Comment

        Working...
        X