Announcement

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

  • Splitting up gen OK=1 if ... into several expressions

    Hi,
    I try to run the following command
    gen OK=1 if strpos( name, "one") >0 | strpos( name, "two") >0 | strpos( name, "three") >0 | .... | strpos( name, "fourhundred") >0 | strpos( name, "fourhundredone") >0
    The problem is that I get the error message "too many literals" and I wonder how I can shorten & split up this command.

    The goal is to keep only those with a OK value (I believe one could formulate this somehow as 'keep if strpos( name, "one") >0 ....' too but that's the same problem.

    If there's a similar thread, I apologies.

  • #2
    This suggests that there is a limit to how many literals you can specify. You can break it down into several commands

    Code:
    gen OK=...
    replace OK =1 if ... & OK!=1
    Alternatively, I would switch to regular expressions. The syntax is much shorter and it may not have length restrictions.

    Code:
    gen OK= regexm(name, "(one|two|three|fourhundred)")

    Comment


    • #3
      Thank you very much Andrew! I think it should work. Strangely, if I enter my long regular expression I get a "regexp: unterminated ()" error, which seems to be related to length too (see this statalist entry)

      Using the individual commands worked. Thanks so much!

      Comment

      Working...
      X