Announcement

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

  • keep observations if first 2 character of the string is capital letter

    I have a dataset where the variable x is a string. I need to keep only observations at which the first 2 characters in the variable x is capital letter. Can anyone tell me how to do it ?

  • #2
    Here is one way:

    Code:
    keep if usubstr(x, 1, 2) == ustrupper(usubstr(x, 1, 2))
    The approach assumes that each observation in x has at least two characters and that the third character and any other characters following are irrelevant.

    Comment


    • #3
      Daniel's approach is general, but the following should work if one is dealing with the English alphabet:

      Code:
      keep if regexm(x, "^[A-Z][A-Z]")

      Comment


      • #4
        thank you so much, Andrew and Daniel

        Comment

        Working...
        X