Announcement

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

  • If condition with value labels

    Is it possible to impose if condition with value labels ?
    for example :
    Code:
    lab define gender 1 "Male" 2 "Female"
    lab values sex gender
    drop if sex == "Male"   // or drop if sex==Male

  • #2
    Code:
    drop if sex == "Male":gender

    Comment


    • #3
      Originally posted by Joseph Coveney View Post
      Code:
      drop if sex == "Male":gender
      Thanks.

      Comment


      • #4
        Why does the colon notation at the end work? Is this syntax unique to labels, or are we leveraging a more general feature of the language? It almost looks like the colon here is dereferencing a value on the gender object using "Male" as a key, but if so this feels idiosyncratic to the way the rest of the language works.

        Comment


        • #5
          Originally posted by Daniel Schaefer View Post
          Why does the colon notation at the end work? Is this syntax unique to labels [...]?
          The syntax is unique to value labels used in expressions. This feature is demonstrated in Higbee (2004).

          I cannot speak to the underlying mechanics but if it is something like a lookup, it does not handle collisions well; only the first matching value is returned:

          Code:
          . label define foolbl 42 "foo" 73 "foo"
          
          . local not_73 = "foo":foolbl
          
          . display `not_73'
          42
          For similar functionality (and same behavior for duplicate labels) in Mata, see st_vlsearch().


          Higbee, K. 2004. Stata tip 14: Using value labels in expressions. The Stata Journal, 4(4), pp. 486--487.

          Comment

          Working...
          X