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
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
Comment