Announcement

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

  • Removing a single character from a string

    Hello,

    I am interested in removing the third character from a variable that looks like this:
    id
    011003160514
    011009160515
    011013100002
    011019122486
    011027160517

    so that the variable instead looks like this:
    id
    01003160514
    01009160515
    01013100002
    01019122486
    01027160517

    Any tips are appreciated, thank you.

  • #2
    Code:
    gen shortened_version = substr(original_var, 1, 2) + substr(original_var, 4, .)
    Note: This assumes that the variable is, in fact, a string variable. This, of course, cannot be determined with certainty from your display. So, perhaps we have both wasted our time on this. To avoid guesswork and wasted time, in the future, when showing data examples, please use the -dataex- command to do so. 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.

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

    Comment


    • #3
      This indeed worked, thank you very much Clyde! Noted on the dataex, will use this next time I ask a question.

      Comment

      Working...
      X