6

我最近下载了一些 ASCII 格式的数据,这些数据与我想与 R 一起使用的 SAS 设置文件一起使用。这样的数据文件如下:

https://dl.dropboxusercontent.com/u/8474088/Data.txt

在此处使用相应的 SAS 设置文件:

https://dl.dropboxusercontent.com/u/8474088/Setup.sas

我应该注意,设置文件旨在处理大约 50 个具有相似结构的不同数据文件(上面的链接是其中之一的示例)。

在找到 SAScii 包后,我认为我状态良好,但无法读取 read.SAScii 或 parse.SAScii 来处理这些文件。任一命令都会出错。

read.SAScii(data.file,setup.file,beginline=581)

Error in if (as.numeric(x[j, "start"]) > as.numeric(x[j - 1, "end"]) +  : 
  missing value where TRUE/FALSE needed
In addition: Warning message:
NAs introduced by coercion 

parse.SAScii(setup.file,beginline=581)

Error in if (as.numeric(x[j, "start"]) > as.numeric(x[j - 1, "end"]) +  : 
  missing value where TRUE/FALSE needed
In addition: Warning message:
NAs introduced by coercion 

SAScii 文档中给出的示例使用了更简单的设置文件,所以我想知道上述文件的复杂性是否导致了问题(例如,在 INPUT 命令之前的文件中列出的有关 VALUE 的信息)。

关于如何进行的任何想法都会很棒。提前致谢。

4

1 回答 1

4

parse.SAScii 帮助的详细信息部分所述,此包无法读取重叠列......并且您的文件显然有它们。;) 为了让 SAScii 工作,您必须在硬盘上将.sas文件分成四个单独的文件。.sas就是这样-

# load all necessary libraries
library(stringr)
library(SAScii)
library(downloader)

# create two temporary files
tf <- tempfile()
tf2 <- tempfile()

# download the sas import script
download( "https://dl.dropboxusercontent.com/u/8474088/Setup.sas" , tf )

# download the actual data file
download( "https://dl.dropboxusercontent.com/u/8474088/Data.txt" , tf2 )

# read the sas importation instructions into R
z <- readLines( tf )

# here are the break points
z[ substr( str_trim( z ) , 1 , 1 ) == '#' ]

sas.script.breakpoints <- which( substr( str_trim( z ) , 1 , 1 ) == '#' )

script.one <- z[ 581:sas.script.breakpoints[1] ]
script.two <- z[ sas.script.breakpoints[1]:sas.script.breakpoints[2] ]
script.three <- z[ sas.script.breakpoints[2]:sas.script.breakpoints[3] ]
script.four <- z[ sas.script.breakpoints[3]:length(z) ]

# replace some stuff so these look like recognizable sas scripts
script.one[ length( script.one ) ] <- ";"

script.two[ 1 ] <- "input blank 1-300"
script.two[ length( script.two ) ] <- ";"

script.three[ 1 ] <- "input blank 1-300"
script.three[ length( script.three ) ] <- ";"

script.four[ 1 ] <- "input blank 1-300"

# test then import data set one
writeLines( script.one , tf )
parse.SAScii( tf )
x1 <- read.SAScii( tf2 , tf )

# test then import data set two
writeLines( script.two , tf )
parse.SAScii( tf )
x2 <- read.SAScii( tf2 , tf )

# test then import data set one
writeLines( script.three , tf )
parse.SAScii( tf )
x3 <- read.SAScii( tf2 , tf )

# test then import data set four
writeLines( script.four , tf )
parse.SAScii( tf )
x4 <- read.SAScii( tf2 , tf )
于 2013-05-24T22:08:30.803 回答