Announcement

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

  • Problem with modifying dta files from directory I get the error: no variables defined

    Hi, I´m working with a code to call up files from a directory and save them in another folder with changes.
    I have dta files for each quarter (years 2005-2018) and want to save them with a few changes before doing appending them.
    Code:
        
    foreach y in 05 06 07 08 09 10 11 12 13 14 15 16 17 18 {
        forval q=1/4 {
            capture use "$od\sdemt`q'`y'.dta", clear
                    destring n_ent, replace
            capture gen year=20`y'
            capture gen quarter=`q'
            save "$prd\sdemt`q'`y'.dta", replace    
            }
    }
    Problem: when I run the code it gets me the error:
    no variables defined.
    I am working in a Windows 10 computer, however, when I run this code in a mac computer it works perfectly.
    I made sure that the folder is the one with the dta files using the command cd
    Also changed / for \ while using Windows 10.
    Does anyone have any advice of what could be happening?
    Thank you!

    Eugenia
    Last edited by Eugenia Suarez; 03 Apr 2019, 09:02.

  • #2
    Originally posted by Eugenia Suarez View Post
    Also changed / for \ while using Windows 10.
    Undo this change and see whether it works. The backslash acts as an escape character and is dangerous when used with local and/or global macros.

    Also, get rid of all those captures; they are even more dangerous. Had you prefixed your destring (and save) with capture, as you do for all other lines of code, you would have gotten the impression that your code worked when, in fact, it did nothing at all.

    Best
    Daniel

    Comment


    • #3
      Building on Daniel's advice, if you want to suppress the usual messages from a command, you should precede it with the quietly prefix rather than the capture prefix. The capture prefix not only suppresses the usual messages, it also suppresses error messages and prevents the program from stopping when there is an error, which means any error message you eventually see may not tell you much about what the original error was. The capture command is designed be followed by examination of the return code given by _rc to take appropriate action on anticipated error codes and to stop on unanticipated errors.

      See the output of help quietly and help capture for more details.

      Comment


      • #4
        Thank you for your responses! It helped me to understand where the problem was. I got rid of the
        Code:
        captures
        .
        Thanks again!

        Comment


        • #5
          Tangentially, I'll just point out that you do not have to enumerate all those numbers 05 through 18 in your -foreach y in- loop. You can do this instead:

          Code:
          forvalues z = 5/18 {
              local y: display %02.0f `z'
                  forvalues q = 1/4 {
                         etc. using `y' just as before

          Comment

          Working...
          X