Nick Cox
Nick Cox

Reputation: 27

How to recode missing values within a range in Stata

I asked a similar question earlier. I'm attempting to fill in missing values such that observations 0-458 are e 0, 445-832 are 1, and 832-850 are 0.

The following code allowed me to replace missing values in observations 1-160 with 1, with the rest of the observations set to 0.

replace myvar = cond(_n <= 160, 1, 0) if missing(myvar)

How can I interpret this command for what my current purpose?

Upvotes: 0

Views: 943

Answers (1)

Nick Cox
Nick Cox

Reputation: 37183

There is no observation 0. I assume you meant observation 1. Your rules are ambiguous otherwise as you give two rules for 445-458 and two rules for 832.

I will give code for a minimal data example.

clear
set obs 6
gen myvar = .

Assume you want myvar in observations 1/2 to be 0, 3/4 to be 1, 5/6 to be 0.

Method 1

replace myvar = inrange(_n, 3, 4) if missing(myvar)

Method 2

replace myvar = cond(_n <= 2, 0, cond(_n <= 4, 1, 0)) 

Method 3

replace myvar = 0 if missing(myvar) in 1/2
replace myvar = 1 if missing(myvar) in 3/4 
replace myvar = 0 if missing(myvar) in 5/6 

In general, however, replacing in terms of observation numbers is not best technique. It is utterly dependent on sort order. Also, if there are criteria in terms of other variables, they are preferable as making more and better sense in records of reproducible research, to yourself in the future and to colleagues, reviewers and yet others too.

Upvotes: 1

Related Questions