Announcement

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

  • Syntax error on "or" command

    Dear Madam/Sir,

    I am new to STATA programming. After searching several posts, I make the following command with the error message.

    keep if 5200<=sic<=5999|5000<=sic<=5199|2000<=sic<=3999|10 00<=sic<=1400|
    invalid syntax
    r(198);

    Any advice will be highly appreciated.

    Sincerely,
    Joon

  • #2
    Possibly the embedded space or the trailing | symbol is evoking that message.

    That said, Stata won't read this as you wish. Try

    Code:
    inrange(sic, 5200, 5999) | inrange(sic, 5000, 5199) | inrange(sic, 2000, 3999) | inrange(sic, 1000, 1400)

    To see the basic pitfall, consider

    Code:
    . di 0.1 < 0.2 < 0.3
    0
    Why is that false? Because 0.1 < 0.2 is evaluated as 1 because the expression is true and then (carrying forward the result) 1 < 0.3 is evaluated as 0 because that expression is false. Stata is just parsing and evaluating from left to right and not acting as you would expect from your mathematical education.

    I can't recall much about the SIC classification implied here, not being an economist Is

    Code:
    inrange(sic, 5200, 5999) | inrange(sic, 5000, 5199)
    the same as

    Code:
    inrange(sic, 5000, 5999)
    ?

    Note that "or" (meaning precisely, |) is an operator, not a command.
    Last edited by Nick Cox; 18 Nov 2021, 18:41.

    Comment


    • #3
      Thanks for your prompt reply, Nick. I tried command using "inrange" before, but it did not work.

      keep if inrange (sic,5000,5999)| inrange (sic,2000,3999) | inrange(sic,1000,1400)
      inrange not found

      Thanks
      Joon

      Comment


      • #4
        While in most respects the use of whitespace does not matter in Stata, there are places where it does. Stata does not allow whitespace between the name of a function and the ( that introduces its argument list. So

        Code:
        keep if inrange(sic,5000,5999)| inrange(sic,2000,3999) | inrange(sic,1000,1400)

        Comment


        • #5
          inrange() is a function, not a command. The distinction is sharp in Stata. That may puzzle anyone more used to other software because (1) what in Stata is a command looks similar to functions elsewhere (2) often elsewhere functions and commands are, at least informally, not sharply distinguished.

          As Clyde Schechter explains, inrange() can't be separated from its arguments.

          The error message arises because Stata is guessing that you are referring to a variable or scalar called inrange but as you don't have one it can only report that such a variable or scalar is not found.

          Comment

          Working...
          X