Child marriage median Mozambique [message #30183] |
Wed, 09 October 2024 16:10 |
Simon
Messages: 3 Registered: October 2024
|
Member |
|
|
Hi there,
I wrote some stata code to automatically return medians in complex survey design datasets. Compliments this post: https://userforum.dhsprogram.com/index.php?t=msg&th=1337 &goto=2470&S=08c954804736d97c007deae6e959abed#msg_24 70
Here it is:
tempfile outputlog
log using `outputlog', text replace
tab ageMarried if gender1 == 1 & age > 24 & age < 50 [iweight=wt],m
log close
file open myfile using `outputlog', read
file read myfile line
local cum_sum = 0
local prev_cum_pct = 0
local stopper = 0
while r(eof) == 0 {
if `stopper' == 3 {
}
else {
if regexm("`line'", "[0-9]+") {
local cum_pct = real(word("`line'", -1))
local age_pct = real(word("`line'", 1))
if `cum_pct' < 50 | `prev_cum_pct' == 0 {
local prev_cum_pct = `cum_pct'
local prev_cum_pct1 = `cum_pct'
}
else if `cum_pct' >= 50 {
local stopper = `stopper' + 1
local medianCalc = (`age_pct')+1*(50-`prev_cum_pct')/(`cum_pct'-`prev_cum_pct')
display `medianCalc'
}
}
}
file read myfile line
}
file close myfile
You might have to vary the stopper limit depending on the variable - I couldn't find a nicer way of escaping the while...
Cheers
|
|
|
|