Announcement

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

  • Trying to save collapsed files - invalid file specification error

    Hello,

    I am trying to collapse all files in a folder and replace the old files with the new ones. Stata seems to be having trouble with the code to save and replace the collapsed files. Here is what I have written so far:
    Code:
    local files : dir "E:\Data3" files "*.dta"
     
    cd "E:\Data3"
    
    foreach file in `files' {
            use `file’, clear
            collapse (mean) tMin tMax prec, by (dateNum fips_code)
            save, replace
    }
    Stata returns:
    invalid file specification
    r(198);
    I think the error has something to do with trying to save a file after it has been collapsed. I have tried to collapse just one file, which works, but when I try to save it I get the same error.

    Thank you.

  • #2
    I think
    Code:
    save, replace
    should be
    Code:
     save `file', replace
    Last edited by Justin Niakamal; 12 Jan 2019, 18:25. Reason: spacing

    Comment


    • #3
      A careful reading of the output of help save surprises me with the information that the filename is indeed optional: the current filename will be substituted if none is given.

      The problem, then, is that help collapse describes the command as

      collapse converts the dataset in memory into a dataset of means, sums, medians, etc.
      and, implicitly, Stata considers the collapsed dataset a new, and thus unnamed dataset, and so, as Justin suggests in post #2, you must specify the output dataset name.

      Comment

      Working...
      X