Announcement

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

  • Search Command for Alphanumeric Characters

    Greetings,

    I use Stata 18. I have a big dataset (30+K observations) and one of my variables (Unit Index) has more than 100 categories (alphanumeric strings). I want to write a command that generates a new variable and enters 1 for all observations, which unit index starts with the character "J" followed by numbers.
    I tried the following codes, but with no success. I appreciate any advice/help, please.

    generate newvar = 0
    replace newvar = 1 if strpos (UnitInd, "J[0-9]")

    OR

    gen newvar = ustrregexs (0) if ustrregexs(UnitInd, "J[0-9]")

    Thanks,
    Eliot

    Last edited by Eliot Assoudeh; 29 Apr 2024, 06:40.

  • #2
    Hi Elliot,

    try

    Code:
    gen newvar = 1 if regexm(UnitInd,"^J")
    All the best,
    Benno

    Comment


    • #3
      Hi Benno,

      Thanks! It is very helpful but the command also captured any code that starts with "JK" as well. I am only interested in capturing the codes that start with "J" and are followed by numbers. Any suggestions?

      Thx!

      Comment


      • #4
        Got it!

        gen newvar = 1 if regexm(UnitInd,"^J[0-9]")

        Thank you so much Benno!

        Comment

        Working...
        X