Announcement

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

  • Is it possible to destring a variable which starts with a letter?

    Hello, I am using Parliamentary Constituency codes in my dataset which begin with either E, S, N, W dependent on if the constituency is in England, Scotland, Northern Ireland or Wales respectively. The variable type is a string. I have made several attempts to destring the variable in the usual way however have been unsuccessful. I suspect this is because of the letter at the start of each observation? I was wondering whether there are possibly any suggestions as to overcome this? Many thanks.
    Last edited by Ellie Daglish; 22 Feb 2019, 12:55.

  • #2
    Could you provide some examples of such variable?

    Comment


    • #3
      Here are some examples of the variables I am using:

      E14000576
      E14000876
      S14000029
      W07000687
      W07000087
      N06000045
      N06000009

      I want to drop all observations where the code begins with N however when I have attempted to do so it provides an error relating to the fact that they are in string format. Hence, my #1 question where I attempted to destring them so that I can drop these variables. If you have any suggestions of an alternative way to do this they would be greatly appreciated. Many thanks.

      Comment


      • #4
        Code:
        clear
        input str9 code
        "E14000576"
        "E14000876"
        "S14000029"
        "W07000687"
        "W07000087"
        "N06000045"
        "N06000009"
        end
        
        drop if substr(code, 1, 1) == "N"
        Please, next time use -dataex- to post your example (see FAQ #12).

        Comment


        • #5
          I would go
          Code:
          egen countryc=substr(code,1,1)
          drop if countryc == "N"
          HTH

          Comment


          • #6
            Thank you for your help, that is great.

            Comment


            • #7
              I guess people worked this out but in #5

              Code:
               
               egen countryc=substr(code,1,1)
              is a typo for

              Code:
               
               gen countryc=substr(code,1,1)
              Indeed, as Andrea Discacciati pointed out in #4 you don't need to create a new variable at all, as

              Code:
              drop if substr(code, 1, 1) == "N" 
              gets you there directly.

              Comment

              Working...
              X