Announcement

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

  • Syntax error in a loop using macros

    Dear Statalisters,

    This question may seem simple to answer, yet I can't find the solution to my issue. When running this code:

    Code:
    forvalues x = 1/2 {
    local z: dir "$input/survey_`x'/Zimbabwe" files "*.dta"
    use "$input/survey_`x'/Zimbabwe/`z'", clear
    rename var1 var2
    rename var3 var4
    
    save, replace
    }
    I get the following result:

    invalid 'zimbabwe'
    r(198);

    If I choose to define a global macro rather than a local, I get the following result:

    file C:/Users/xxxxx/yyyy/zzzzz/uuuuu/Inputs/survey_1/Zimbabwe/.dta not found
    r(601);

    Can someone explain me where's the mistake in my code? It shouldn't be hard to spot the error, yet i can't seem to find it.

    Please note that there is exactly one file in each of the two Zimbabwe directories. I suspect my problem arised from the fact that the file name has spaces in it, but if my intuition is correct, how to fix this issue without changing the name of the file?


    Last edited by Adam Sadi; 07 Jul 2022, 04:14.

  • #2
    This is always a fun one. Try this
    Code:
    forvalues x = 1/2 {
    
    
    local z: dir "$input/survey_`x'//Zimbabwe" files "*.dta"
    
    
    use "$input/survey_`x'//Zimbabwe//`z'", clear
    
    rename (var1 var3) (var2 var4)
    save, replace
    }

    Comment


    • #3
      Dear Jared:

      Thank you very much for your help. However when I copy and paste your code I still get the same output, i.e.

      Code:
      invalid 'zimbabwe' 
      r(198);

      Comment


      • #4
        Do
        Code:
        set tr on
        and run the loop again. Post back exactly the entire output Stata gives you

        Comment


        • #5
          Dear Jared:

          Here is the output after introducing the command you provided:


          - forvalues x = 1/2 {
          - local z: dir "$input/CF/Round_`x'//Zimbabwe" files "*.dta"
          = local z: dir "C:/Users/xxxx/Dropbox/yyyy/zzz/Inputs/survey_1//Zimbabwe" files "*.dta"
          - use "$input/survey_`x'//Zimbabwe//`z'", clear
          = use "C:/Users/xxxx/Dropbox/yyyy/zzz/Inputs/survey_1//Zimbabwe//"zimbabwe-covid follow up 2020-full data.
          > dta"", clear
          invalid 'zimbabwe'
          rename (var1 var3) (var2 var4)
          save, replace
          }

          I forgot to mention I was working on a Dropbox, in case that may help. However I had to hide the parts of $input that are supposed to be private. $input is just a local that is a short for my "C:/Users/xxxx/Dropbox/yyyy/zzz/Inputs/" path

          Comment


          • #6
            The issue is your file is being used as


            "zimbabwe-covid follow up 2020-full data.

            With the quotation marks being the main issue

            Comment


            • #7
              Jared:

              Indeed, I understand that there shouldn't be any quotation mark in the file. Is there any way I can ignore them?

              Thank you very much. In any case, I didn't know about the command set tr on and it will be very useful in the future.

              Comment


              • #8
                Uhhhhhh i think remove the quotes from around dta.



                And yeah tracing will 8/10 times be a life saver.

                When done just do set tr off

                Comment


                • #9
                  Sorry Jared, I may have not understood your last post or I may have not asked my question the right way.

                  I am using a loop over 2 files (possibly more in the future but at the moment I am just trying with 2) and I would like to replace the same variable with another in each of those files. So I would like an automated way to extract the file name within a defined directory in a macro, in order to use the same macro without having to copy and paste the name of the file every time. I didn't quite understand where should I remove the quotes

                  Comment


                  • #10
                    I know what you wanna do, what I meant was this
                    Code:
                    forvalues x = 1/2 {
                    local z: dir "$input/survey_`x'//Zimbabwe" files *.dta
                    use "$input/survey_`x'//Zimbabwe//`z'", clear
                    rename (var1 var3) (var2 var4)
                    save, replace
                    }

                    Comment


                    • #11
                      Dear Jared:

                      Thank you very much for your precious time. However I'm afraid the code doesn't work:

                      varlist not allowed
                      r(101);

                      EDIT : I could find a solution by trying using a loop over the files I searched with the command dir and it worked!
                      Last edited by Adam Sadi; 07 Jul 2022, 13:27.

                      Comment


                      • #12
                        Can you post the code that was finally successful? I should've thought of that solution sooner. Adam Sadi

                        Comment


                        • #13
                          Code:
                           forvalues x = 1/2 {
                          local z: dir "$inputs/Zimbabwe" files "*.dta"
                              
                          foreach file of local z {
                                  use "$inputs/Zimbabwe/`file'", clear
                                  replace (var1 var2) (var3 var4)
                                  save, replace
                              } 
                          }
                          Thanks a lot for your time!

                          Comment

                          Working...
                          X