Import SAS data with haven
haven
is an extremely easy-to-use package to import data from three software packages: SAS, STATA and SPSS. Depending on the software, you use different functions:
- SAS:
read_sas()
- STATA:
read_dta()
(orread_stata()
, which are identical) - SPSS:
read_sav()
orread_por()
, depending on the file type.
All these functions take one key argument: the path to your local file. In fact, you can even pass a URL; haven
will then automatically download the file for you before importing it.
You'll be working with data on the age, gender, income, and purchase level (0 = low, 1 = high) of 36 individuals (Source: SAS). The information is stored in a SAS file, sales.sas7bdat
, which is available in your current working directory. You can also download the data here.
This is a part of the course
“Intermediate Importing Data in R”
Exercise instructions
- Load the
haven
package; it's already installed on DataCamp's servers. - Import the data file
"sales.sas7bdat"
. Call the imported data framesales
. - Display the structure of
sales
withstr()
. Some columns represent categorical variables, so they should be factors.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Load the haven package
# Import sales.sas7bdat: sales
# Display the structure of sales
This exercise is part of the course
Intermediate Importing Data in R
Parse data in any format. Whether it's flat files, statistical software, databases, or data right from the web.
Next to R, there are also other commonly used statistical software packages: SAS, STATA and SPSS. Each of them has their own file format. Learn how to use the haven and foreign packages to get them into R with remarkable ease!
Exercise 1: havenExercise 2: Import SAS data with havenExercise 3: Import STATA data with havenExercise 4: What does the graphic tell?Exercise 5: Import SPSS data with havenExercise 6: Factorize, round twoExercise 7: foreignExercise 8: Import STATA data with foreign (1)Exercise 9: Import STATA data with foreign (2)Exercise 10: Do you know your data?Exercise 11: Import SPSS data with foreign (1)Exercise 12: Excursion: CorrelationExercise 13: Import SPSS data with foreign (2)What is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.