Announcement

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

  • define a dummy with cond()

    Dear All, Suppose the data set is
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float n int rep78
     1 3
     2 3
     3 .
     4 3
     5 4
     6 3
     7 .
     8 3
     9 3
    10 3
    11 3
    12 2
    13 3
    14 3
    15 4
    16 3
    17 2
    18 2
    19 3
    20 5
    end
    My command is
    Code:
    gen d = cond(rep78>2,1,0)
    However, I need `d' to be missing for the 3rd and 7th observations. I wonder the command `cond()' can do this?
    Ho-Chuan (River) Huang
    Stata 19.0, MP(4)

  • #2
    I am fond of cond() but the code you cite has no clear advantage over

    Code:
    gen d = rep78 > 2
    but either way you can go

    Code:
    gen d = rep78 > 2 if rep78 < .
    or

    Code:
    gen d = cond(rep78 > 2, 1, 0) if rep78 < .
    or

    Code:
    gen d = cond(missing(rep78), ., rep78 > 2)
    or yet other ways should now occur to you.

    See also (for example)

    https://www.stata.com/support/faqs/d...rue-and-false/

    https://www.stata-journal.com/articl...article=dm0099

    Comment


    • #3
      Dear Nick, Thanks a lot.
      Ho-Chuan (River) Huang
      Stata 19.0, MP(4)

      Comment

      Working...
      X