Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: merge: replace values with missings


From   daniel klein <[email protected]>
To   [email protected]
Subject   Re: st: merge: replace values with missings
Date   Fri, 23 Mar 2012 10:20:39 +0100

Marc,

I do not see a way to get -merge- to do this, but I would not worry
too much about "detecting all cases" when doing this "by hand". Here
are two ways I can think of. Both seem admittedly rather clumsy, so
hopefully someone will come up with a smarter solution.

You could for example create new variables in your using dataset,
indicating missing values.

u <using_datatset> ,clear
unab vars : *
loc vars : subinstr loc vars "<id>" ""
foreach v of loc vars {
	qui g byte _miss`v' = mi(`v')
}
mer 1:1 id using master
foreach v of loc vars {
	qui replace `v' = . if _miss`v'
}
drop _miss*

You might run into limits of maximum variables allowed here if your
dataset is large. Therefore another way could be to replace missing
values in the using dataset with numerical values that are known not
to be valid values.

u <using_datatset> ,clear
foreach v of var * {
	qui replace `v' = -42 if mi(`v')
}
mer 1:1 id using master
foreach v of var * {
	qui replace `v' = . if (`v' == -42)
}

Note that neither code handles string variables. If you have string
variables you will have to type a few more lines.

Best
Daniel
*
*   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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index