Announcement

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

  • label value?

    Dear All, Is there any other way (say, one line command) to obtain `a' as below.
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str4 y
    "Y"
    "N"
    end
    
    label define a 1 "Y" 0 "N"
    encode y, gen(a)
    Thanks.
    Ho-Chuan (River) Huang
    Stata 19.0, MP(4)

  • #2
    Maybe
    Code:
    generate byte a = y == "Y" if !missing(y)
    or
    Code:
    generate byte a = cond(y == "Y", 1, cond(y == "N", 0, .)
    (They are not equivalent, and I wouldn't use either, but rather the following.)
    Code:
    label define NY 0 N 1 Y
    encode y, generate(a) label(NY) noextend

    Comment


    • #3
      There is also -encoder-, by Dan Klein, available from SSC.

      Code:
      encoder y, gen(a) setzero
      The -setzero- option causes Stata to create a label with zero as the base value.

      Comment


      • #4
        Dear @Joseph Coveney, and @Clyde Schechter, Thanks for these useful suggestions.
        Ho-Chuan (River) Huang
        Stata 19.0, MP(4)

        Comment


        • #5
          encoder is actually by David Tannenbaum.

          Comment


          • #6
            Yes, sorry for the misattribution. Daniel is right; -encoder- is by David Tannenbaum. I was confusing it with -elabel-, which really is by Daniel Klein.

            Comment

            Working...
            X