Announcement

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

  • Generating a new variable but missing values have been included

    Hi,

    My code is:

    Code:
    generate var_66 = 1 if var_33 >=2
    However it has put a 1 in the new variable where there are missing values in var_33, rather than just where there is a score of 2 or greater in var_33.
    Any advice?
    Thanks

  • #2
    Joe:
    Stata considers -.- as the highest number (hence your condition does not qualify for missing values exclusion).
    A possible fix would be:
    Code:
    replace var_66==.
    generate var_66=1 if var_33 >=2 & var_33!=.
    Kind regards,
    Carlo
    (StataNow 18.5)

    Comment


    • #3
      Perfect thanks for the clarification

      Comment


      • #4
        In Stata {1, 0} or {1, 0, missing} indicators are often more useful than {1, missing} indicators. So typically I would go

        Code:
        generate var_66 = var_33 >= 2 if var_33 <  .
        Notes: < . is shorter than but equivalent to != . if there are no extended missing values. If there are extended missing values, it excludes them too.

        Comment


        • #5
          In logical expressions like x>=2 Stata evaluates the expression to true when x is missing, treating missing values as greater than any nonmissing value, a consequence of the way Stata stores missing values. This differs from arithmetic expressions, where x+2 would be missing if x were missing. Furthermore, you code will generate var_66 as a missing value when var_33<2, which s not a good idea. Either it should be 0/1; or it should be 0/1/missing.

          So what you need is something like the following
          Code:
          generate var_66 = var_33 >=2 & var_33<.
          which will generate a 0 if var33 is <2 or missing, and a 1 otherwise.

          Or perhaps you would prefer to have the missing value propagate to the new value, in which case the following will help
          Code:
          generate var_66 = var_33 >=2 & var_33<.
          replace var_66 = var_33 if missing(var_33)

          Comment

          Working...
          X