Announcement

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

  • double looping using forvalue

    I wanted to import spss files into stata. The format of the file is month_date_raw_data.sav. For example "1_2020_raw_data.sav" I tried to write a loop to call in each month and year but I keep getting an error r(601). Why isn't this loop working?

    HTML Code:
    forvalues i= 2020/2021 {
       forvalues j=1/12 {
      import spss using "C:\Users\bf\files\spss\`j'_`i'_raw_data.sav" , case(lower) clear
        
      *Add year and month variable 
       gen year= `j'
       gen month=`i'
       tempfile part_`i'`j'
       save `part_`i''`j'
    }
     }

  • #2
    You have an extra '

    Should work if you substitute in

    Code:
    save `part_`i'`j''

    Comment


    • #3
      Thanks but that's not where my code breaks. It gives me this error:

      file C:\Users\bf\files\spss`j'_2020_raw_data.sav not found

      The file 1_2020_raw_data.sav exists and if I switch the i and j position, I get the same error. It doesn't seem to recognize the first loop.

      Comment


      • #4
        You switch year and month from outside the loop to inside.

        Comment

        Working...
        X