1
votes

I have a .csv file generated by R on a Mac. I'd like to import the dataset into SAS in Windows XP. Note that there are many missing values in my .csv file (NA's). How could I import the dataset into SAS without them? I'd like SAS to change all NA's automatically to ".", but it is often difficult even to get Mac-generated .csv files into SAS 9.3 at all.

I tried "write.dbf()" from the foreign library and I generated a .dbf file but unfortunately, SAS DBMS does not know .dbf.

3
I don't know R at all, but one of your comments mentioned problems using a MAC generated CSV file on your Windows machine. That is because a CSV file is just a plain text file and plain text files are encoded differently between UNIX, MAC, and Windows. To have SAS read a text file that was created on a MAC, add the option TERMSTR=CR to your INFILE statement.BellevueBob

3 Answers

4
votes

In the R write.table function, there is an argument na="NA" Change this to na="."

You could also just use find and replace in excel.

4
votes

I'm not sure how to do that as you're reading in the data, but you should be able to use a normal infile statement, then an if/then. Something like:

DATA new;
infile 'C:\myfolder\mydataset.csv' dlm=',' firstobs=2;
if newvar1='NA' then newvar1=.;

If you give me more details about your dataset, I may be able to help you more.

I also wonder if you could get the same effect by replacing the NAs with ' ' or numeric(0) in R before exporting it to send to SAS? IMO R is more flexible, so I prefer to do my data manipulation there.

3
votes

You can use R to write an SAS XPORT formatted file. See write.xport in the SASxport package.