Announcement

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

  • Dropping observations within a variable if they are not numeric

    How do I drop observations within a variable if they are not numeric?

  • #2
    The answer depends very much on what your data are like to begin with, and you have not shown us sample data as recommended in the Statalist FAQ linked to from the top of the page.

    Even the best descriptions of data are no substitute for an actual example of the data. There are many ways your data might be organized that are consistent with what little you have told us. In order to get a helpful response, you need to show some example data.

    Be sure to use the dataex command to do this. If you are running version 15.1 or later, 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 and 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 William for your kind response.

      I apologise for giving little to no details.

      My data looks something like this:

      EstadísticadelPadrónContinuo

      43.- Tarragona
      Población por sexo, Sección y relación lugar de nacimiento y residencia.
      Unidades: Personas

      4300101001
      4300201001
      4300301001
      4300401001
      4300401002
      4300401003
      4300401004
      4300402001
      4300402002



      The variable mentioned in Bold is the one variable I want to merge my other datsets with. The problem is that the column starts out my mentioning other things in alphabets. Whereas I'm only interested in the numbers mentioned here. Therefore, I would like to delete these alphabets.



      Comment


      • #4
        A starting point using regex()



        Code:
        clear
        
        
        input str39 v1
        "EstadísticadelPadrónContinuo" 
        "43.- Tarragona"
        "Población por sexo, Sección y relación lugar de nacimiento y residencia."
        "Unidades: Personas"
        "4300101001"
        "4300201001"
        "4300301001"
        "4300401001"
        "4300401002"
        "4300401003"
        "4300401004"
        "4300402001"
        "4300402002"
        end
        
        
        
        cap drop if regexm(v1, "[A-z]")
        
        list
        
        *or*
        
        cap keep if   regexm(v1, "^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]")
        
        list
        Eric A. Booth | Senior Director of Research | Far Harbor | Austin TX

        Comment

        Working...
        X