Reputation: 4570
In R, I have used the write.foreign() function from the foreign library in order to write a data frame as a SAS data set.
write.foreign(df = test.df, datafile = 'test.sas7bdat', codefile = 'test.txt', package = "SAS")
The SAS data file is written, but when I try to open it in SAS Viewer 9.1 (Windows XP), I receive the following message - "SAS Data set file format is not supported".
Note: I am generally unfamiliar with SAS, so if an answer exists that would have been known by a regular SAS user, please excuse my ignorance.
Upvotes: 14
Views: 23791
Reputation: 1
I recently ran into a similar issue where I needed to convert some .csv files to SAS7BDAT and XPT. I couldn't find any good examples without SAS. After a bit of experimenting, I found the solution.
Here is a working sample on GitHub: https://github.com/AplosAnalytics/Aplos-File-Conversion-Tools/blob/main/src/export_to_sas.R
This code will read a CSV file to a data frame and then export it to SAS using the haven
library. You don't need SAS, just R and some supporting libraries (see the readme at the repo root).
# a usefull little tool to export .csv files to SAS
library(haven)
main <- function() {
print("Hello R World - Let's export some csv files to SAS!")
current_dir <- getwd()
cat("current directory: ", current_dir, "\n")
if (endsWith(current_dir, "src")) {
current_dir <- dirname(current_dir)
}
files_dir <- file.path(current_dir, "files")
# Normalize the path to make it absolute
files_dir <- normalizePath(files_dir, winslash = "/", mustWork = TRUE)
cat("files directory noralized: ", files_dir, "\n")
output_dir <- file.path(files_dir, ".output")
if (!dir.exists(output_dir)) {
dir.create(output_dir, recursive = TRUE)
}
# Loop through the files input1.csv to input6.csv
for (i in 1:6) {
# Construct input and output file paths
input_file <- file.path(files_dir, paste0("input", i, ".csv"))
sas7bdat_file <- file.path(output_dir, paste0("input", i, ".sas7bdat"))
xpt_file <- file.path(output_dir, paste0("input", i, ".xpt"))
# Check if the input file exists
if (!file.exists(input_file)) {
cat("Skipping missing file:", input_file, "\n")
next
}
# Read the CSV file
df <- read.csv(input_file)
# Write to .sas7bdat, overwriting if the file exists
write_sas(df, sas7bdat_file)
# Write to .xpt_file, overwriting if the file exists
write_xpt(df, xpt_file)
# Print confirmation
cat("Converted:", input_file)
cat("\n\t", "-->", sas7bdat_file, "\n\t -->", xpt_file, "\n")
}
cat("Processing complete.\n")
}
main()
``
Upvotes: -1
Reputation: 171
This is an edit to Hong Ooi's answer.
In R:
library(foreign)
write.foreign(df=test.df, datafile="test.csv", codefile="test.sas", package="SAS")
In SAS:
Upload both test.csv and test.sas files. Open test.sas. You may have to edit the test.sas code that is output from the write.foreign function. What worked for me is updating the INFILE
line to include the library / location:
"/home/kristenmae0/test.csv"
Upvotes: 0
Reputation: 1
You can do it easily with SAS : just have a test with SAS/IML (proc iml) or IMLPlus (object oriented version) with SAS/IML Studio.
or download SAS/IML Studio for free : http://www.sas.com/apps/demosdownloads/92_SDL_sysdep.jsp?packageID=000721 This release of SAS/IML Studio provides the capability to interface with the R language.
Upvotes: -1
Reputation: 57696
write.foreign
with option package="SAS"
actually writes out a comma-delimited text file and then creates a script file with SAS statements to read it in. You have to run SAS and submit the script to turn the text file into a SAS dataset. Your call should look more like
write.foreign(df=test.df, datafile="test.csv", codefile="test.sas", package="SAS")
Note the different extension. Also, write.foreign
writes factor variables as numeric variables with a format controlling their appearance -- ie, the R definition of a factor. If you just want the character representation, you'll have to convert the factors via as.character
before exporting.
Upvotes: 16
Reputation: 72769
I'm not much of a SAS user either, but I've used write.xport()
before and it's worked fine. My crude understanding is that there are two types of SAS files, internal ones and XPORT files. The XPORT ones are the ones that are more compatible across different versions, architectures, etc.
Upvotes: 8