Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.

# re: st: replace missing values of a variable with the median of that variable based on particular criterion

 From "Airey, David C" To "statalist@hsphsun2.harvard.edu" Subject re: st: replace missing values of a variable with the median of that variable based on particular criterion Date Sat, 11 Aug 2012 22:23:45 -0500

```.
Here is my try.

I'm curious, what did you try, and what were your problems?
For me, the difficulty was noting the save option to tabstat
and getting the statistic into a local.

-Dave

clear
input PersonID	Inc	Range
1	100	.
2	.	1
3	200	.
4	.	2
5	232	.
6	500	.
7	150	.
8	340	.
9	.	1
10	55	.
end

tabstat Inc if Inc < 200 & !missing(Inc), s(q) save
matrix mymatrix = r(StatTotal)
local mymedian1 = mymatrix[2,1]
replace Inc = `mymedian1' if Range == 1

tabstat Inc if Inc < 600 & Inc >= 200 & !missing(Inc), s(q) save
matrix mymatrix = r(StatTotal)
local mymedian2 = mymatrix[2,1]
replace Inc = `mymedian2' if Range == 2

list

PersonID   Inc   Range
1.          1   100       .
2.          2   100       1
3.          3   200       .
4.          4   286       2
5.          5   232       .
6.          6   500       .
7.          7   150       .
8.          8   340       .
9.          9   100       1
10.         10    55       .

> Dear all,
>
> I have two variables Inc (labeled as income) and Range on respondent-level.
>
> For each respondent, if Inc<., then Range=.;
> if Inc=., then Range is nonmissing, and provides the range of income for
> that respondent (Range=1 means 0<=Inc<200, Range=2 means 200<=Inc<600 ).
>
> The data structure is like:
>
> PersonID    Inc    Range
> 1              100         .
> 2              .             1
> 3              200         .
> 4              .             2
> 5              232         .
> 6              500         .
> 7              150         .
> 8              340         .
> 9              .             1
> 10            55           .
>
> I want to realize the following procedure to replace the missing values of
> Inc:
> When Inc=. & Range=1, replace the missing value of Inc with the median
> income of those respondents who provide nonmissing values for Inc and 0<=
> Inc<200;
> When Inc=. & Range=2, replace the missing value of Inc with the median
> income of those respondents who provide nonmissing values for Inc and
> 200<=Inc<600.
>
> How to realize this purpose?
>
> Thank you very much for your help.
>
> Best,
> Sharon

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```