Announcement

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

  • Find and Replace Feature in Do Files

    Dear All,

    Is there a way where the find & replace function can be made to be case-sensitive?

    E.g. searching for "dia" (variable name in a repeated measures analysis) so that it would flag dia0, dia1, dia2, etc.

    But then "Diastolic" (as used in graph titles) is also highlighted and if I press on "replace all" it would change the first part of that word, whereas I would like for it to be left as is.

    Hope this is clear.

    I use Stata 18 for Mac.

    Thanks

  • #2
    A do-file is simply a text file. So you may import it using import delimited and use standard string functions to edit it.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str200 command
    "summarize Diastolic"              
    "regress dia44 var1 var2 var3 dia7"
    "mean diabetes"                    
    "drop dia2"                        
    "*list of diagnostics"            
    "keep dia5 var4 var5"              
    "*find matches of DIAPHRAGM"      
    end
    
    *REPLACE dia? (CASE-SENSITIVE) WITH "sys?"
    gen wanted= trim(itrim(ustrregexra(" " +command+ " ","\bdia(\d+)\b", "sys$1")))
    Res.:

    Code:
    l, sep(0)
    
         +-----------------------------------------------------------------------+
         |                           command                              wanted |
         |-----------------------------------------------------------------------|
      1. |               summarize Diastolic                 summarize Diastolic |
      2. | regress dia44 var1 var2 var3 dia7   regress sys44 var1 var2 var3 sys7 |
      3. |                     mean diabetes                       mean diabetes |
      4. |                         drop dia2                           drop sys2 |
      5. |              *list of diagnostics                *list of diagnostics |
      6. |               keep dia5 var4 var5                 keep sys5 var4 var5 |
      7. |        *find matches of DIAPHRAGM          *find matches of DIAPHRAGM |
         +-----------------------------------------------------------------------+
    
    .
    So you want something like:

    Code:
    import delimited mydofile.do
    egen command= concat(v*)
    replace command= trim(itrim(ustrregexra(" " +command+ " ","\bdia(\d+)\b", "sys$1")))
    contract command, nomiss
    list, sep(0)
    Last edited by Andrew Musau; 31 Jul 2023, 06:51.

    Comment


    • #3
      From the do-file editor, if you to Edit > Find > Replace, you will see an option to "match case" which means the search will be case-sensitive.

      Comment


      • #4
        Dear Andrew and Leonardo,

        Thank you both for your replies. This will help tremendously.
        Much appreciated

        Comment

        Working...
        X