Announcement

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

  • A loop problem

    Dear all:

    I have a problem when dealing with my data, for one variable. There is only "1","2", and "0", I want to find the first "1", and then find the next "2", then find the next "1",and so on like the following example:
    from the right to the left
    0 0
    0 0
    1 1
    1 0
    1 0
    1 0
    0 0
    1 0
    2 2
    2 0
    1 1
    1 0

    Thank you!
    Best wishes

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte v1
    0
    0
    1
    1
    1
    1
    0
    1
    2
    2
    1
    1
    end
    
    gen wanted = v1 in 1
    forvalues i = 2/`=_N' {
        replace wanted = 1 if inlist(wanted[_n-1], 0, 2) & v1 == 1 in `i'
        replace wanted = 2 if wanted[_n-1] == 1 & v1 == 2 in `i'
        replace wanted = wanted[_n-1] if missing(wanted) in `i'
    }
    
    gen run = sum(wanted != wanted[_n-1])
    sort run, stable
    by run: replace wanted = 0 if _n > 1
    In the future, when showing data examples, please use the -dataex- command to do so, as I have here. 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


    • #3
      Originally posted by Clyde Schechter View Post
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input byte v1
      0
      0
      1
      1
      1
      1
      0
      1
      2
      2
      1
      1
      end
      
      gen wanted = v1 in 1
      forvalues i = 2/`=_N' {
      replace wanted = 1 if inlist(wanted[_n-1], 0, 2) & v1 == 1 in `i'
      replace wanted = 2 if wanted[_n-1] == 1 & v1 == 2 in `i'
      replace wanted = wanted[_n-1] if missing(wanted) in `i'
      }
      
      gen run = sum(wanted != wanted[_n-1])
      sort run, stable
      by run: replace wanted = 0 if _n > 1
      In the future, when showing data examples, please use the -dataex- command to do so, as I have here. 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.
      Thank you for your help! Clyde. I will use the -dataex- in the future. I really appreciate it.

      Comment

      Working...
      X