Announcement

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

  • Removing 0s from string

    Hello everyone, I'm currently working with a dataset which uses a code to identify specific companies. The codes contain 6 digits each, but some of them have two 0s as the first two digits (ex. 001005). I'd need to drop the first two zeros, but I can't find a way to do it without removing the first two digits from all the codes. Could someone help me on this one ? Thank you in advance for your help!

  • #2
    Code:
    replace var = substr(var, 2, .) if substr(var, 1, 2) == "00"
    Note: code is untested because no example data was provided. Beware of typos or other errors. In the future, when asking for help with code, to maximize the likelihood that respondents will provide code that actually works in your data, you should include example data in your post. The way to do that is with the -dataex- command. If you are running version 17, 16 or a fully updated version 15.1 or 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.

    Comment


    • #3
      Hi Clyde, thank you for your answer! I ran the code, but it appears it only deletes the first zero from the code, not both (001005 became 01005, not 1005) and I don't understand why. Thank you again in advance for your time and consideration, can't thank you enough !

      Comment


      • #4
        I think Clyde meant

        Code:
        replace var = substr(var, 3, .) if substr(var, 1, 2) == "00"

        Comment


        • #5
          Yes, Andrew Musau is correct.

          Comment


          • #6
            Thank you Andrew and Clyde, appreciate the help !!!

            Comment

            Working...
            X