I have a dataset with five variables (r11livsib to r15livsib) that record the number of living siblings reported by each participant across five waves of data collection, along with an ID variable (hhidpn). I aim to calculate the number of siblings who passed away for each participant between consecutive waves.
However, there are two challenges:
I have some code I wrote attached below as well.
input long hhidpn byte(r11livsib r12livsib r13livsib r14livsib r15livsib)
74379010 . . . . .
203464020 . . . . .
204640010 . . . . .
79613010 4 . . 4 .
910474010 . . 7 . 7
55388010 . . . . .
206025010 . . . . .
502305020 . . . . .
212090010 . . . . .
201875010 . . . . .
201152010 . . . . .
82990020 . . . . .
206281010 . . . . .
57586030 . . . . .
201200010 . . . . .
200516020 . . . . .
202811020 . . . . .
540244010 . . 4 . 8
211913010 . . . . .
202212010 . . . . .
116697020 . . . . .
60076020 . . . . .
47679030 . . . . .
200792010 . . . . .
200994020 . . . . .
206976020 . . . . .
11863010 . . . . .
200861010 . . . . .
20918020 . 11 . . .
58401010 . . . . .
79537040 . . . . .
912349011 . . . . 3
200183020 . . . . .
207059010 . . . . .
502138020 . . . . .
41043010 . . . . .
56660010 . . . . .
205771020 . . . . .
210031020 . . . . .
543927020 . . . . .
501016010 . . . . .
45943010 . . . . .
41901010 1 . . . .
208830010 . . . . .
21225010 . . . . .
202382010 . . . . .
45781010 . . . . .
18157010 . . . . .
202393010 . . . . .
210123010 . . . . .
206569010 . . . . .
211374010 . . . . .
203044020 . . . . .
213253020 . . . . .
205150010 . . . . .
203639010 . . . . .
87483010 . . . . .
48604020 . . . . .
206080010 . . . . .
500086020 . 5 . . .
174300010 . . . . .
202424010 . . . . .
203214010 . . . . .
61107030 . . . . .
208259010 . . . . .
201612020 . . . . .
86023020 . . . . .
37025010 . . . . .
55831020 . . . . .
43284010 . . . . .
205274010 . . . . .
52632010 . . . . .
54794010 . . . . .
204370010 . . . . .
53063040 . . . . .
59300020 10 . . . .
212238010 . . . . .
38739010 2 . . . .
212137020 1 . . . .
544928010 . . 3 . 3
45759020 . . . . .
204567010 . . . . .
207233010 . . . . .
22731010 5 . . . .
500651020 3 . . . .
57210010 . . . . .
200527010 . 1 . . .
11787040 . . . . .
202563020 . . . . .
204060020 1 . . . .
203704020 . . . . .
205914020 . . . . .
205014010 . . . . .
11155010 . . . . .
22938010 . . . . .
211447010 . . . . .
84778010 . . . . .
78493040 . . . . .
210535010 . . . . .
212805010 . . . . .
end
[/CODE]
local siblings r11livsib r12livsib r13livsib r14livsib r15livsib
local i = 1
foreach v of local siblings{
recode `v' (.m=.) (.d=.) (.r=.)
}
forvalue i = 1/4{
bys hhidpn: gen sibling_death_wave`i' =.
}
bys hhidpn: replace sibling_death_wave1 = -999 if (r11livsib == . | r12livsib == .)
bys hhidpn :replace sibling_death_wave1 = -888 if (r12livsib > r11livsib) & (!missing(r12livsib) & !missing(r11livsib))
bys hhidpn: replace sibling_death_wave1 = (r11livsib - r12livsib) if (sibling_death_wave1 != -888 ) & (sibling_death_wave1 != -999)
**** Sibling deaths wave2*******
bys hhidpn: replace sibling_death_wave2 = -999 if (r12livsib == . | r13livsib == .)
bys hhidpn :replace sibling_death_wave2 = -888 if (r12livsib > r13livsib) & (!missing(r12livsib) & !missing(r13livsib))
bys hhidpn: replace sibling_death_wave2 = (r12livsib - r13livsib) if (sibling_death_wave2 != -888 ) & (sibling_death_wave2 != -999)
****sibling deaths wave3*****
bys hhidpn: replace sibling_death_wave3 = -999 if (r13livsib == . | r14livsib == .)
bys hhidpn :replace sibling_death_wave3 = -888 if (r14livsib > r13livsib) & (!missing(r13livsib) & !missing(r14livsib))
bys hhidpn: replace sibling_death_wave3 = (r13livsib - r14livsib) if (sibling_death_wave3 != -888 ) & (sibling_death_wave3 != -999)
**** sibling death wave4*****
bys hhidpn: replace sibling_death_wave4 = -999 if (r15livsib == . | r14livsib == .)
bys hhidpn :replace sibling_death_wave4 = -888 if (r15livsib > r14livsib) & (!missing(r14livsib) & !missing(r15livsib))
bys hhidpn: replace sibling_death_wave4 = (r14livsib - r15livsib) if (sibling_death_wave4 != -888 ) & (sibling_death_wave4 != -999)
However, there are two challenges:
- Inconsistent Reporting: Some participants reported an increase in the number of living siblings over time, which is logically inconsistent since the number of siblings can only decrease or remain the same. For example, participant hhidpn = 540244010 reported 4 living siblings in r13livsib but 8 in r15livsib.
- Missing Data: Some participants have missing values for one or more waves.
- For inconsistent entries where the number of living siblings in a later wave exceeds the earlier wave, assign a value of -888 to the calculated number of deceased siblings for that wave.
- For valid entries (where the number of living siblings decreases or stays the same), compute the number of deceased siblings as the difference between the two waves.
- For missing values, assign a code of -999 to indicate missing data.
I have some code I wrote attached below as well.
input long hhidpn byte(r11livsib r12livsib r13livsib r14livsib r15livsib)
74379010 . . . . .
203464020 . . . . .
204640010 . . . . .
79613010 4 . . 4 .
910474010 . . 7 . 7
55388010 . . . . .
206025010 . . . . .
502305020 . . . . .
212090010 . . . . .
201875010 . . . . .
201152010 . . . . .
82990020 . . . . .
206281010 . . . . .
57586030 . . . . .
201200010 . . . . .
200516020 . . . . .
202811020 . . . . .
540244010 . . 4 . 8
211913010 . . . . .
202212010 . . . . .
116697020 . . . . .
60076020 . . . . .
47679030 . . . . .
200792010 . . . . .
200994020 . . . . .
206976020 . . . . .
11863010 . . . . .
200861010 . . . . .
20918020 . 11 . . .
58401010 . . . . .
79537040 . . . . .
912349011 . . . . 3
200183020 . . . . .
207059010 . . . . .
502138020 . . . . .
41043010 . . . . .
56660010 . . . . .
205771020 . . . . .
210031020 . . . . .
543927020 . . . . .
501016010 . . . . .
45943010 . . . . .
41901010 1 . . . .
208830010 . . . . .
21225010 . . . . .
202382010 . . . . .
45781010 . . . . .
18157010 . . . . .
202393010 . . . . .
210123010 . . . . .
206569010 . . . . .
211374010 . . . . .
203044020 . . . . .
213253020 . . . . .
205150010 . . . . .
203639010 . . . . .
87483010 . . . . .
48604020 . . . . .
206080010 . . . . .
500086020 . 5 . . .
174300010 . . . . .
202424010 . . . . .
203214010 . . . . .
61107030 . . . . .
208259010 . . . . .
201612020 . . . . .
86023020 . . . . .
37025010 . . . . .
55831020 . . . . .
43284010 . . . . .
205274010 . . . . .
52632010 . . . . .
54794010 . . . . .
204370010 . . . . .
53063040 . . . . .
59300020 10 . . . .
212238010 . . . . .
38739010 2 . . . .
212137020 1 . . . .
544928010 . . 3 . 3
45759020 . . . . .
204567010 . . . . .
207233010 . . . . .
22731010 5 . . . .
500651020 3 . . . .
57210010 . . . . .
200527010 . 1 . . .
11787040 . . . . .
202563020 . . . . .
204060020 1 . . . .
203704020 . . . . .
205914020 . . . . .
205014010 . . . . .
11155010 . . . . .
22938010 . . . . .
211447010 . . . . .
84778010 . . . . .
78493040 . . . . .
210535010 . . . . .
212805010 . . . . .
end
[/CODE]
local siblings r11livsib r12livsib r13livsib r14livsib r15livsib
local i = 1
foreach v of local siblings{
recode `v' (.m=.) (.d=.) (.r=.)
}
forvalue i = 1/4{
bys hhidpn: gen sibling_death_wave`i' =.
}
bys hhidpn: replace sibling_death_wave1 = -999 if (r11livsib == . | r12livsib == .)
bys hhidpn :replace sibling_death_wave1 = -888 if (r12livsib > r11livsib) & (!missing(r12livsib) & !missing(r11livsib))
bys hhidpn: replace sibling_death_wave1 = (r11livsib - r12livsib) if (sibling_death_wave1 != -888 ) & (sibling_death_wave1 != -999)
**** Sibling deaths wave2*******
bys hhidpn: replace sibling_death_wave2 = -999 if (r12livsib == . | r13livsib == .)
bys hhidpn :replace sibling_death_wave2 = -888 if (r12livsib > r13livsib) & (!missing(r12livsib) & !missing(r13livsib))
bys hhidpn: replace sibling_death_wave2 = (r12livsib - r13livsib) if (sibling_death_wave2 != -888 ) & (sibling_death_wave2 != -999)
****sibling deaths wave3*****
bys hhidpn: replace sibling_death_wave3 = -999 if (r13livsib == . | r14livsib == .)
bys hhidpn :replace sibling_death_wave3 = -888 if (r14livsib > r13livsib) & (!missing(r13livsib) & !missing(r14livsib))
bys hhidpn: replace sibling_death_wave3 = (r13livsib - r14livsib) if (sibling_death_wave3 != -888 ) & (sibling_death_wave3 != -999)
**** sibling death wave4*****
bys hhidpn: replace sibling_death_wave4 = -999 if (r15livsib == . | r14livsib == .)
bys hhidpn :replace sibling_death_wave4 = -888 if (r15livsib > r14livsib) & (!missing(r14livsib) & !missing(r15livsib))
bys hhidpn: replace sibling_death_wave4 = (r14livsib - r15livsib) if (sibling_death_wave4 != -888 ) & (sibling_death_wave4 != -999)
Comment