[R] How to read only ten rows from a SAS dataset (read.ssd)?

David Winsemius dwinsemius at comcast.net
Tue Oct 19 19:42:00 CEST 2010


On Oct 19, 2010, at 1:31 PM, johannes rara wrote:

> Thanks David,
>
> Yes, my code really works (using the foreign package), but when
> handling a SAS file which contains > 500 000 rows and > 100 cols it is
> not really fun anymore. My intention was do some preliminary research
> from the data and the whole dataset was not needed.
>
> After all, I could not find a possibility to get limited amount of
> rows from a dataset when importing data to R.

Which was why I suggested that you explore the possibilities offered by:

-- Hmisc::sas.get with a suitable if= statement

I seem to remember there is a SAS internal variable named something  
like _N_ that is a "line number". Perhaps

... ,  if="_N_ <= 10",

OR:
-- using SAS to output a smaller file.

OR:

--- adding a SAS line to the output returned within the read.ssd  
function.

-- 
David.


> -J
>
> 2010/10/19 David Winsemius <dwinsemius at comcast.net>:
>>
>> On Oct 19, 2010, at 6:47 AM, johannes rara wrote:
>>
>>> I'm trying to read SAS datasets on Windows:
>>>
>>> sashome <- "C:/Program Files/SAS/SAS 9.1"
>>> fold <- "C:/temp"
>>> g <- read.ssd(fold, "sasfile", sascmd = file.path(sashome,  
>>> "sas.exe"))
>>
>> And this was successful?
>>
>>>
>>> How to get only e.g first ten rows into R?
>>
>> Presumably you also entered require(foreign) if you had success.  
>> Looking at
>> the help page, we see no parameter that would effect such a result.  
>> So just
>> type:
>>
>> read.ssd
>>
>> You see that this function's code is available and if you know SAS,  
>> you
>> should be able to insert the needed line that would limit the  
>> dataset length
>> to only ten lines. I'm not being coy. I would probably had further
>> suggestions 20 years ago when I was using SAS.
>>
>> There is a function sas.get in package Hmisc that offers more  
>> extensive
>> control, but it is not clear to me on looking at the parameters  
>> whether your
>> particular request would be easily accommodated. The ifs= parameter  
>> would
>> appear to be the most promising candidate to me. It appears that  
>> these file
>> formats are accepted:
>> sasds.suffix <- c("sd2", "sd7", "ssd01", "ssd02", "ssd03",
>>        "ssd04", "sas7bdat")
>>
>>
>> Also, since the use of read.ssd implies that you have a working  
>> copy of SAS,
>> then another option is simply exporting a file in the format of  
>> your choice?
>> The SAS XPT format seems to be well handled by external programs.
>>
>>
>> --
>> David.
>>>
>>> -J
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>
>> David Winsemius, MD
>> West Hartford, CT
>>
>>

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list