Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Replace O by NA

    Hello
    What is wrong with my code please to replace my 0 by something meaning NA ? :
    replace namevariable = NA if namevariable==0
    or
    replace namevariable = "" if namevariable==0
    Thanks a lot for your help

  • #2
    Code:
    replace namevariable = NA if namevariable==0
    The problem with this one is that in this context NA can only be understood as the name of another variable in your data set. You are asking Stata to replace the value in namevariable by the value of NA in the same observation if the present value of namevariable is 0. If there is no variable in your data set called NA, then Stata will complain and halt.

    Code:
    replace namevariable = "" if namevariable==0
    The problem here is that a variable in Stata must be all numeric or all string; Stata is not a spreadsheet. I gather that namevariable was originally created as a numeric variable. So it must remain that way. (Well, there are a couple of commands that can give the illusion of changing it: -destring- and -tostring-, although what they actually do is eliminate the variable and create a new one with the same name.) To mark 0 as a missing value for name variable, you can do either of these:

    Code:
    replace namevariable = . if namevariable == 0
    or
    Code:
    mvdecode namevariable, mv(0)
    In the end, either way namevariable will now show Stata's system missing value (which is displayed as a period (.) in the browser and when listing data) where the zeroes once were. Read -help missing- for more information about missing values in Stata.

    Comment


    • #3
      A few extra notes to add to @Clyde Schechter's helpful explanation.

      Stata attaches no special meaning to NA as a string, and even less is it a distinctive numeric value meaning numeric missing or some such.

      Behind your question may lie awareness of the use of NA in other software. It can be conveniently ambiguous as not available or not applicable.

      If you have numeric values 0 that to you mean NA then you could define a value label NA. That would be dangerous as usually users want to ignore missings in calculations and zeros aren't ignored unless you so specify. An alternative is to map 0 to an extended missing value say .a and attach a value label to that.

      Comment


      • #4
        Thank you both. The notation with the point would be perfect. In my data the 0 are doubtfull.

        Comment

        Working...
        X