Announcement

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

  • Looping over files in directory if they exist

    I am looping over csv files in a directory, the names of which differ by two strings that i call `fed' and 'mask'. Not all filenames with these combinations exist, however. Following advice from previous forums, I initialised my loop by:

    Code:
    foreach fed in EUR GER  {
        foreach mask in Yes No {
            local     thisfile : dir "$datapath" file "`fed'-Masks`mask'_concat.csv"
            if         "`thisfile'" == "" {
                                          di "`fed'-Masks`mask'_concat.csv does not exist. Skipping..."
            }
            else {        
                                          di "Processing `fed'-Masks`mask'_concat.csv..."
            }
        }
    }
    In this example, only the file "GER-MasksNo_concat.csv" exists, and it is the last called by the loop. The output is:

    Code:
    EUR-MasksYes_concat.csv does not exist. Skipping...
    EUR-MasksNo_concat.csv does not exist. Skipping...
    GER-MasksYes_concat.csv does not exist. Skipping...
    MasksNo_concat.csv"" invalid name
    Why is it giving this error?

  • #2
    For reasons that are unclear to me, when you create local macro thisfile, the non existent file creates it as "", which works OK. But when you have an existent file, you get ""GER-MasksNo_concat.csv"", which the parser then interprets as an empty string ("") followed by GER-MasksNo_concat.csv"", which is clearly an ill-expression. The quickest fix is to change -if "`thisfile'" == "" {- to:
    Code:
    if `"`thisfile'"' == "" {
    By enclosing `thisfile' in compound double quotes (`"..."'), Stata can correctly pair up beginning and end quotes that match and the code then runs.

    That said, what you've done is kind of a kludgy approach. Here's how I would do it:

    Code:
    foreach fed in EUR GER  {
        foreach mask in Yes No {
            capture confirm file "`fed'_masks`mask'_concat.csv"
            if   c(rc) {
                                          di "`fed'-Masks`mask'_concat.csv does not exist. Skipping..."
            }
            else {
                                          di "Processing `fed'-Masks`mask'_concat.csv..."
            }
        }
    }
    If you are not familiar with -capture- and -c(rc)-, read -help capture-. It's an incredibly useful command. Also read -help confirm- to see the things it can help you check on besides the existence of a file.

    Comment


    • #3
      Works great. I knew -capture-, but hadn't implemented with -c(rc)- before. That's neat.

      Comment

      Working...
      X