Announcement

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

  • "find and replace" words in Stata code

    I often find I want to make my generic Stata code suit the specifics of my dataset/output.

    So, in the example below, I want to replace the generic word "outcome" (and all compound words containing the word "outcome") with a more specific word like "colon_cc":

    Code:
    gen outcome1 = 0
    replace outcome1 = 1 if icd >= "C180" & icd <= "C188"
    gen outcome2 = 0
    replace outcome2 = 1 if icd == "C189"
    
    gen outcome_grp = 0
    replace outcome_grp = 1 if outcome1 == 1 | outcome2 == 1
    
    stset exit, fail(outcome1) enter(entry) origin(entry) id(sysno)
    stcox i.exposure i.other_vars
    regsave 1.exposure using "dir\results.dta", ci pval addlabel(description, outcome1) append
    
    stset exit, fail(outcome2) enter(entry) origin(entry) id(sysno)
    stcox i.exposure i.other_vars
    regsave 1.exposure using "dir\results.dta", ci pval addlabel(description, outcome2) append
    
    stset exit, fail(outcome_grp) enter(entry) origin(entry) id(sysno)
    stcox i.exposure i.other_vars
    regsave 1.exposure using "dir\results.dta", ci pval addlabel(description, outcome_grp) append
    ...and so on.

    Currently I use the "find and replace" function in MS Word to do this! (I'm sure many Stata users would regard this as a sin, but I'm still very much learning.)

    Any advice would be greatly appreciated!

    With regards

  • #2
    If you are using Stata DO file, then why not use the "find and replace" Short-cut Ctr+H
    Regards
    --------------------------------------------------
    Attaullah Shah, PhD.
    Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
    FinTechProfessor.com
    https://asdocx.com
    Check out my asdoc program, which sends outputs to MS Word.
    For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

    Comment


    • #3
      Thank you so much for your reply.

      I was hoping there might be a more elegant solution that would preserve the generic code, perhaps a local macro at the start to tell Stata that whenever it comes across the word "outcome" to treat it as "colon_cc"?

      Again, many thanks for your offered suggestion.

      With regards

      Comment


      • #4
        If you change all instances of

        Code:
        outcome
        to

        Code:
        `outcome'
        and write before any of those

        Code:
        local outcome outcome
        then next time around you can change that last line to

        Code:
        local outcome colon_cc
        and you have what you want.

        Otherwise I want to point out that

        Code:
        gen outcome1 = 0
        replace outcome1 = 1 if icd >= "C180" & icd <= "C188"
        
        gen outcome2 = 0
        replace outcome2 = 1 if icd == "C189"  
        
        gen outcome_grp = 0
        replace outcome_grp = 1 if outcome1 == 1 | outcome2 == 1
        can be cut down to

        Code:
        gen outcome1 = icd >= "C180" & icd <= "C188"
        gen outcome2 = icd == "C189"
        gen outcome_grp = outcome1 == 1 | outcome2 == 1
        or even

        Code:
        gen outcome1 = inrange(icd, "C180", "C188")
        gen outcome2 = icd == "C189"
        gen outcome_grp = outcome1 | outcome2
        following principles explained at https://www.stata.com/support/faqs/d...true-and-false


        Comment


        • #5
          That's marvellous, thanks so much Nick.
          And for your other suggestions too - my coding is horribly inefficient and these tips literally transform my life working with Stata!

          Comment


          • #6
            Thanks again, Nick, for your very helpful earlier response.

            It's very useful to be able to use

            Code:
            local outcome colon_cc
            to create variables such as `outcome'_grp and `outcome'_grp_broad, without having to change my lengthy generic code.

            I was wondering if it's possible to do a similar thing but with global macros?

            The trouble is that

            Code:
            global outcome colon_cc
            is not compatible with $outcome_grp (because, unlike the local macro, the global macro doesn't know to stop in the middle of the word).

            I hope that makes sense.

            Kind regards

            Comment


            • #7
              Documented problem and solution. See 18.3.10 in https://www.stata.com/manuals/u18.pdf

              Code:
              . global outcome "frog"
              
              . di "|$outcome_group|"
              ||
              
              . di "|${outcome}_group|"
              |frog_group|

              Comment


              • #8
                perfect, thanks

                Comment

                Working...
                X