Announcement

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

  • Creating variables recording position of all occurrences of a number (2) in string sequence of 1s and 2s

    I have a string variable ‘pgrid’ that contains a series of 1s and 2s. This is a variable I have derived to summarise whether each observation is recorded as being a parent of someone else in the household based on other variables (not included here) describing a fuller set of household relationships.

    pgrid is constructed so that the position of each 1 or 2 in the string is meaningful - it indicates the relevant pid of the other person in the household. So for observation 6 pgrid indicates that person has a parent relationship with someone at position 3 in the household. Some observations have no 2s, some have many (up to 7).

    I want to create new variables that record the position of each occurrence of the 2 in the string and is missing otherwise. e.g. kidpid1, kidpid2

    I’ve used strpos to do this for the first occurrence e.g. gen kidpnum1=strpos(pgrid,”2”). But some observations have many 2s in the string sequence (up to 7 occurences of 2), some have none.

    Could a loop be created to record each position of 2 or is there another way?

    Thank you for any suggestions

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str10 pid str2 pnum byte dvhsize str10 pgrid
    "210109031" "1" 2 "1111111111"
    "210109032" "2" 2 "1111111111"
    "330109041" "1" 2 "1111111111"
    "330109042" "2" 2 "1111111111"
    "550109061" "1" 3 "1121111111"
    "550109062" "2" 3 "1121111111"
    "550109063" "3" 3 "1111111111"
    "174109071" "1" 3 "1121111111"
    "174109072" "2" 3 "1121111111"
    "174109073" "3" 3 "1111111111"
    "823109081" "1" 1 "1111111111"
    "333109111" "1" 2 "1111111111"
    "333109112" "2" 2 "1111111111"
    "113109121" "1" 2 "1211111111"
    "113109122" "2" 2 "1111111111"
    end
    label values dvhsize DVHsize

  • #2
    Nice question.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str10 pid str2 pnum byte dvhsize str10 pgrid
    "210109031" "1" 2 "1111111111"
    "210109032" "2" 2 "1111111111"
    "330109041" "1" 2 "1111111111"
    "330109042" "2" 2 "1111111111"
    "550109061" "1" 3 "1121111111"
    "550109062" "2" 3 "1121111111"
    "550109063" "3" 3 "1111111111"
    "174109071" "1" 3 "1121111111"
    "174109072" "2" 3 "1121111111"
    "174109073" "3" 3 "1111111111"
    "823109081" "1" 1 "1111111111"
    "333109111" "1" 2 "1111111111"
    "333109112" "2" 2 "1111111111"
    "113109121" "1" 2 "1211111111"
    "113109122" "2" 2 "1111111111"
    end
    label values dvhsize DVHsize
    
    clonevar work=pgrid 
    
    forval j = 1/7 {
        gen kidpid`j' = cond(strpos(work, "2"), strpos(work, "2"), .) 
        replace work = subinstr(work, "2", "9", 1)
    }
    
    list pgrid kidpid* 
    
         +----------------------------------------------------------------------------------+
         |      pgrid   kidpid1   kidpid2   kidpid3   kidpid4   kidpid5   kidpid6   kidpid7 |
         |----------------------------------------------------------------------------------|
      1. | 1111111111         .         .         .         .         .         .         . |
      2. | 1111111111         .         .         .         .         .         .         . |
      3. | 1111111111         .         .         .         .         .         .         . |
      4. | 1111111111         .         .         .         .         .         .         . |
      5. | 1121111111         3         .         .         .         .         .         . |
         |----------------------------------------------------------------------------------|
      6. | 1121111111         3         .         .         .         .         .         . |
      7. | 1111111111         .         .         .         .         .         .         . |
      8. | 1121111111         3         .         .         .         .         .         . |
      9. | 1121111111         3         .         .         .         .         .         . |
     10. | 1111111111         .         .         .         .         .         .         . |
         |----------------------------------------------------------------------------------|
     11. | 1111111111         .         .         .         .         .         .         . |
     12. | 1111111111         .         .         .         .         .         .         . |
     13. | 1111111111         .         .         .         .         .         .         . |
     14. | 1211111111         2         .         .         .         .         .         . |
     15. | 1111111111         .         .         .         .         .         .         . |
         +---------------------------------------------------------------------------------

    Comment


    • #3
      That's perfect - thank you

      Comment

      Working...
      X