First of all, you should make sure that your data is ordered by patient id and by STARTENC in order to be able to compare one line to the next.
Secondly, you cannot just compare one line to the next as the two lines might be for different patients.
In R, one way of getting the result would be to use the data.table
package.
I demonstrate the solution in the code below using a tiny dataset where you have 4 patients, and 6 encounters. Only patient A had to come back within 90 days after some encounter.
library(data.table)
# build fake data
ids <- c('D', 'C', 'A', 'A', 'A', 'B')
starts <- c('2008-01-01', '2007-04-02', '2006-02-11', '2006-03-15', '2009-02-01', '2009-03-17')
# making sure it is ordered correctly
data <- data.table(Id=ids, STARTENC=starts)[order(Id, STARTENC)]
# converting strings to Date
data[, STARTENC := as.Date(STARTENC)]
# for every patient and encounter building a column that will contain the
# date of the NEXT patient's encounter. THe by=.(Id) is important here
data[, NEXTENC := shift(STARTENC, type = 'lead'), by=.(Id)]
# computing the delta between an encounter and the next
data[, DELTA := difftime(NEXTENC, STARTENC)]
# counting how many lines have a delta below 90
data[DELTA <= 90, .N]