Announcement

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

  • Save datasets in loop

    Hi,

    I am trying to open, create a couple of variables, and then save a group of datasets in a different folder in a loop but is not working. The datasets open, the creation of variables work but the datasets do not save in the folder correctly. I want to save the datasets using the same names they have original. This is the code that I am using:

    global path "C:/Users/tessamay/folder"
    global raw "${path}/Data/Raw"
    global temp "${path}/Data/Temp"

    foreach x in 2019 2020 2021 2022 2023{
    use "${raw}/`x'.dta", clear
    {…}
    save "${temp}/ `x'.dta", replace
    }


    Thanks,

    Tessa

  • #2
    Get rid of the blank space between / and `x' in your -save- command.

    Comment


    • #3
      Hi, thanks Clyde for your answer. I did that and still wont save the datasets with the correct names.

      Comment


      • #4
        What are the file names that are currently written to the file system and why are they wrong? Please be as detailed as possible about the behavior you are seeing.

        Comment


        • #5
          I did this and it worked perfectly.

          Code:
          clear all
          cd S:/STATA/Junk/Raw
          
          forv i = 2019/2023 {
              clear
              set obs 1
              g year = `i'
              save `i', replace
          }
          
          global path "S:/STATA/Junk"
          global raw "${path}/Raw"
          global temp "${path}/Try"
          
          foreach x in 2019 2020 2021 2022 2023{
          use "${raw}/`x'.dta", clear
          g x = rnormal()
          save "${temp}/`x'.dta", replace
          }
          I did create the folders first.

          Comment


          • #6
            I used the same code than George proposed but for some reasons it still wont work. The datasets overwrites themselves under ".dta"

            Comment


            • #7
              The datasets overwrites themselves under ".dta"
              I'm not sure what you mean by that. Can you be more specific?

              Comment


              • #8
                I think O.P. means that the data sets are all written as a file named .dta, with each iteration overwriting the previous one. This implies that local macro `x' is, for some reason, not being recognized in the -save- command. The only thing I can think of that would cause that is if O.P. is trying to run the loop one line at a time, or in some chunks. That, of course, would lead to `x' disappearing when the code is interrupted. It is critical that the entire loop, from -foreach- through the final closing } be run without interruption in one fell swoop.

                Comment


                • #9
                  hmm. somehow `x' is being altered.

                  did you run exactly the same code (making up the data and creating the test directories)?


                  you may have done this, but you might [ di `x' ] right after the foreach line and before you save to see if it's being changed, and if so, track it back in the code to see why. Maybe one of the the interim calculations alters `x'?
                  Sometimes I mess up and use the same `i' in different loops, which makes a mess of it.

                  you can test this by doing nothing but open and save to a different directory.



                  Comment


                  • #10
                    George was right. I used the same `i' in different loops. I changed to another letter and worked perfectly.

                    Thanks

                    Comment

                    Working...
                    X