Announcement

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

  • create a new var extracting part of string with different length

    Hi,

    How can I create a new var(var2) extracting part of string from var1 with different length?
    The string portion that I want to extract is between "evb_" and "_f" and are in the next table (var2):
    var1 var2
    axb_bk_evb_0300_f_9 0300
    axb_bk_evb_1000_f_5 1000
    axb_bk_evb_4500_f 4500
    axb_bk_evb_1700_f 1700
    axb_bk_evb_0600_f 0600
    axb_bk_evb_1200_f_3 1200
    axb_bk_evb_1100_f 1100
    axb_bk_evb_1300_f_4 1300
    axb_bk_evb_2500_f 2500
    Kind Regards.
    R.
    Last edited by Ruki EC; 20 Mar 2019, 10:22.

  • #2
    Well, for the example you show, the following will work:
    Code:
    gen var3 = substr(var1, 12, 4)
    The fact that the length of var1 is variable doesn't really matter. The simple code shown will work because the part you want to extract is always at the same location in the string (starting at position 12) and always has 4 digits. Now, if your real data are not that simple, the code would have to be modified. In that case, post back with a new data example that shows the full extent of the variability in var1 you are facing.

    Comment

    Working...
    X