Announcement

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

  • #16
    I found a solution for a) and b), except for the parentheses:

    Code:
    gen Instrument=""
    replace first = ustrregexs(0) if ustrregexm(CompanyInstrument, "[\d+\s?\d+?\s?/?\d+?\s?]+%.*")
    
    gen Company = subinstr(CompanyInstrument, first, "", .) 
    
    gen Instrument2 = "" 
    foreach w in Cum. Pref. {
      replace Instrument2 = Instrument2 + "`w' " if strpos(Company, "`w'")
    } 
    
    gen Instrument_final = Instrument + Instrument2

    Comment


    • #17
      Originally posted by Clyde Schechter View Post
      I don't believe your loop does what you say it does, but it also doesn't do what you want. Try this:

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str22 name
      "AAH DEAD"
      "AB AIRLINES DEAD"
      "AB CONSULTING ALIVE"
      "AB DYNAMICS DON'T KNOW"
      "AB ELTN.PRDS."
      "ABACUS GROUP DEAD"
      end
      
      foreach scenario in DEAD ALIVE "DON'T KNOW"{
      local target = reverse(`"`scenario'"')
      local length = length(`"`target'"')
      replace name = reverse(subinstr(reverse(name), "`target' ", "", 1)) ///
      if substr(name, length(name)-`length', `=`length'+1') == `" `scenario'"'
      }
      In the future, please use the -dataex- command to show example data, as I have done here. It greatly simplifies the process of replicating your Stata example in another person's Stata, so that code can be tested on it. If you are running version 15.1, -dataex- is 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.
      Hi @Clyde Schechter , thanks for your attention. I know it's been over many years, just wanna ask a quick question about your code here. Does it only work if the words (DEAD ALIVE "DON'T KNOW") are positioned at the last at the string? It seems not working for words positioning at the begining of the string.

      Comment


      • #18
        I have a variable which is a district name (such as District A, District B, District C,etc). I want to remove "Distrct " and only keep A, B, C, etc.
        When I try above suggestion, it does not work for my case.
        My code is
        replace name = subinstr(name,"District ","",1) if substr(name,1,9)=="District "

        I would appreciate any help on this.

        Comment


        • #19
          The code in #18 looks fine to me, so we need to see a data example please. Possible problems include leading spaces; inconsistencies of case, etc.

          Comment


          • #20
            Thanks so much, Nick. After removing blanks with "trim () ", it works now.

            Comment

            Working...
            X