Announcement

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

  • How to open sas7bdat file in stata?

    The original data form is sas7bdat, how can I open it using sata?

  • #2
    Spacey:
    you can open that file with
    Stat/Transfer (
    http://www.stattransfer.com/), that comes at a reasonable price.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      I don't use SAS myself, but if you have access to a version then you should export the data as a .csv file (or .dta if it allows it) and then import to Stata through the normal route of 'import delimited using "{PATH}/name of dataset.csv" or use "{PATH}/name of dataset.dta".

      If you do not have access to SAS (like me) then I think you will need to convert the data in a different programme first. I usually use R for this. The code would be:

      Code:
      //Import sas7dbat package to R (this only ever needs to be done once)
      install.packages("sas7bdat")
      //Load sas7bdat package into your current R session (this needs to be done whenever you start a new session)
      library("sas7bdat")
      //Read your sas7bdat data into R
      data = read.sas7bdat("name of dataset.sas7bdat")    
      
      //Load foreign package (same as above - needs to be done for every session)
      library(foreign)
      //Export your dataframe as a .dta file
      write.dta(data, "name of dataset.dta")
      Note: the above is R code and will not work if you try and run it in Stata. If you do not have R, you can download it here: https://www.r-project.org/; this is free!

      There might be a package which does this directly in Stata and i'd be keen to hear if there is - I do not know of any though

      Comment


      • #4
        [It's proc export in SAS, as told in the other thread]

        References:
        http://www.ats.ucla.edu/stat/mult_pk...AS_toStata.htm
        https://support.sas.com/documentatio...a000316288.htm

        Comment


        • #5
          Originally posted by Chris Larkin View Post
          I don't use SAS myself, but if you have access to a version then you should export the data as a .csv file (or .dta if it allows it) and then import to Stata through the normal route of 'import delimited using "{PATH}/name of dataset.csv" or use "{PATH}/name of dataset.dta".

          If you do not have access to SAS (like me) then I think you will need to convert the data in a different programme first. I usually use R for this. The code would be:

          Code:
          //Import sas7dbat package to R (this only ever needs to be done once)
          install.packages("sas7bdat")
          //Load sas7bdat package into your current R session (this needs to be done whenever you start a new session)
          library("sas7bdat")
          //Read your sas7bdat data into R
          data = read.sas7bdat("name of dataset.sas7bdat")
          
          //Load foreign package (same as above - needs to be done for every session)
          library(foreign)
          //Export your dataframe as a .dta file
          write.dta(data, "name of dataset.dta")
          Note: the above is R code and will not work if you try and run it in Stata. If you do not have R, you can download it here: https://www.r-project.org/; this is free!

          There might be a package which does this directly in Stata and i'd be keen to hear if there is - I do not know of any though
          Thanks very much. I used this code in R and did it successfully.
          And share a method using Python:
          **Step 1** cmd----pip install sas7bdat
          **Step 2**
          from sas7bdat import SAS7BDAT with SAS7BDAT(fname) with f:
          df = f.to_data_frame() *.to_stata("name.dta")
          Last edited by Spacey Shi; 25 Oct 2016, 07:30.

          Comment


          • #6
            Originally posted by Chris Larkin View Post
            I don't use SAS myself, but if you have access to a version then you should export the data as a .csv file (or .dta if it allows it) and then import to Stata through the normal route of 'import delimited using "{PATH}/name of dataset.csv" or use "{PATH}/name of dataset.dta".

            If you do not have access to SAS (like me) then I think you will need to convert the data in a different programme first. I usually use R for this. The code would be:

            Code:
            //Import sas7dbat package to R (this only ever needs to be done once)
            install.packages("sas7bdat")
            //Load sas7bdat package into your current R session (this needs to be done whenever you start a new session)
            library("sas7bdat")
            //Read your sas7bdat data into R
            data = read.sas7bdat("name of dataset.sas7bdat")
            
            //Load foreign package (same as above - needs to be done for every session)
            library(foreign)
            //Export your dataframe as a .dta file
            write.dta(data, "name of dataset.dta")
            Note: the above is R code and will not work if you try and run it in Stata. If you do not have R, you can download it here: https://www.r-project.org/; this is free!

            There might be a package which does this directly in Stata and i'd be keen to hear if there is - I do not know of any though
            I transformed a sas format file into dta file in R, but the format of variable---date of birth was changed, the original format is year-month-date, but the dta file just shows year, what should I do to solve this problem?Thanks very much.

            Comment


            • #7
              Originally posted by Spacey Shi View Post
              I transformed a sas format file into dta file in R, but the format of variable---date of birth was changed, the original format is year-month-date, but the dta file just shows year, what should I do to solve this problem?Thanks very much.
              I don't know what R does when it transforms SAS datasets into Stata datasets, but if it's just a change in the format as you say, then you could do the following to fix that. Either
              Code:
              format birth_date %tdCY-N-D
              if it's a date (integer type with days since January 1st, 1960) or
              Code:
              format birth_date %tcCCYY-NN-DD
              if it's a date-time (double-precision type with number of milliseconds since midnight, January 1st, 1960).

              Comment


              • #8
                Originally posted by Chris Larkin View Post
                I don't use SAS myself, but if you have access to a version then you should export the data as a .csv file (or .dta if it allows it) and then import to Stata through the normal route of 'import delimited using "{PATH}/name of dataset.csv" or use "{PATH}/name of dataset.dta".

                If you do not have access to SAS (like me) then I think you will need to convert the data in a different programme first. I usually use R for this. The code would be:

                Code:
                //Import sas7dbat package to R (this only ever needs to be done once)
                install.packages("sas7bdat")
                //Load sas7bdat package into your current R session (this needs to be done whenever you start a new session)
                library("sas7bdat")
                //Read your sas7bdat data into R
                data = read.sas7bdat("name of dataset.sas7bdat")
                
                //Load foreign package (same as above - needs to be done for every session)
                library(foreign)
                //Export your dataframe as a .dta file
                write.dta(data, "name of dataset.dta")
                Note: the above is R code and will not work if you try and run it in Stata. If you do not have R, you can download it here: https://www.r-project.org/; this is free!

                There might be a package which does this directly in Stata and i'd be keen to hear if there is - I do not know of any though
                This worked perfectly for me too. Thanks for posting !!
                However, after converting the sas file to Stata, the date format is changed in Stata. The following is the extract from Stata-
                Name date Frequency
                AB 17167 34
                AC 17198 25
                AD 17226 23
                AE 17257 22
                AF 17287 21
                AG 17318 26
                AH 17348 25
                AI 17379 23
                AJ 17410 22
                AK 17440 21
                AL 17471 19
                AM 17501 18
                AN 17532 16
                I tried the command given above for change in birth date.. but it does not help me. Please suggest to recover the date stored in original file.

                Comment


                • #9
                  You can give rioweb a try. It doesn’t always work but when it does it is very easy to use.

                  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


                  • #10
                    I'll just note that .sas7bdat files can be read directly by Stata version 16 with the -import sas- command. Of course, if you are using an older version of Stata, this will not help you.

                    Comment


                    • #11
                      Originally posted by Richard Williams View Post
                      You can give rioweb a try. It doesn’t always work but when it does it is very easy to use.

                      https://gallery.shinyapps.io/rioweb/
                      Thanks for the suggestion.

                      Comment


                      • #12
                        Originally posted by Clyde Schechter View Post
                        I'll just note that .sas7bdat files can be read directly by Stata version 16 with the -import sas- command. Of course, if you are using an older version of Stata, this will not help you.
                        Well, I have Stata 14.Thanks.

                        Comment


                        • #13
                          Originally posted by Joseph Coveney View Post
                          I don't know what R does when it transforms SAS datasets into Stata datasets, but if it's just a change in the format as you say, then you could do the following to fix that. Either
                          Code:
                          format birth_date %tdCY-N-D
                          if it's a date (integer type with days since January 1st, 1960) or
                          Code:
                          format birth_date %tcCCYY-NN-DD
                          if it's a date-time (double-precision type with number of milliseconds since midnight, January 1st, 1960).
                          Hi, this command works as it gives me the date in months like the following-

                          date
                          2007-09-01
                          2007-09-01
                          2007-12-01
                          2007-03-01
                          2007-06-01
                          2007-04-01
                          2007-06-01
                          2007-07-01
                          2007-09-01


                          I have expected 12 monthly observations every year for each firm totalling to 400 firms.

                          However, when I use the command for dates to tsset or xtset my data for monthly. It gives me strange numbers like-
                          date 3390m8 3390m8 3390m8 3390m8 3390m8 3390m8 3390m8 3390m8 This is because I used format birth_date %tdCY-N-D here. Kindly suggest the command to convert in months so that I can tsset or xtset my data.Thanks.

                          Comment


                          • #14
                            Originally posted by Anita Mendi View Post
                            Kindly suggest the command to convert in months so that I can tsset or xtset my data.Thanks.
                            At the command line in Stata, type
                            Code:
                            help datetime
                            and click on the blue hyperlink for SIF-to-SIF conversion and then scroll down in that subsection until you see the table with the left-hand "From:" column of "date" and right-hand "To:" columns that include "monthly'. You'll see the function tm=mofd(td).

                            Given this and your earlier posts in this thread, I recommend that you give the entire help file a good read through.

                            Comment


                            • #15
                              Thanks Joseph!!

                              I used the following command and it worked.

                              gen mdate = mofd(date)
                              format mdate %tm

                              Comment

                              Working...
                              X