Announcement

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

  • Loop Through The Same Procedures with Multiple Datasets

    Hi all,

    I have several datasets that I would like to do the same procedures to. I have the same generic variable names, like VAR1, VAR2, etc. in each dataset. I want to be able to run one do-file to perform the same tests and procedures over all of the different datasets. I want the outputs to be saved using the beginning filename. Please let me know if you can help me out or if there are already existing answers about this topic.

  • #2
    You are not giving us a lot of details to work with, so we can only give you very general answers.

    One thing that often helps in this type of situation is that you can add options when asking Stata to execute a .do file.

    Say we have a .do file called dataprep_anc.do and we have another do file that contains the line do dataprep_anc.do file1.dta something else. If we run dataprep_anc that way, then while running dataprep_anc.do it will define the local macro `0' containing file1.dta something else, the local macro `1' to contain file1.dta, local macro `2' containing something and local macro `3' containing else. So a sketch of a possible solution would involve two .do files, lets call them dataprep.do and dataprep_anc.do

    Code:
    // ----------------------------------- begin dataprep.do
    forvalues i = 1/10 {
        do dataprep_anc file`i'.dta
    }
    
    // ------------------------------------- end dataprep.do
    
    //-------------------------------- begin dataprep_anc.do
    use `1'
    
    // smart operations
    
    // save the results in some smart way
    
    // --------------------------------- end dataprep_anc.do
    I tend to use the args command inside dataprep_anc.do to automatically give the local macros more informative names.
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment

    Working...
    X