Announcement

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

  • Best way to write "input files" at top of .do file (macros?)

    I am interested in listing all of my "input" and "output" files early in my code so that the list is easy to look at and modify. Is there a standard way to do this in Stata?

    My best guess is to use global or local macros to store the text of the file path and then call upon those macros later when I need to import that specific data set. I'm not sure how this would work or if another approach would be preferred.

  • #2
    I never know all the list of input files and output files beforehand, so I don't usually specify them at the top (I do for when running regressions and then exporting them to Excel using outreg2).

    Code:
    capture set more off
    clear
    use "C:\Dissertation\1-Data\cvc_job_talk_cleaned.dta", clear
    set linesize 80 
    
    version 14
    cd C:\Data  /* Change directory to C:Data */
    
    
    local rhsvars "acq_exp lnalliances_3yr lnpatents_3yr  acq_re lnassets acq_cash_assets acq_q target_age target_public"
    local rhsvars "`rhsvars' ln_fwd_cites_5yr stock terms_undisclosed boom9900 post2000"
    
    
    local outreg_file "cvc3_table4_recency_diminishing"
    local outreg_line1 "excel bdec(4) rdec(3) se bracket addstat(F test: , e(F)) adec(3)" 
    local do_file "C:\1-cvc-paper3\Tables for Org Sci R&R\Table 4 - Recency & Diminishing returns.do"
    
    
    *** Start running regressions here
    reg car4_250_100 `rhsvars' if good_gary==1, cluster(acq_gvkey)
    outreg2 using `outreg_file', `outreg_line1' ctitle("Everything - so lines up") ///
        addnote("", do file is `do_file', "Run on $S_DATE", Log file is `log_file', Using data from $S_FN) replace
        * Note $S_FN could also be written as c(filename) and $S_DATE could be written as c(current_date)
        * c(filename) returns a string containing the filename last specified with a use or save, such as "C:\Data\auto.dta".  c(filename) is equal to $S_FN.
        * c(filedate) returns a string containing the date and time the file in c(filename) was last saved, such as "7 Jul 2014 13:51".  c(filedate) is equal to $S_FNDATE.
        * LOOKUP creturn
    
        
    * 3. CVC Count - current yr
    xtreg car4_250_100 ln_cvcount1 `rhsvars' if good==1, fe i(acq_gvkey) vce(cluster acq_gvkey)
    outreg2 using `outreg_file', `outreg_line1' ctitle("Current year only") append
    
    * 4. CVC Count - prior yr
    xtreg car4_250_100 ln_cvc_count_prior `rhsvars' if good==1, fe i(acq_gvkey) vce(cluster acq_gvkey)
    outreg2 using `outreg_file', `outreg_line1' ctitle("Prior yr only") append
    
    * 5. CVC Count- 2 yrs prior
    xtreg car4_250_100 ln_cvc_count_2yr_prior `rhsvars' if good==1, fe i(acq_gvkey) vce(cluster acq_gvkey)
    outreg2 using `outreg_file', `outreg_line1' ctitle("2 yr prior") append
    So I've defined outreg_file, do_file, and log_file as local macros. It would be easy enough to create local files for input_file1, input_file2, input_file3, and then call "use `input_file1', clear" and so on.

    Comment


    • #3
      Thank you. This makes sense. Is there way to do this with string scalars instead? I know I can do this:
      HTML Code:
      scalar dir = "../mydir"
      cd dir
      but don't know if there's an equivalent way to refer to excel files in "import excel" (local macros work for this, but I prefer using scalars)

      Comment


      • #4
        Originally posted by Krista Lane View Post
        Is there way to do this with string scalars instead? . . .local macros work for this, but I prefer using scalars
        I would stick with local macros, but the following seems to work.
        Code:
        version 15.1
        
        clear *
        
        quietly sysuse auto
        
        scalar define source = "Autos.xlsx"
        
        quietly export excel using `=source', sheet(Autos) firstrow(variables) replace
        
        import excel using "`=source'", sheet(Autos) firstrow clear
        
        erase "`=source'"
        
        scalar drop source
        
        exit
        What advantage do you see in a string scalar?

        Comment


        • #5
          Krista,
          see a different approach with OOP below.
          Best, Sergiy Radyakin

          Code:
          clear all
          version 15.0
          
          class Job {
             string input
             string output
          }
          
          program define .Job.Work
            use "`.input'"
            outsheet using "`.output'", replace
          end
          
          .MyJob1=.Job.new()
            .MyJob1.input="C:\Program Files (x86)\Stata15\ado\base/a/auto.dta"
            .MyJob1.output= "C:\temp\delme.txt"
          .MyJob1.Work

          Comment


          • #6
            Oh yes, the code that I posted is incorrect. It should be
            Code:
            scalar dir = "../mydir"
            cd `=dir'
            so what Joseph writes makes sense.

            My understanding was that scalars don't disappear, so if I want to run a few lines of code but not the entire .do file, I can still refer to the directories/files if I defined them in an earlier run. I've been discouraged from using global macros which also don't disappear (but perhaps using scalars like this is also not advisable).

            Thank you both for your answers!

            Comment

            Working...
            X