[R] FW: R Version Problem in using write.foreign+SAS

Stephen D. Weigand weigand.stephen at charter.net
Thu Dec 21 09:15:01 CET 2006


Shubha,

On Dec 20, 2006, at 10:54 PM, Shubha Vishwanath Karanth wrote:

> Could I find a solution for this? Please please please... How can I
> change the built in code of "foreign:::writeForeignSAS" for the format?

I tend not to change originals if I can help it, so I would create my 
own
version and call it writeForeignSAS2 as follows:

### Make a copy and write it out
writeForeignSAS2 <- foreign:::writeForeignSAS
dump("writeForeignSAS2", "writeForeignSAS2.R")
### Now make desired changes to the new version and then
### source it in
source("writeForeignSAS2.R")

### call write.foreign and specify your particular 'package'
write.foreign(df, datafile, codefile, package = "SAS2", ...)


> -----Original Message-----
> Sent: Wednesday, December 20, 2006 6:06 PM
> To: Shubha Karanth; r-help at stat.math.ethz.ch
> Subject: Re: [R] R Version Problem in using write.foreign+SAS
>
> Hi,
>
> If I am using R 2.4.0, and work with write.foreign command, I get the
> datetime format as 01Dec2006 00:00:00, because the built in function
> (foreign:::writeForeignSAS) has the fixed format for Datetimes and the
> format is "%d%b%Y %H:%M:%S". And that's the reason why I get the
> datetime as 01Dec2006 00:00:00 instead of 2006-12-01 00:00:00.
>
> So, R users, could I make this code flexible to use any datetime
> formats?

Probably not. As of SAS 9.13, the date part of a datetime
that SAS reads in has to be of the form ddmmmyy or ddmmmyyyy. That's
why this format is used in writeForeignSAS.  (I could joke about SAS
saving more flexible datetime formats and informats for version 10.)

Some more specifics below.

> Also where could I see locate this code in the foreign package
> itself?
>
> Thank you,
> Shubha.
>
> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Shubha Karanth
> Sent: 20. joulukuuta 2006 17:15
> To: r-help at stat.math.ethz.ch
> Subject: [R] R Version Problem in using write.foreign+SAS
>
> Hi experts,
>
> I have a problem in Write.foreign command (SAS).
>
> I have a data frame called d.
>
>> d
>
>  Datetime
>
> 2006-12-01 00:00:00
> 2006-12-01 00:10:00
> 2006-12-01 00:20:00
> 2006-12-01 00:30:00
> 2006-12-01 00:40:00
>
>> class(d$Datetime)
>
> [1] "POSIXt"  "POSIXct"
>
> Then I tried with,
>
> write.foreign(d,"Z:\\try_i.sas7bdat"," Z:\\try_i.sas
> ",package="SAS",dataname="g")
>
> If I use R 2.4.0 version I get the below text file generated.
>
> "01Dec2006 00:00:00"
> "01Dec2006 00:10:00"
> "01Dec2006 00:20:00"
> "01Dec2006 00:30:00"
> "01Dec2006 00:40:00"

This is the only format for datetime variables SAS will understand.

> If I use R 2.2.0 version I get the below text file generated.
>
> 2006-12-01 00:00:00
> 2006-12-01 00:10:00
> 2006-12-01 00:20:00
> 2006-12-01 00:30:00
> 2006-12-01 00:40:00

In earlier versions of writeForeignSAS, the 'datafile' was written
out this way but the 'codefile' didn't treat the variable as a
datetime variable.

If you want the data written out in a certain format, you can
convert the variable to character with

d$mydatetime <- format(d$mydatetime)

But this means that the 'codefile' will tell SAS to read
the variable in as a character variable.

>
> But I need to use R 2.4.0 and get the text file generated by R 2.2.0.
>
> Could somebody help me on this?
>
> Thank you.
>


Stephen Weigand
Rochester, Minnesota, USA



More information about the R-help mailing list