Created
June 12, 2023 22:07
-
-
Save jmclawson/f4dafffd3f657b09b0192cafe270e9d3 to your computer and use it in GitHub Desktop.
from SAS to R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
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