由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Statistics版 - 求助:Import .sas7bdat to R
相关主题
R 小问题how to combine characters and numric variable in "where" clause by SAS?urgent!
有技巧得用R才能发挥它的威力SAS 怎样在Character value 里面输入numerical value
用R灌水的终极利器。SAS Base 50题中的30和33题疑问
【R】关于R的variable type求SAS code
R: 怎么读入某个field中含有comma的文件?问个SAS问题
SAS data merge求助SAS问题求助
[合集] SAS的一个问题Import excel file to sas (the first 8 or more observations
请教2个sas base问题sas question
相关话题的讨论汇总
话题: sas7bdat话题: numeric话题: factor话题: character话题: import
进入Statistics版参与讨论
1 (共1页)
D*********2
发帖数: 535
1
data structure:
1 col of character
3 cols of numeric
tried following ways:
1) .sas7bdat to .csv, then read.csv
problem: character be trimmed, eg. 007 to 7. I really need the trial number
to be 3 digits.
2) .sas7bdat to .txt, then read.table
problem: numeric variables be read as factor, say, 167.400, after using as.
numeric, it became 1674.
I also tried colClasses, but unless set the numeric cols to "factor" or "
character", there is an error message.
Thanks a lot!!!!!!!!!!
P****D
发帖数: 11146
2
read.ssd in foreign should do it. I don't have any code with me right now,
but the page below should help you:
http://n4.nabble.com/Reading-sas7bdat-files-directly-td1469515.html
D*******a
发帖数: 207
3

number
Just use colClass to read as "character", then you will have 007.
1. If you can not read other than "factor" or "character, this is because
you have non-numeric entries in this column. For example, "Missing", "-",
etc. Find it out and correct it.
2.You can't use as.numeric(factor) in this case. This function will return
the coded level (very dangerous! You will be burnt by this in the future, if
you are not careful). For example,
as.numeric(factor(c("a",2)))
[1] 2 1
You got 1674 because

【在 D*********2 的大作中提到】
: data structure:
: 1 col of character
: 3 cols of numeric
: tried following ways:
: 1) .sas7bdat to .csv, then read.csv
: problem: character be trimmed, eg. 007 to 7. I really need the trial number
: to be 3 digits.
: 2) .sas7bdat to .txt, then read.table
: problem: numeric variables be read as factor, say, 167.400, after using as.
: numeric, it became 1674.

D*********2
发帖数: 535
4
Thanks a lot!!!!!
Yes, I do have missing values in the three numeric variables, denoted as "."
, Should I correct it in SAS before importing them into R, like recoded it
is "999"? Is it the common way to do this? Because I was thinking we may
need to avoid hard coding in SAS.
Besides, I doubt it is caused by missing value, here is the error message I
got after read.table
Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,
scan() expected 'a real', got '"117.200"'
Thanks
D*********2
发帖数: 535
5
Get the idea. Thanks!

【在 P****D 的大作中提到】
: read.ssd in foreign should do it. I don't have any code with me right now,
: but the page below should help you:
: http://n4.nabble.com/Reading-sas7bdat-files-directly-td1469515.html

D*******a
发帖数: 207
6

."
I

You can change the dot to NA, then R can recognize it as NA. 999 is not a
good idea, as later on you may confuse it with valid numbers.
Your R code complained maybe because you used quoted txt file. I guess your number is "117.200" (quoted by "") other than 117.200 (not quoted by ""). Take a look of your .csv file (using a text editor).
In my previous answer I answered your first choice too (csv). Did you try to read 007 as character?

【在 D*********2 的大作中提到】
: Thanks a lot!!!!!
: Yes, I do have missing values in the three numeric variables, denoted as "."
: , Should I correct it in SAS before importing them into R, like recoded it
: is "999"? Is it the common way to do this? Because I was thinking we may
: need to avoid hard coding in SAS.
: Besides, I doubt it is caused by missing value, here is the error message I
: got after read.table
: Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,
: scan() expected 'a real', got '"117.200"'
: Thanks

D*********2
发帖数: 535
7
Yes, it works when I use
> dat.corr <- read.csv(file="xxx.csv", colClasses=rep("factor", 4), T)
However, I did not notice, even use read.csv, it gave me "factor". Why?
I simply did import to .csv in SAS.
> dat.corr <- read.csv(file="xxx.csv", T)
> sapply(dat.corr, class)
Y X1 X2 X3
"integer" "factor" "factor" "factor"
Y is the one should be factor, say, "007". X1:X3 should be numeric.
Thanks again!
s**c
发帖数: 1247
8
sometimes I used a fake observation at the first line
them delete it

【在 D*********2 的大作中提到】
: Yes, it works when I use
: > dat.corr <- read.csv(file="xxx.csv", colClasses=rep("factor", 4), T)
: However, I did not notice, even use read.csv, it gave me "factor". Why?
: I simply did import to .csv in SAS.
: > dat.corr <- read.csv(file="xxx.csv", T)
: > sapply(dat.corr, class)
: Y X1 X2 X3
: "integer" "factor" "factor" "factor"
: Y is the one should be factor, say, "007". X1:X3 should be numeric.
: Thanks again!

