R

R 讀取 SAS7BDAT 格式 SAS 檔案教學與範例

介紹如何在 R 語言中使用 sas7bdat 套件讀取 SAS7BDAT 格式的 SAS 檔案。

安裝 sas7bdat 套件

sas7bdat 套件 可以從官方的 CRAN 套件庫直接下載安裝:

# 安裝 sas7bdat 套件
install.packages("sas7bdat")

讀取 SAS7BDAT 格式 SAS 檔案

這裡我們以 airline.sas7bdat 這個檔案來示範如何使用 R 的 sas7bdat 套件來讀取 SAS7BDAT 格式 SAS 檔案。

在引入 sas7bdat 套件之後,呼叫 read.sas7bdat 讀取 SAS7BDAT 格式的 SAS 檔案:

# 引入 sas7bdat 套件
library(sas7bdat)

# 讀取 SAS7BDAT 格式的 SAS 檔案
df <- read.sas7bdat("airline.sas7bdat")
head(df)
  YEAR     Y     W      R     L     K
1 1948 1.214 0.243 0.1454 1.415 0.612
2 1949 1.354 0.260 0.2181 1.384 0.559
3 1950 1.569 0.278 0.3157 1.388 0.573
4 1951 1.948 0.297 0.3940 1.550 0.564
5 1952 2.265 0.310 0.3559 1.802 0.574
6 1953 2.731 0.322 0.3593 1.926 0.711

透過 read.sas7bdat 所產生的 data frame 除了資料本身之外,還包含了許多屬性資料,我們可以使用 attributes 將這些屬性資料取出:

# 取得各類屬性資料
df.attr <- attributes(df)

names 屬性包含了所有的變數名稱:

# 變數名稱
df.attr$names
[1] "YEAR" "Y"    "W"    "R"    "L"    "K"

SAS 變數的屬性可以透過 column.info 來取得:

# 變數屬性
for (ci in df.attr$column.info) {
  cat("name:", ci$name, "\n")
  cat("  offset:", ci$offset, "\n")
  cat("  length:", ci$length, "\n")
  cat("  type:", ci$type, "\n")
  cat("  format:", ci$format, "\n")
  cat("  label:", ci$label, "\n")
}
name: YEAR
  offset: 0
  length: 4
  type: numeric
  format:
  label: year
name: Y
  offset: 4
  length: 8
  type: numeric
  format:
  label: level of output
name: W
  offset: 12
  length: 8
  type: numeric
  format:
  label: wage rate
name: R
  offset: 20
  length: 8
  type: numeric
  format:
  label: interest rate
name: L
  offset: 28
  length: 8
  type: numeric
  format:
  label: labor input
name: K
  offset: 36
  length: 8
  type: numeric
  format:
  label: capital input

亦可搭配 plyr 套件將變數屬性整理成 data frame:

# 將變數屬性整理成 data frame 格式
library(plyr)
ci.df <- rbind.fill(lapply(df.attr$column.info, function(f) {
  as.data.frame(Filter(Negate(is.null), f))
}))
ci.df
  name offset length    type fhdr foff flen           label lhdr loff llen
1 YEAR      0      4 numeric    0    0    0            year    0   32    4
2    Y      4      8 numeric    0    0    0 level of output    0   40   15
3    W     12      8 numeric    0    0    0       wage rate    0   60    9
4    R     20      8 numeric    0    0    0   interest rate    0   76   13
5    L     28      8 numeric    0    0    0     labor input    0   96   11
6    K     36      8 numeric    0    0    0   capital input    0  112   13

批次處理大量 SAS7BDAT 格式 SAS 檔案

若需要一次將一個目錄下的所有 SAS7BDAT 格式 SAS 檔案都轉為 CSV 檔案,同時將變數屬性也一併轉出,可以使用以下指令稿:

library(sas7bdat)
library(plyr)

# 大量 SAS7BDAT 檔案所在目錄
folder = "my_sas7bdat_folder"

# 列出所有 SAS7BDAT 檔案
sas7bdat.files <- list.files(path = folder, pattern = "sas7bdat$")

for (f in sas7bdat.files) {
  # 來源 SAS7BDAT 檔案
  sas7bdat.path = file.path(folder, f)
  cat("Source:", sas7bdat.path, "\n")

  # 資料輸出 CSV 檔案
  data.path = sub(".sas7bdat", "_data.csv", sas7bdat.path)
  cat("Data Output:", data.path, "\n")

  # 變數屬性輸出 CSV 檔案
  var.attr.path = sub(".sas7bdat", "_attr.csv", sas7bdat.path)
  cat("Variable Attributes Output:", var.attr.path, "\n")

  # 讀取並輸出資料
  data.df <- read.sas7bdat(sas7bdat.path)
  write.csv(data.df, file = data.path, fileEncoding = "UTF-8")

  # 讀取並輸出變數屬性
  attr.df <- attributes(data.df)
  ci.df <- rbind.fill(lapply(attr.df$column.info, function(f) {
    as.data.frame(Filter(Negate(is.null), f))
  }))
  write.csv(ci.df, file = var.attr.path, fileEncoding = "UTF-8")
}

這裡在匯出 CSV 檔案時,使用的 fileEncodingUTF-8,若後續要在 Windows 系統下處理資料,建議改為 Big5 編碼(或是省略不指定 fileEncoding,預設則為 Big5 編碼),這樣可以避免以 Excel 直接開啟時出現亂碼的問題。

Share
Published by
Office Guide

Recent Posts

Python 使用 PyAutoGUI 自動操作滑鼠與鍵盤

本篇介紹如何在 Python ...

9 個月 ago

Ubuntu Linux 以 WireGuard 架設 VPN 伺服器教學與範例

本篇介紹如何在 Ubuntu ...

9 個月 ago

Linux 網路設定 ip 指令用法教學與範例

本篇介紹如何在 Linux 系...

9 個月 ago

Windows 使用 TPM 虛擬智慧卡保護 SSH 金鑰教學與範例

本篇介紹如何在 Windows...

11 個月 ago

Linux 以 Shamir’s Secret Sharing 分割保存金鑰教學與範例

介紹如何在 Linux 中使用...

11 個月 ago

Linux 以 Cryptsetup、LUKS 加密 USB 隨身碟教學與範例

介紹如何在 Linux 系統中...

11 個月 ago