Announcement

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

  • How to group IDs in Stata

    Hello.
    I need to group the cases into two by the IDs using the last character (N or P).
    How do I do this in stata.
    A sample of the study IDs is listed below.
    Thank you.

    [CODE]
    * Example generated by -dataex-. For more info, type help dataex
    clear
    Sty_id
    "ADH001N"
    "ADH001P"
    "ADH002N"
    "ADH002P"

  • #2
    Code:
    gen wanted = regex(Sty_id,"N$")

    Comment


    • #3
      You appear to have edited the dataex output. You should post the exact output as whether the variable Sty_id is a string variable or a numerical variable with value labels is crucial.

      Code:
      gen which= substr(Sty_id, -1, 1)
      sort which
      l, sepby(which)

      Comment


      • #4
        and here is a solution that does not use regular expressions:
        Code:
        gen byte wanted = inlist(substr(Sty_id,-1,1),"N","P")
        note that since you butchered your -dataex- example (please don't do that), this is not tested

        Comment


        • #5
          The codes worked. Thank to everyone for your assistance. I have also noted the point raised by Andrew Musau and Rich Goldstein on edited output. I need your expert advise on how to improve my coding in stata. Thank you.

          Comment

          Working...
          X