Announcement

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

  • Import mulitple sas7bdat files into stata with a loop?

    Hello

    I have 524 sas files I would like to import as dta files, ex.

    bef201012.sas7bdat
    bef201112.sas7bdat
    bef201212.sas7bdat
    faik2010.sas7bdat
    faik2011.sas7bdat
    faik2012.sas7bdat
    ind2010.sas7bdat
    ind2011.sas7bdat
    ind2012.sas7dat

    etc,

    My code looks like this:

    local filenames: dir . files "*sas7bdat"

    foreach file of local filenames {
    import sas using "`file'"
    save `file'.dta, replace
    }

    but it doesn't seems to work very well? Is there anybody that can help me correct my code

    Kind regards Frank




  • #2
    Maybe something closer to the following.
    Code:
    local filenames: dir . files "*.sas7bdat"
    
    foreach file of local filenames {
        import sas using "`file'", clear
        local stata_name : subinstr local file ".sas7bdat" ""
        save `stata_name', replace // Beware spaces in path; enclose in double-quotation marks, if needed
    }
    You don't need to specify the file extension when saving a Stata dataset.

    Comment


    • #3
      Thank you Joseph! It worked fine

      Kind regards Frank
      Last edited by Frank Kjeldsen; 30 Jul 2020, 03:08.

      Comment


      • #4
        Hello,

        I have tried using the code Joseph suggested:

        Code:
        cd "path_to_the_data"
        local filenames: dir . files "*.sas7bdat"
        
        foreach file of local filenames {
            import sas using "`file'", clear
            local stata_name : subinstr local file ".sas7bdat" ""
            save "path_to_save_folder\`f'`stata_name'", replace
        }
        It appears to work (pretty quickly I may add) and I see the Stata data file in my destination folder. However, When I go to open that new Stata dataset, I get an error: "file note found (error code 601)". When I do the conversion manually, everything opens without a problem. Any suggestions?

        Comment


        • #5
          I should add that I am using Stata 17.

          Thank you in advance for any suggestions!

          Comment


          • #6
            I'm not at my computer to test this, but could your macro variable `f' be causing the problem? Either it shouldn't be there or perhaps was omitted when you programmatically try to -use- the new dataset?

            Comment


            • #7
              Hmm, I tried this and still the same issue. Also, I do not include the `f' the file gets overwritten with the new file.

              Comment


              • #8
                Originally posted by Elena Draghici View Post
                Hmm, I tried this and still the same issue. Also, I do not include the `f' the file gets overwritten with the new file.
                Flip your backslash (reverse solidus) to a forward slash (solidus) so that it won't escape what follows it.
                Code:
                save "path_to_save_folder/`f'`stata_name'", replace
                What is `f', anyway? Is is defined in the code that you're not showing?

                Comment


                • #9
                  Hi Joseph, thank you so much, it worked! The forward slash also solved a different problem too, so I really appreciate it!

                  The `f' is short for `file' so that the naming convention of the SAS files is kept when saving the .dta file.

                  Comment

                  Working...
                  X