D******n
发帖数: 2836
9
for 1) u can easily convert it
data$v1<-sprintf("%03d",data$v1)

number

【在 D*********2 的大作中提到】
: data structure:
: 1 col of character
: 3 cols of numeric
: tried following ways:
: 1) .sas7bdat to .csv, then read.csv
: problem: character be trimmed, eg. 007 to 7. I really need the trial number
: to be 3 digits.
: 2) .sas7bdat to .txt, then read.table
: problem: numeric variables be read as factor, say, 167.400, after using as.
: numeric, it became 1674.

D*******a
发帖数: 207
10

First change the dots to NA in the X1,X2,X3 columns. Then:
dat.corr <- read.csv(file="xxx.csv", colClass=c("character",rep("numeric",3)
), T)

【在 D*********2 的大作中提到】
: Yes, it works when I use
: > dat.corr <- read.csv(file="xxx.csv", colClasses=rep("factor", 4), T)
: However, I did not notice, even use read.csv, it gave me "factor". Why?
: I simply did import to .csv in SAS.
: > dat.corr <- read.csv(file="xxx.csv", T)
: > sapply(dat.corr, class)
: Y X1 X2 X3
: "integer" "factor" "factor" "factor"
: Y is the one should be factor, say, "007". X1:X3 should be numeric.
: Thanks again!

相关主题
SAS data merge求助how to combine characters and numric variable in "where" clause by SAS?urgent!
[合集] SAS的一个问题SAS 怎样在Character value 里面输入numerical value
请教2个sas base问题SAS Base 50题中的30和33题疑问
进入Statistics版参与讨论
D*********2
发帖数: 535
11
Thanks, problem solved!
I really appreciate your help :-)

3)

【在 D*******a 的大作中提到】
:
: First change the dots to NA in the X1,X2,X3 columns. Then:
: dat.corr <- read.csv(file="xxx.csv", colClass=c("character",rep("numeric",3)
: ), T)

D*******a
发帖数: 207
12
You are welcome.
D*********2
发帖数: 535
13
Thanks. Although the problem is solved. I am still interested in this read.
ssd. I am so confused, would you please give me a hand?
I tried the example in the help document for windows,
read.ssd(file.path(sashome, "core", "sashelp"), "retail",
sascmd = file.path(sashome, "sas.exe"))
no problem, successfully imported.
But back to my case,
statdat <- "/Users/Dreamer1122/Documents/My SAS Files/9.2"
read.ssd(file.path(statdat), "dat_corr_03232010", sascmd = file.path(sashome
, "sas.exe

【在 P****D 的大作中提到】
: read.ssd in foreign should do it. I don't have any code with me right now,
: but the page below should help you:
: http://n4.nabble.com/Reading-sas7bdat-files-directly-td1469515.html

D*********2
发帖数: 535
14
Thanks, works well for 1).

【在 D******n 的大作中提到】
: for 1) u can easily convert it
: data$v1<-sprintf("%03d",data$v1)
:
: number

P****D
发帖数: 11146
15
1. Did you specify sashome correctly?
2. I'd suggest you specify the full path for your dataset in the first file.
path option.
This read.ssd command works fine on my 32-bit XP with SAS 9.2.

【在 D*********2 的大作中提到】
: Thanks. Although the problem is solved. I am still interested in this read.
: ssd. I am so confused, would you please give me a hand?
: I tried the example in the help document for windows,
: read.ssd(file.path(sashome, "core", "sashelp"), "retail",
: sascmd = file.path(sashome, "sas.exe"))
: no problem, successfully imported.
: But back to my case,
: statdat <- "/Users/Dreamer1122/Documents/My SAS Files/9.2"
: read.ssd(file.path(statdat), "dat_corr_03232010", sascmd = file.path(sashome
: , "sas.exe

P****D
发帖数: 11146
16
Oh, and you do need to have full function SAS (SAS universal viewer will not
suffice) installed on your computer to make this read.ssd work. I don't
know if you can get it to work with WPS.

【在 D*********2 的大作中提到】
: Thanks. Although the problem is solved. I am still interested in this read.
: ssd. I am so confused, would you please give me a hand?
: I tried the example in the help document for windows,
: read.ssd(file.path(sashome, "core", "sashelp"), "retail",
: sascmd = file.path(sashome, "sas.exe"))
: no problem, successfully imported.
: But back to my case,
: statdat <- "/Users/Dreamer1122/Documents/My SAS Files/9.2"
: read.ssd(file.path(statdat), "dat_corr_03232010", sascmd = file.path(sashome
: , "sas.exe

1 (共1页)
进入Statistics版参与讨论
相关主题
sas questionR: 怎么读入某个field中含有comma的文件?
sas importing questionSAS data merge求助
请教两个关于SAS的问题[合集] SAS的一个问题
如何读以.sas7bdat结尾的FILE请教2个sas base问题
R 小问题how to combine characters and numric variable in "where" clause by SAS?urgent!
有技巧得用R才能发挥它的威力SAS 怎样在Character value 里面输入numerical value
用R灌水的终极利器。SAS Base 50题中的30和33题疑问
【R】关于R的variable type求SAS code
相关话题的讨论汇总
话题: sas7bdat话题: numeric话题: factor话题: character话题: import