Announcement

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

  • How to systematically erase the last character of a variable?

    I am working with 2 data sets that contain information about thousands of schools, and in order to match them I am going to use the school code given by the government. The problem is that in one data set the code has 10 characters each, whereas in the other it consists of 11, I have already checked that the schools are the same, it is just that one extra character, how can I erase that extra character from every school code? I am thinking of using a loop, for example: foreach var of school_code {

    But then I do not know how to tell stata to erase the last character from every school code. Can you help me?

  • #2
    This question is in your case more easily solved by addressing "how to systematically extract the first 10 characters of a variable" which you can do by, for example
    Code:
    rename school school11
    generate str10 school = substr(school11,1,10)
    I preserved the original value of school as school11 because, if your luck is like my experience, the minute you get rid of it, you'll find some reason you need it. Perhaps that character stuck on the end has meaning that hasn't been revealed to you yet? So it's no trouble to keep the original around in case it's needed.

    Comment


    • #3
      Thanks, William!

      Comment

      Working...
      X