Announcement

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

  • Logical Operators

    Hi

    Using Stata 12 I/c.

    I want to use logical operators, AND & OR, with 2 variables which are of different types (string & numeric), how do I do so?

    Thank you

  • #2
    You need to convert the string variable to numeric; check out the destring and encode commands and the real() function. If your string contains all numeric data, the real() function may be preferable as it does not require creating a new variable or modifying the existing variable.

    Comment


    • #3
      your question is not clear - how do you want to use them? e.g., with the auto data set (-sysuse auto-), I can certainly do the following: "count if substr(make,1,2)=="Ch" & mpg>20" and get a sensible answer (4 here)

      Comment


      • #4
        First, note that the logical operators are & (and) and | (or). Second, if you're asking whether Stata can interpret a string as a boolean value (e.g., as in Python), it can't. As an alternative, you can use the missing() function (or the synonym mi()) to mimic this behavior. For example, if y is a numeric variable and s is a string variable, you can write something like
        gen z = [exp] if y & !mi(s)
        which will apply if y is nonzero and s is nonempty.

        Comment

        Working...
        X