Announcement

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

  • Drop the last 4 characters of a string

    Hi,

    I would like to remove the last 4 characters of a string. here is the data:


    Code:
    stockidentifier
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    AHD.ASX
    I have tried to use the command "substr" but I could not figure out the right syntax. Thanks!

  • #2
    Code:
    generate str stock_prefix = substr(stockidentifier, 1, strlen(stockidentifier) - 4)

    Comment


    • #3
      Would you be able to explain the syntax? I thought that the second number was the position.

      I have several stock identifiers that have a stock prefix longer than 3 characters. For instance: ABCDED.ASX. Is that still the right syntax?

      Comment


      • #4
        Francois:
        you can start with eliminating what lies after the full stop:
        Code:
        set obs 1
        g stockidentifier="AHD.ASX"
        split stockidentifier , p(.)
        drop stockidentifier2
        Kind regards,
        Carlo
        (StataNow 18.5)

        Comment


        • #5
          Code:
          substr()
          is a function (not a command!). The distinction is important because commands and functions are quite distinct in Stata (whether that is so for other software is a different question). Thus functions have different syntax and are documented separately.

          It seems that you want to eliminate the suffix such as ".ASX".

          Joseph's method is fine so long as the suffix has that length.

          Assuming that the suffix starts with a stop (period) and there is no such character earlier, then another way to do it (the same as that used inside split) is

          Code:
          gen id2 = substr(id, 1, strpos(id, ".") - 1) 
          Remember that help on any function is directly obtainable by a call such as

          Code:
          help substr()
          That is, spelling out parentheses flags to Stata that you are interested in the function named.
          Last edited by Nick Cox; 01 Sep 2017, 02:50.

          Comment


          • #6
            Originally posted by Francois Durant View Post
            Would you be able to explain the syntax? I thought that the second number was the position.
            It is.

            You want the remove the last four characters. To do so, you calculate the position of the last character (using the strlen() function) and then subtract four from it.

            Originally posted by Francois Durant View Post
            I have several stock identifiers that have a stock prefix longer than 3 characters. For instance: ABCDED.ASX. Is that still the right syntax?
            Yes. It doesn't matter how long the prefix is—if you want to remove the last four characters, then it is the still the right syntax.

            Comment

            Working...
            X