

I have two variabled called "prean" and "pa_kum" that contain missing values. These missing values i would like to replace by 0 (zero). What command can i use to do this?
Thanks in advance for your support. Regards


In reply to this post by Tirthankar Chakravarty
I have one specific and one general comment here.
Specifically, Martin is correct to underline that . is not the only missing value, but the loop in question is easily fixed by
foreach x of varlist prean pa_kurn{
replace `x' = 0 if missing(x)
}
Generally, a minimal search missing points to several resources. One of the very first entries, to [I] missing values, answers this question comprehensively, so it is difficult to see why it was felt necessary to send it to Statalist.
Nick
[hidden email]
Martin Weiss
============
Stata even forgives the missing blank after if! Amazing!
*************
clear*
inp prean pa_kurn
2 3
. 5
. 3
. .
4 5
end
foreach x of varlist prean pa_kurn{
replace `x' = 0 if(`x' == .)
}
l
*************
One problem with this "firstprinciples" approach is that you have to compare explicitly with all missing values, i.e. also the extended ones.
recode can help avoid this problem.
Neil Shephard
=============
foreach x of varlist prean pa_kurn{
replace `x' = 0 if(`x' == .)
}
...is one way to skin this cat.
Martin Weiss
============
clear*
input myvar
1
3
5
.
3
6
7
end
recode myvar (mis = 0)
l
Tirthankar Chakravarty
mvencode
Tobias Friedli
> I have two variabled called "prean" and "pa_kum" that contain missing values.
> These missing values i would like to replace by 0 (zero). What command can i
> use to do this?
*
* 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/


Should be
replace `x' = 0 if missing(`x')
Nick
[hidden email]
Original Message
From: Nick Cox
I have one specific and one general comment here.
Specifically, Martin is correct to underline that . is not the only missing value, but the loop in question is easily fixed by
foreach x of varlist prean pa_kurn{
replace `x' = 0 if missing(x)
}
Generally, a minimal search missing points to several resources. One of the very first entries, to [I] missing values, answers this question comprehensively, so it is difficult to see why it was felt necessary to send it to Statalist.
Nick
[hidden email]
Martin Weiss
============
Stata even forgives the missing blank after if! Amazing!
*************
clear*
inp prean pa_kurn
2 3
. 5
. 3
. .
4 5
end
foreach x of varlist prean pa_kurn{
replace `x' = 0 if(`x' == .)
}
l
*************
One problem with this "firstprinciples" approach is that you have to compare explicitly with all missing values, i.e. also the extended ones.
recode can help avoid this problem.
Neil Shephard
=============
foreach x of varlist prean pa_kurn{
replace `x' = 0 if(`x' == .)
}
...is one way to skin this cat.
Martin Weiss
============
clear*
input myvar
1
3
5
.
3
6
7
end
recode myvar (mis = 0)
l
Tirthankar Chakravarty
mvencode
Tobias Friedli
> I have two variabled called "prean" and "pa_kum" that contain missing values.
> These missing values i would like to replace by 0 (zero). What command can i
> use to do this?
*
* 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/


