Announcement

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

  • STATA to SAS export error

    Hello statalister,

    I've been trying to export a large dataset from stata to SAS but I'm consistently encountering an error.

    There are several variables for which the export is not allowed, and the following error is displayed:

    Code:
    fdasave "my_database.sas",rename replace
    
    *Error below
    active_drug_1, active_drug_2, active_drug_3, active_drug_4, active_drug_5, active_drug_6, active_drug_10, active_drug_11, active_drug_12,
        active_drug_13, active_drug_7, active_drug_8, active_drug_9, active_drug_14, p_description, radiological_r:  string variables too long for
        .xpt format
    I've tried to "trim" the variables in order to remove blanks as shown in the help for SAS related export commands, but there is no replacement with the various *trim functions

    Code:
    foreach var of varlist active_drug_1 active_drug_2 active_drug_3         ///
    active_drug_4 active_drug_5 active_drug_6 active_drug_10 active_drug_11 ///
    active_drug_12 active_drug_13 active_drug_7 active_drug_8 active_drug_9 ///
    active_drug_14 p_description radiological_r {
        replace `var'=strltrim(`var')               // No replacement
        replace `var'=strtrim(`var')                // No replacement
        replace `var'=ustrtrim(`var')             // No replacement
        replace `var'=subinstr(`var'," ","",.)    // This command is executed but the error persists
        replace `var'=""                                 // This command is also executed but the error persists as well 
    }
    Thank you for your kind help

  • #2
    It seems to me your data may violate several rules for SAS export. For example, "The names of the variables and value labels may not be longer than eight characters". Also, "String variables may not exceed 200 characters." So, just trimming the variables may not be enough. You need to get them under 200 characters. Extra blanks may not be the issue.

    Do you really need Sas export or just Sas? Will this page meet your needs? https://gallery.shinyapps.io/rioweb/
    -------------------------------------------
    Richard Williams, Notre Dame Dept of Sociology
    StataNow Version: 19.5 MP (2 processor)

    EMAIL: [email protected]
    WWW: https://www3.nd.edu/~rwilliam

    Comment


    • #3
      Thank you for your answer Richard. I actually needed a ".sas" database.

      Comment


      • #4
        Can't SAS, at least recent versions, read Stata datasets? If not, then why not export to a file type that SAS can read, such as a plain-text delimited (tab, comma) file? I'm not sure that SAS can read Excel workbooks without additional costly add-ons, but that might be a possibility, too.

        Comment


        • #5
          Originally posted by Dusan Petrovic View Post
          Thank you for your answer Richard. I actually needed a ".sas" database.
          rioweb may have saved me from buying the latest version of stat/transfer. But if you need more power and flexibily, R is free, and you can install packages like rio and haven for importing and exporting files. I'm not crazy about R but it can fill in occasionally where Stata lacks some feature.
          -------------------------------------------
          Richard Williams, Notre Dame Dept of Sociology
          StataNow Version: 19.5 MP (2 processor)

          EMAIL: [email protected]
          WWW: https://www3.nd.edu/~rwilliam

          Comment


          • #6
            Richard Williams It seems that R cannot create SAS datasets.

            R package rioweb depends on package rio which depends on package haven (https://github.com/leeper/rio/issues/135) which has unresolved issue #224 "The exported file cannot be opened by SAS" (https://github.com/tidyverse/haven/issues/224).

            Comment


            • #7
              Bummer. I don't have SAS so I can't test. Maybe the problem is OS dependent or depends on the type of file you are converting from? You can always try it and see if it works. Or, if SAS can't read Stata directly, maybe you can use rioweb to convert to a format it can read.
              -------------------------------------------
              Richard Williams, Notre Dame Dept of Sociology
              StataNow Version: 19.5 MP (2 processor)

              EMAIL: [email protected]
              WWW: https://www3.nd.edu/~rwilliam

              Comment


              • #8
                Richard, SAS 9.3 for Windows can read Stata 12 datasets directly. For Stata 13 or higher datasets, SAS gives this error message: "For Stata, Release flags of 103 to 115 are supported. You had -> 60 Requested Input File Is Invalid ERROR: Import unsuccessful. See SAS Log for details." http://support.sas.com/kb/50/326.html

                Dusan, what do you mean by you need a ".sas" database? Usually SAS datasets have file name extension .sas7bdat whereas .sas is for the SAS syntax file.

                fdasave has been renamed export sasxport. See
                Code:
                help import sasxport
                You can export a few variables at a time to SAS to quickly narrow down the problem with the original SAS export error.

                Comment


                • #9
                  Aha. Well, rioweb says it creates .csv and .sas files for you. So, it sounds like it is creating the files SAS needs to create a SAS data file, not the SAS data file itself. So maybe it will be ok. Maybe rioweb did it this way because they were aware of the problems with trying to create a SAS data file directly. I would definitely give rioweb a try.

                  You could also try saving a Stata file in version 12 format. I don't know if that will work given that this file apparently has super-long strings. I don't remember when Stata added such support.

                  Then again, I don't know how long the strings are that .csv can support. I have no idea what one does with such super-long strings, but apparently some people have a use for them.

                  -------------------------------------------
                  Richard Williams, Notre Dame Dept of Sociology
                  StataNow Version: 19.5 MP (2 processor)

                  EMAIL: [email protected]
                  WWW: https://www3.nd.edu/~rwilliam

                  Comment


                  • #10
                    I'm not sure how valuable the OP's time and effort are, but maybe just breaking down and purchasing Stat/Transfer would have avoided all of the diversions with errors and unresolved issues and days' long back-and-forths on user lists, and kept the project smoothly on track.

                    Comment


                    • #11
                      Hopefully the OP got his problem solved way back in Post #3! And hopefully he'll chime back in and tell us one way or the other.

                      Stat/Transfer is a great program. But it costs money, and some people only need it for a one-shot task. I'm hoping that the freebie rioweb will meet a lot of people's needs. The few things I've tried it with seem to work fine, but I have no way of testing it for some formats.
                      -------------------------------------------
                      Richard Williams, Notre Dame Dept of Sociology
                      StataNow Version: 19.5 MP (2 processor)

                      EMAIL: [email protected]
                      WWW: https://www3.nd.edu/~rwilliam

                      Comment


                      • #12
                        Dear all,

                        Thank you for your answers and suggestions. What happened actually is that we have a new person in the lab who only uses SAS, which is why I needed to export the .dta database in .sas format. I used both fdasave and export sasxport functions without success.

                        In the end, I didn't include the problematic variables as the person didn't need them for their work, but I'm considering the "R option" with rioweb package proposed by Richard as I'm not willing to spend money for Stat/Transfer.

                        Thanks again

                        Dusan

                        Comment

                        Working...
                        X