Announcement

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

  • loop for generating new variables

    Dear Statalisters,

    I'm new to loops in Stata. I have ~50 files with exactly the same variables in them and wanting to generate same new variables for each file. I tried this:

    clear
    cd "E:\test"
    local list : dir . files "*.dta"
    foreach f of local list {
    gen person_id=patid
    drop patid
    save "`f'.dta", replace
    clear
    }

    But then got this error message:
    patid not found
    r(111);

    Could you please advise how to fix this problem?

    Many thanks

  • #2
    You haven't opened your file inside your loop, so that is why Stata could not find the variable patid. Here is a fixed version of your code:

    Code:
    clear
    cd "E:\test"
    local list : dir . files "*.dta"
    foreach f of local list {
        use `f', clear
        rename patid person_id
        save "fixed`f'", replace
    }
    So at each round I first open that dataset, rename the variable padid to person_id (doing it this way preserved the data type, which can be especially important for id variables, see help precision, display formats, labels, and notes), and than save it as a different file (fixed<old_name>, where <old_name> is the name of the original file). It is extremely important that you never ever ever ever ever under any circumstance change the raw data. So that is why it is save under a different name. Now, if you made an error you just fix the .do file, run it again, and it is fixed. If you have overwritten the raw data, you have to throw everything away, and start collecting new data.

    I expect that you want to do more to those dataset than rename one variable. If there are a large number of operations you want to perform, then it can be helpful to store them in their own .do file. The first .do file would look like this:

    Code:
    clear all
    cd "E:\test"
    local list : dir . files "*.dta"
    foreach f of local list {
        do fileprep.do `f'
    }
    The second file (fileprep.do) would look like this:

    Code:
    args file
    
    use `file', clear
    
    rename patid person_id
    // other stuff you want to do
    
    save "fixed`file'", replace
    So the first file looks pretty much like what you have done before except that inside the loop there is only the line do fileprep.do `f'. With the do command you can do another do file, in this case the file fileprep.do. Anything you type after the filename is passed on to that do file as an argument: the first thing after the file name is stored in the local macro `1', the second in the local macro `2', etc. Inside the second file (fileprep.do), the command args "renames" the local macro `1' to `file', which I think is easier to read. After that it looks like the things we have done before inside the loop. This trick is useful when you want to do a lot of things to those files.
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Thank you ever so much Maarten. This is most helpful. It works beautifully now

      Comment

      Working...
      X