<>
*************
replace `x' = 0 if (`x' >= .)
*************
might also do the trick.
It is important, in all solutions offered in this thread, to make sure you feed them only numeric variables. Nick`s mi() function does cover strings as well, but the first part of the replace command is then no longer correct.
HTH
Martin
Ursprüngliche Nachricht
Von: [hidden email] [mailto: [hidden email]] Im Auftrag von Nick Cox
Gesendet: Montag, 9. August 2010 17:20
An: [hidden email]
Betreff: RE: st: Replace missing values by 0
Should be
replace `x' = 0 if missing(`x')
Nick
[hidden email]
Original Message
From: Nick Cox
I have one specific and one general comment here.
Specifically, Martin is correct to underline that . is not the only missing value, but the loop in question is easily fixed by
foreach x of varlist prean pa_kurn{
replace `x' = 0 if missing(x)
}
Generally, a minimal search missing points to several resources. One of the very first entries, to [I] missing values, answers this question comprehensively, so it is difficult to see why it was felt necessary to send it to Statalist.
Nick
[hidden email]
Martin Weiss
============
Stata even forgives the missing blank after if! Amazing!
*************
clear*
inp prean pa_kurn
2 3
. 5
. 3
. .
4 5
end
foreach x of varlist prean pa_kurn{
replace `x' = 0 if(`x' == .)
}
l
*************
One problem with this "firstprinciples" approach is that you have to compare explicitly with all missing values, i.e. also the extended ones.
recode can help avoid this problem.
Neil Shephard
=============
foreach x of varlist prean pa_kurn{
replace `x' = 0 if(`x' == .)
}
...is one way to skin this cat.
Martin Weiss
============
clear*
input myvar
1
3
5
.
3
6
7
end
recode myvar (mis = 0)
l
Tirthankar Chakravarty
mvencode
Tobias Friedli
> I have two variabled called "prean" and "pa_kum" that contain missing values.
> These missing values i would like to replace by 0 (zero). What command can i
> use to do this?
*
* 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/*
* 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/


Tony,
Sometimes data, especially count data, are stored in a spreadsheet where a
blank cell indicates a zero value. For example, if the data are
handentered and there are a lot of zero values, it is more efficient not
to enter a zero, and many spreadsheet functions treat the blank cell as a
zero value.
However, if you paste or insheet these data into Stata, the blank cells
are encoded as missing values. In this case it is necessary to convert the
missing values to zeroes.
David

David Radwin
Research Associate
MPR Associates, Inc.
2150 Shattuck Ave., Suite 800
Berkeley, CA 94704
Phone: 5108494942
Fax: 5108490794
www.mprinc.com
> Original Message
> From: [hidden email] [mailto:owner
> [hidden email]] On Behalf Of Lachenbruch, Peter
> Sent: Monday, August 09, 2010 2:59 PM
> To: ' [hidden email]'
> Subject: st: RE: Replace missing values by 0
>
> In all of the discussion of this query, nobody has asked why it is
> important to do this? If you are computing means, the 0 values will
> distort the mean (or median if enough), and if you simply want to ignore
> zeros with "if x~=0") retaining the missing value will do this
> automatically.
>
> I am unsure if I've ever had a reason to convert missing values to a
> numeric value, but have had many times when the 9 for missing (or
> whatever) had to be set to "."
>
> Tony
>
> Peter A. Lachenbruch
> Department of Public Health
> Oregon State University
> Corvallis, OR 97330
> Phone: 5417373832
> FAX: 5417374001
>
>
> Original Message
> From: [hidden email] [mailto:owner
> [hidden email]] On Behalf Of Hobst
> Sent: Monday, August 09, 2010 1:55 AM
> To: [hidden email]
> Subject: st: Replace missing values by 0
>
> I have two variabled called "prean" and "pa_kum" that contain missing
> values.
> These missing values i would like to replace by 0 (zero). What command
can
> i
> use to do this?
>
> Thanks in advance for your support. Regards
> 
> View this message in context:
> http://statalist.1588530.n2.nabble.com/Replacemissingvaluesby0> tp5388239p5388239.html
> Sent from the Statalist mailing list archive at Nabble.com.
*
* 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/


But the original question was about missing values being represented as 0.
Use of a blank cell for 0 allows the confusion of missing and true 0. At any rate, if you want to code missing as 0 that's a problem to my mind.
Tony
Peter A. Lachenbruch
Department of Public Health
Oregon State University
Corvallis, OR 97330
Phone: 5417373832
FAX: 5417374001
Original Message
From: [hidden email] [mailto: [hidden email]] On Behalf Of David Radwin
Sent: Monday, August 09, 2010 3:29 PM
To: [hidden email]
Subject: st: RE: RE: Replace missing values by 0
Tony,
Sometimes data, especially count data, are stored in a spreadsheet where a
blank cell indicates a zero value. For example, if the data are
handentered and there are a lot of zero values, it is more efficient not
to enter a zero, and many spreadsheet functions treat the blank cell as a
zero value.
However, if you paste or insheet these data into Stata, the blank cells
are encoded as missing values. In this case it is necessary to convert the
missing values to zeroes.
David

David Radwin
Research Associate
MPR Associates, Inc.
2150 Shattuck Ave., Suite 800
Berkeley, CA 94704
Phone: 5108494942
Fax: 5108490794
www.mprinc.com
> Original Message
> From: [hidden email] [mailto:owner
> [hidden email]] On Behalf Of Lachenbruch, Peter
> Sent: Monday, August 09, 2010 2:59 PM
> To: ' [hidden email]'
> Subject: st: RE: Replace missing values by 0
>
> In all of the discussion of this query, nobody has asked why it is
> important to do this? If you are computing means, the 0 values will
> distort the mean (or median if enough), and if you simply want to ignore
> zeros with "if x~=0") retaining the missing value will do this
> automatically.
>
> I am unsure if I've ever had a reason to convert missing values to a
> numeric value, but have had many times when the 9 for missing (or
> whatever) had to be set to "."
>
> Tony
>
> Peter A. Lachenbruch
> Department of Public Health
> Oregon State University
> Corvallis, OR 97330
> Phone: 5417373832
> FAX: 5417374001
>
>
> Original Message
> From: [hidden email] [mailto:owner
> [hidden email]] On Behalf Of Hobst
> Sent: Monday, August 09, 2010 1:55 AM
> To: [hidden email]
> Subject: st: Replace missing values by 0
>
> I have two variabled called "prean" and "pa_kum" that contain missing
> values.
> These missing values i would like to replace by 0 (zero). What command
can
> i
> use to do this?
>
> Thanks in advance for your support. Regards
> 
> View this message in context:
> http://statalist.1588530.n2.nabble.com/Replacemissingvaluesby0> tp5388239p5388239.html
> Sent from the Statalist mailing list archive at Nabble.com.
*
* 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/*
* 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/



In many cases, skip patterns in questionnaires leave behind missing values, either 9's or .'s. If you told the interviewer that you didn't do behavior X in the past year, you won't be asked how many times you did X in the past 30 days. The missing value for 30day behavior X really does represent a zero.
Dave
====================================
David C. Bell
Professor of Sociology
Indiana University Purdue University Indianapolis (IUPUI)
(317) 2781336
====================================
On Aug 9, 2010, at 6:29 PM, David Radwin wrote:
> Tony,
>
> Sometimes data, especially count data, are stored in a spreadsheet where a
> blank cell indicates a zero value. For example, if the data are
> handentered and there are a lot of zero values, it is more efficient not
> to enter a zero, and many spreadsheet functions treat the blank cell as a
> zero value.
>
> However, if you paste or insheet these data into Stata, the blank cells
> are encoded as missing values. In this case it is necessary to convert the
> missing values to zeroes.
>
> David
>
> 
> David Radwin
> Research Associate
> MPR Associates, Inc.
> 2150 Shattuck Ave., Suite 800
> Berkeley, CA 94704
> Phone: 5108494942
> Fax: 5108490794
>
> www.mprinc.com
>
>
>> Original Message
>> From: [hidden email] [mailto:owner
>> [hidden email]] On Behalf Of Lachenbruch, Peter
>> Sent: Monday, August 09, 2010 2:59 PM
>> To: ' [hidden email]'
>> Subject: st: RE: Replace missing values by 0
>>
>> In all of the discussion of this query, nobody has asked why it is
>> important to do this? If you are computing means, the 0 values will
>> distort the mean (or median if enough), and if you simply want to ignore
>> zeros with "if x~=0") retaining the missing value will do this
>> automatically.
>>
>> I am unsure if I've ever had a reason to convert missing values to a
>> numeric value, but have had many times when the 9 for missing (or
>> whatever) had to be set to "."
>>
>> Tony
>>
>> Peter A. Lachenbruch
>> Department of Public Health
>> Oregon State University
>> Corvallis, OR 97330
>> Phone: 5417373832
>> FAX: 5417374001
>>
>>
>> Original Message
>> From: [hidden email] [mailto:owner
>> [hidden email]] On Behalf Of Hobst
>> Sent: Monday, August 09, 2010 1:55 AM
>> To: [hidden email]
>> Subject: st: Replace missing values by 0
>>
>> I have two variabled called "prean" and "pa_kum" that contain missing
>> values.
>> These missing values i would like to replace by 0 (zero). What command
> can
>> i
>> use to do this?
>>
>> Thanks in advance for your support. Regards
>> 
>> View this message in context:
>> http://statalist.1588530.n2.nabble.com/Replacemissingvaluesby0>> tp5388239p5388239.html
>> Sent from the Statalist mailing list archive at Nabble.com.
>
> *
> * 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/*
* 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/


be careful  while what you say is true, you also won't be asked if
you were happy doing "x"  and 0 is not correct for that
I definitely agree with Tony that just mindlessly doing what the OP
asked is very dangerous
Rich
On 8/10/10 12:13 PM, David Bell wrote:
> 
> In many cases, skip patterns in questionnaires leave behind missing values, either 9's or .'s. If you told the interviewer that you didn't do behavior X in the past year, you won't be asked how many times you did X in the past 30 days. The missing value for 30day behavior X really does represent a zero.
>
> Dave
> ====================================
> David C. Bell
> Professor of Sociology
> Indiana University Purdue University Indianapolis (IUPUI)
> (317) 2781336
> ====================================
>
>
>
>
> On Aug 9, 2010, at 6:29 PM, David Radwin wrote:
>
>> Tony,
>>
>> Sometimes data, especially count data, are stored in a spreadsheet where a
>> blank cell indicates a zero value. For example, if the data are
>> handentered and there are a lot of zero values, it is more efficient not
>> to enter a zero, and many spreadsheet functions treat the blank cell as a
>> zero value.
>>
>> However, if you paste or insheet these data into Stata, the blank cells
>> are encoded as missing values. In this case it is necessary to convert the
>> missing values to zeroes.
>>
>> David
>>
>> 
>> David Radwin
>> Research Associate
>> MPR Associates, Inc.
>> 2150 Shattuck Ave., Suite 800
>> Berkeley, CA 94704
>> Phone: 5108494942
>> Fax: 5108490794
>>
>> www.mprinc.com
>>
>>
>>> Original Message
>>> From: [hidden email] [mailto:owner
>>> [hidden email]] On Behalf Of Lachenbruch, Peter
>>> Sent: Monday, August 09, 2010 2:59 PM
>>> To: ' [hidden email]'
>>> Subject: st: RE: Replace missing values by 0
>>>
>>> In all of the discussion of this query, nobody has asked why it is
>>> important to do this? If you are computing means, the 0 values will
>>> distort the mean (or median if enough), and if you simply want to ignore
>>> zeros with "if x~=0") retaining the missing value will do this
>>> automatically.
>>>
>>> I am unsure if I've ever had a reason to convert missing values to a
>>> numeric value, but have had many times when the 9 for missing (or
>>> whatever) had to be set to "."
>>>
>>> Tony
>>>
>>> Peter A. Lachenbruch
>>> Department of Public Health
>>> Oregon State University
>>> Corvallis, OR 97330
>>> Phone: 5417373832
>>> FAX: 5417374001
>>>
>>>
>>> Original Message
>>> From: [hidden email] [mailto:owner
>>> [hidden email]] On Behalf Of Hobst
>>> Sent: Monday, August 09, 2010 1:55 AM
>>> To: [hidden email]
>>> Subject: st: Replace missing values by 0
>>>
>>> I have two variabled called "prean" and "pa_kum" that contain missing
>>> values.
>>> These missing values i would like to replace by 0 (zero). What command
>> can
>>> i
>>> use to do this?
>>>
>>> Thanks in advance for your support. Regards
>>> 
>>> View this message in context:
>>> http://statalist.1588530.n2.nabble.com/Replacemissingvaluesby0>>> tp5388239p5388239.html
>>> Sent from the Statalist mailing list archive at Nabble.com.
*
* 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/


I'd put it this way.
If blanks that should be zeros are interpreted by Stata on input as
missings, then it's appropriate to map those missings to zeros before
analysis.
It's easy enough to show that this can happen, e.g. by copy and paste
from a spreadsheet to Stata's data editor.
Clearly, anyone doing this should be certain that it is exactly what
they want. If there are other genuine missings, replacing them by zero
is a very bad idea.
Nick
[hidden email]
Lachenbruch, Peter
But the original question was about missing values being represented as
0.
Use of a blank cell for 0 allows the confusion of missing and true 0.
At any rate, if you want to code missing as 0 that's a problem to my
mind.
*
* 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/

