Skip to content
Back to formatted view

Raw Message

Message-ID: <49EF8693.70303@biostat.ku.dk>
Date: 2009-04-22T21:05:23Z
From: Peter Dalgaard
Subject: Count Code
In-Reply-To: <79389F38-7CB4-4564-882D-50B2EEEB1E36@comcast.net>

David Winsemius wrote:
> 
> On Apr 22, 2009, at 3:42 PM, Bronagh Grimes wrote:
> 
>> Hi there,
>> I am interested in converting SAS code to R & I wondered if anyone had
>> any quick R code/tips for the following piece of test SAS code?
>>
>> Any feedback greatly appreciated.
>>
>> data A1;
>>   set A1;
>>  count + 1;
>>  by subject_id;
>>  if first.subject_id then count = 1;
>> run;
> 
> There were several solution offered a couple of months back to the 
> challenge of replicating the features of the SAS _N_ construct. At least 
> that is my memory of how it looks in SAS. You probably have the right 
> books to consult for that question to construct a proper search:

I think this one could be

count <- ave(a1$subject_id, a1$subject_id, FUN = seq_along)

(assuming that "count+1;" increases the value from the previous record.)

-- 
    O__  ---- Peter Dalgaard             ?ster Farimagsgade 5, Entr.B
   c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
  (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)              FAX: (+45) 35327907