Announcement

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

  • Selecting rows

    I would like to select specific rows in a dataset but I don't know how I can do it.
    - I have more than one row per person.
    - Rows per persons are ordered by record number
    - Every person has a variable A which can be 0 or 1

    I would like to generate the variable select.
    - Select should be 1 for all records of a person following the record for which the variable A is 1
    - Select should be 0 for the records of a person for which the variable A is 1 and for the previous record numbers of that person
    Person Record number Variable A Select
    1 1 0 0
    1 2 1 0
    1 3 0 1
    2 1 1 0
    2 2 0 1
    2 3 0 1
    2 4 0 1
    3 1 0 0
    3 2 0 0
    3 3 1 0
    3 4 0 1
    3 5 0 1

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(person recordnumber variablea select)
    1 1 0 0
    1 2 1 0
    1 3 0 1
    2 1 1 0
    2 2 0 1
    2 3 0 1
    2 4 0 1
    3 1 0 0
    3 2 0 0
    3 3 1 0
    3 4 0 1
    3 5 0 1
    end
    
    bys person (recordnumber): gen wanted= sum(sum(variablea))>=2
    Res.:

    Code:
    . l, sepby(person)
    
         +------------------------------------------------+
         | person   record~r   variab~a   select   wanted |
         |------------------------------------------------|
      1. |      1          1          0        0        0 |
      2. |      1          2          1        0        0 |
      3. |      1          3          0        1        1 |
         |------------------------------------------------|
      4. |      2          1          1        0        0 |
      5. |      2          2          0        1        1 |
      6. |      2          3          0        1        1 |
      7. |      2          4          0        1        1 |
         |------------------------------------------------|
      8. |      3          1          0        0        0 |
      9. |      3          2          0        0        0 |
     10. |      3          3          1        0        0 |
     11. |      3          4          0        1        1 |
     12. |      3          5          0        1        1 |
         +------------------------------------------------+
    
    .

    Comment


    • #3
      Thank you very much, this was very helpfull.

      Comment

      Working...
      X