Announcement

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

  • Dropping mixed characters from the start of a string

    My study has variable trial_id which has data entered in two different ways:

    trial_id
    1.1
    1.2
    1.11
    1.12
    BH013
    BH014
    BH015

    I want to get rid if the '1.' and the 'BH0' (BH0, not BHO), so that I have:

    study_id
    1
    2
    11
    12
    13
    14
    15

    I've been checking out various ways of removing parts of strings but none quite seem to fit with the '1.' or the 'BH0". I would greatly appreciate advice on how to do this.

  • #2
    Code:
    gen study_id = ""
    replace study_id = substr(trial_id,3,.) if substr(trial_id,1,2) == "1."
    replace study_id = substr(trial_id,4,.) if substr(trial_id,1,3) == "BH0"
    or
    Code:
    gen study_id = regexr(trial_id,"^(1.|BH0)","")

    Comment


    • #3
      Code:
      gen study_id = real(regexs(0)) if regexm(trial_id, "[0-9]+$")

      Comment

      Working...
      X