Announcement

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

  • #16
    Nick Cox , I execute the code using a do-file editor

    - macro list - reports what below:

    Code:
    . macro list
    save:           C:\Users\sergio.rivaroli\OneDrive - Alma Mater Studiorum Università di Bologna\Desktop/Stata
    dir:            C:\Users\sergio.rivaroli\OneDrive - Alma Mater Studiorum Università di Bologna\Desktop
    T_gm_fix_span:  0
    S_level:        95
    F1:             help advice;
    F2:             describe;
    F7:             save
    F8:             use
    S_ADO:          BASE;SITE;.;PERSONAL;PLUS;OLDPLACE
    S_StataSE:      SE
    S_OS:           Windows
    S_OSDTL:        64-bit
    S_MACH:         PC (64-bit x86-64)
    Last edited by Sergio Rivaroli; 02 Feb 2023, 06:39.

    Comment


    • #17
      I think you may be missing a set of compound double quotes, which creates issues because your paths have spaces. Where your code has

      Code:
      local savefile = subinstr("`f'",".sav",".dta",.)
      I think it should be (note the extra ` and ' )

      Code:
      local savefile = subinstr(`"`f'"',".sav",".dta",.)
      to ensure the double quoted filename gets passed through correctly.

      As with my prior comment, running set trace on would show you what Stata is executing at each step, so you would see exactly how the savefile macro is being expanded.

      Comment


      • #18
        Hi Kye Lippold , I've used the following code

        Code:
        clear
        set more off
        global dir "C:\Users\sergio.rivaroli\OneDrive - Alma Mater Studiorum Università di Bologna\Desktop"
        local fls: dir "$dir" files "*.sav"
        cd "$dir" 
        global save "$dir/Stata"
        
        foreach f of local fls{
        cd "$dir"
        import spss  `"`f'"' , clear
        local savefile = subinstr(`"`f'"',".sav",".dta",.)
        cd "$save"
        save `"`savefile'"', replace
        }

        but nothing happens in the directory C:\Users\sergio.rivaroli\OneDrive - Alma Mater Studiorum Università di Bologna\Desktop/Stata

        Comment


        • #19
          Dear Kye Lippold , I've run the command - set trace on - and after the command - import spss `"`f'"' , clear - , and resulted this info in red : "using required"

          Click image for larger version

Name:	Immagine 2023-02-02 154334.png
Views:	1
Size:	43.3 KB
ID:	1700031


          Comment


          • #20
            This seems to be what you're trying to do. I have no reason to suppose that it will give results. But it could be important to know what the filelist is.


            Code:
            clear
            set more off
            
            cd "C:\Users\sergio.rivaroli\OneDrive - Alma Mater Studiorum Università di Bologna\Desktop"
            local fls: dir . files "*.sav"
            
            di `"`fls'"'
            
            foreach f of local fls {
                 import spss  `"`f'"' , clear
                 local savefile = subinstr(`"`f'"',".sav",".dta",.)
                 save `"stata/`savefile'"', replace
            }

            Comment


            • #21
              Originally posted by Sergio Rivaroli View Post
              Dear Kye Lippold , I've run the command - set trace on - and after the command - import spss `"`f'"' , clear - , and resulted this info in red : "using required"
              Ah, so set trace has shown you the likely issue -- the correct syntax for import spss is
              Code:
              import spss using `filename', clear
              so you need to include the word "using".

              The exact error is a bit hidden because set trace is including a lot of extra output. If you run set tracedepth 1 first, you will see less output, including the exact "import spss" command that your code is running. That should help debug if it is some other issue.

              Comment


              • #22
                #21 is unfortunately wrong, at least w.r.t. Stata 17. The word using is optional. https://www.stata.com/help.cgi?import_spss

                The trace shows to me that the command can't see a filename at all, and that is the problem.

                Comment


                • #23
                  Dear Nick Cox and Kye Lippold , below the two outputs:

                  The file list:
                  Click image for larger version

Name:	Img_001.png
Views:	1
Size:	27.4 KB
ID:	1700160




                  and other commands:

                  Click image for larger version

Name:	Img_005.png
Views:	1
Size:	33.7 KB
ID:	1700161




                  Comment


                  • #24
                    I suggested much simpler code in #20.

                    I will try some experiments later today but I don't have any .sav files to test that part. Does either of these work directly?

                    Code:
                    import spss pbma_1 
                    
                    import spss "pbma_1"

                    Comment


                    • #25
                      Dear Nick Cox , botw work.

                      Click image for larger version

Name:	Img_006.png
Views:	1
Size:	21.5 KB
ID:	1700190

                      Comment


                      • #26
                        As implied, I don't use SPSS and have no .sav files to experiment with. This is the nearest analogue of your code I can think up, and it works for me.

                        Code:
                        sysuse auto, clear
                        export delimited using auto.csv 
                        
                        sysuse bplong, clear 
                        export delimited using bplong.csv 
                        
                        local fls : dir . files "*.csv"
                        
                        di `"`fls'"'
                        
                        foreach f of local fls {
                             import delimited  `"`f'"' , clear
                             local savefile = subinstr(`"`f'"',".csv","2.dta",.)
                             save `"`savefile'"', replace
                        }
                        As mentioned twice already, your code can be simplified, and although I can't see a bug in the complicated version, I still recommend simpler code.

                        Comment


                        • #27
                          Several SPSS datasets are available for download here:
                          --
                          Bruce Weaver
                          Email: [email protected]
                          Version: Stata/MP 19.5 (Windows)

                          Comment


                          • #28
                            Thanks to Bruce Weaver I can replicate the problem. This works for me with files cancer.sav and disease.sav I didn't test on filenames containing spaces.



                            Code:
                            local fls : dir . files "*.sav"
                            
                            di `"`fls'"'
                            
                            foreach f of local fls {
                                 import spss  "`f'" , clear
                                 local savefile = subinstr(`"`f'"',".csv",".dta",.)
                                 save `"`savefile'"', replace
                            }
                            The line that caused problems was

                            Code:
                             
                             import delimited  `"`f'"' , clear

                            Comment


                            • #29
                              Dear Nick Cox ,

                              Thank you very much indeed, the code runs! Yesssssss!!!

                              Comment


                              • #30
                                A community effort!

                                Comment

                                Working...
                                X