Announcement

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

  • String variable- code group certain cases- regexm(); regexr(); regexs()???

    Dear Statalist,

    I would like to be able to select only those cases in "subp" variable whose value ends with "-1" at the end. Because the variable "subp" is a string variable, I could not find an efficient code. I would really appreciate if you could help me with a suitable code to solve this problem. Thanks. Ramu

    . tab subp

    subp | Freq. Percent Cum.
    ------------+-----------------------------------
    15-0394-1 | 1 0.91 0.91
    15-0394-2 | 1 0.91 1.82
    15-0394-3 | 1 0.91 2.73
    15-0453-1 | 1 0.91 3.64
    15-0453-2 | 1 0.91 4.55

  • #2
    For this particular selection problem you do not need the regular expression functions. You can just do this:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str19 subp
    "15-0394-1"
    "15-0394-2"
    "15-0394-3"
    "15-0453-1"
    "15-0453-2"
    end
    
    gen select = (substr(subp, -2, 2) == "-1")
    
    list, noobs clean
    In the future, when showing data examples, please use the -dataex- command to do so, as I have in this reponse. If you are running version 15.1 or a fully updated version 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.



    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #3
      Thank you so much. That's exactly what I needed. Will follow your advice for future posting.

      Comment

      Working...
      X