Announcement

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

  • binary sequences

    Good evening,
    I have a panel subset about residential changes. In the basic sequence (1=urban, 0 rural) I would need to select those individuals (id) with rural-to-urban moves: at least one “sequence” 0 to 1 (rural to urban) in variable urban. I reshaped the dataset to long format and make a series of if conditions with all moves in urban variable (from1 to 40) but it didn't work well.
    The final variable “sequence 0 to 1” could be useful for analysis as appeared in third column. I would appreciate to know which command/s I should use. Thank you so much.
    id urban sequence 0 to 1
    4287 1
    4287
    4287 1
    4287
    4287 1
    4287 0
    4292 1
    4292 0
    4292 1 1
    5026 0
    5026 0
    5026 0
    5026 0
    5026 0
    5026 1 1
    5026 0

  • #2
    but it didn't work well
    That could mean almost anything. And you don't actually show the code that "didn't work well." So what is anyone supposed to do to help you with that?

    The final variable “sequence 0 to 1” could be useful for analysis as appeared in third column.
    Probably not. Variables that are coded 1/missing value are a trap for the unwary in Stata. You will probably make errors using such a variable. In Stata, yes-no type variables should be coded 1/0, not 1/missing.

    So, here is simple code that will get you a 1/0 coded variable that captures what you want:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int id byte urban
    4287 1
    4287 .
    4287 1
    4287 .
    4287 1
    4287 0
    4292 1
    4292 0
    4292 1
    5026 0
    5026 0
    5026 0
    5026 0
    5026 0
    5026 1
    5026 0
    end
    
    //    PReSERVE THE CURRENT SORT ORDER WITHIN ID
    sort id, stable
    gen long seq = _n
    
    by id (seq), sort: gen rural_to_urban = (urban == 1 & urban[_n-1] == 0)
    by id: egen wanted = max(rural_to_urban)
    In the future, when showing data examples, please use the -dataex- command to do so, as I have done here. If you are running version 15.1 or a fully updated version 14.2, it 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.

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

    Comment


    • #3
      Thank you for your very useful answer. I have reproduced the commands, showing results with dataex. I am using Stata 15.

      sort idnr, stable
      gen long seq = _n

      by idnr (seq), sort: gen rural_to_urban = (urban == 1 & urban[_n-1] == 0)

      br idnr volgnr urban seq rural_to_urban

      by idnr: egen wanted = max(rural_to_urban)


      . dataex idnr urban seq rural_to_urban wanted if idnr==4287 | idnr==4292
      > | idnr==5026

      ----------------------- copy starting from the next line -----------------------
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input long idnr byte urban long seq float(rural_to_urban wanted)
      4287 1 171 0 0
      4287 . 172 0 0
      4287 1 173 0 0
      4287 . 174 0 0
      4287 1 175 0 0
      4287 0 176 0 0
      4292 1 180 0 1
      4292 0 181 0 1
      4292 1 182 1 1
      4292 1 183 0 1
      5026 0 237 0 1
      5026 0 238 0 1
      5026 0 239 0 1
      5026 0 240 0 1
      5026 0 241 0 1
      5026 1 242 1 1
      5026 0 243 0 1
      end
      ------------------ copy up to and including the previous line ------------------

      Listed 17 out of 88313 observations





      Comment

      Working...
      X