Skip to content

Instantly share code, notes, and snippets.

@jmclawson
Created June 12, 2023 22:07
Show Gist options
  • Save jmclawson/f4dafffd3f657b09b0192cafe270e9d3 to your computer and use it in GitHub Desktop.
Save jmclawson/f4dafffd3f657b09b0192cafe270e9d3 to your computer and use it in GitHub Desktop.
from SAS to R
---
title: "Importing SAS data into R"
---
## Get the data
```{r}
# Just download it once
if(!file.exists("medical.sas7bdat")) {
download.file("http://www.principlesofeconometrics.com/sas/medical.sas7bdat",
destfile = "medical.sas7bdat")
}
```
## Load the package and read the data with read_sas()
```{r}
library(haven)
medical <- read_sas("medical.sas7bdat")
```
## Check the data
```{r}
dim(medical)
colnames(medical)
attributes(medical)$label
```
## Peek at it
```{r}
head(medical)
```
## See the labels of one column
```{r}
attr(medical$ID, "label")
```
## See the labels of all columns
```{r}
for(i in 1:ncol(medical)){
this_column <- paste(colnames(medical)[i],
attr(medical[[i]], "label"))
message(this_column)
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment