Announcement

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

  • how to simplify when 2 do files have a lot of same contents?

    My 1st do file is (a much shorter version):
    Code:
    clear *
    use "/Users/zhaoyao/Documents/wiki/daily_contribution.dta" 
    cd "/Users/zhaoyao/Documents/wiki"
    rename *, lower
    
    *same codes as 2nd .do file
    
    local name "Panel A. Contributions from all contributors:"
    matrix rownames table1 = "`name'Addition" "`name'Deletion" "`name'Total"
    matrix colnames table1 = "Mean" "Standard Error" "Mean" "Standard Error" "t-stats" 
                         
    estout matrix(table1, fmt(2))
    #delimit ;
    estout matrix(table1, fmt(2)) using table1_panelA.tex, 
    style(tex) 
    replace
    mlabels(,none) collabels(,none)
    ;
    My 2nd file has the same beginning (.dta file) as 1st .do file. But it has more codes before the "same codes as 2nd f.do ile". And it has a little bit different at the end of 1st do file.

    Code:
    use "/Users/zhaoyao/Documents/wiki/daily_contribution.dta" 
    cd "/Users/zhaoyao/Documents/wiki"
    rename *, lower
    
    Above are the same as 1st.
    has some new stuff
    
    *same codes as 1st .do file
    
    local name "Panel B. Contributions from nonblocked contributors:"
    matrix rownames table1 = "`name'Addition" "`name'Deletion" "`name'Total"
    
    matrix colnames table1 = "Mean" "Standard Error" "Mean" "Standard Error" "t-stats" 
                         
    estout matrix(table1, fmt(2))
    
    #delimit ;
    estout matrix(table1, fmt(2)) using table1_panelB.tex, 
    style(tex) 
    replace
    mlabels(,none) collabels(,none)
    ;
    I'm asking Stata veteran's opinion. If you are in charge, will you and how do you simplify these 2 do files?

  • #2
    I think that it really depends on personal tastes and what else you want to do with the code.
    In your case, I would be tempted to merge two do-files into one do-file if the difference between the two files is not too large.
    You could code something like
    Code:
    args argument // accept one argument for the do-file to switch between A and B
    use "/Users/zhaoyao/Documents/wiki/daily_contribution.dta" 
    cd "/Users/zhaoyao/Documents/wiki"
    rename *, lower
    
    *same codes as 2nd .do file
    
    if  "`argument'"=="A"{
    *Here code exclusiv to 1st do file
    
    }
    if  "`argument'"=="B"{
    *Here code exclusiv to 2nd do file
    
    }
    
    local name "Panel `argument'. Contributions from nonblocked contributors:"
    matrix rownames table1 = "`name'Addition" "`name'Deletion" "`name'Total"
    
    matrix colnames table1 = "Mean" "Standard Error" "Mean" "Standard Error" "t-stats" 
                         
    estout matrix(table1, fmt(2))
    
    #delimit ;
    estout matrix(table1, fmt(2)) using table1_panel`argument'.tex, 
    style(tex) 
    replace
    mlabels(,none) collabels(,none)
    ;

    Comment


    • #3
      See -help include- ; you can -include- the same code in multiple do files.

      Comment


      • #4
        Jeph Herrin You are right about -include-. I completely forgot it because I do not use it myself. But in the scenario described above, it might be the better solution or at least another useful approach.

        Comment


        • #5
          But I checked -help include-. It runs the whole do file. I need to run some of them instead.

          Comment


          • #6
            I think what Jeph Herrin meant was that you create another do-file which contains the common parts and then use the include-command integrate the common code back into the original two do-files. In this way, your code becomes cleaner.

            Comment


            • #7
              Exactly Sven-Kristjan Bormann . Put all of the common code in a separate file, say -common.do-, and then -include common.do- in any other dofile file where you want it to appear. When you use -include-, the file is executed as if it were part of the current file, not as a separate do-file.

              Comment

              Working...
              X