|
Another approach directly inspired by "look ahead and look back" technique.
data want;
set temp;
_prev1=lag(name);
_prev2=lag2(name);
set temp(firstobs=2 keep=name rename=name=_next1) temp(obs=1 drop=_all_);
set temp(firstobs=3 keep=name rename=name=_next2) temp(obs=2 drop=_all_);
run;
Regards,
Haikuo
-----Original Message-----
From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of Jay Smith
Sent: Tuesday, August 23, 2011 8:19 PM
To: SAS-L@LISTSERV.UGA.EDU
Subject: Get 2 records before and records after
Dear all,
Suppose you have a dataset that might look like:
DATA temp;
INPUT name x rank;
DATALINES;
ABC123 34 1
CDF234 14 2
FEG343 19 3
EGE473 27 4
GAS308 52 5
PIE343 11 6
MYD987 34 7
COL138 40 8
HAK989 47 9
FRF879 35 10
;
RUN;
I'd like to identify a set of "groups" associated with each record. The rule
is that the groups are made up of the 2 records below it and the 2 records
above it (data is already sorted appropriately by rank) So for example,
PIE343belongs in a group with MYD987, COL138, GAS308, and EGE473. Upon
identifying those groups members, I would like to add them to the dataset
for that record so the PIE343 record would look something like:
(name) (x) (rank) (group1) (group2) (group3) (group4)
PIE343 11 6 EGE473 GAS308 MYD987 COL138
Any help would be greatly appreciated. Many thanks in advance.
-J
-----------------------------------------
Email messages cannot be guaranteed to be secure or error-free as
transmitted information can be intercepted, corrupted, lost,
destroyed, arrive late or incomplete, or contain viruses. The
Centers for Medicare & Medicaid Services therefore does not accept
liability for any error or omissions in the contents of this
message, which arise as a result of email transmission.
CONFIDENTIALITY NOTICE: This communication, including any
attachments, may contain confidential information and is intended
only for the individual or entity to which it is addressed. Any
review, dissemination, or copying of this communication by anyone
other than the intended recipient is strictly prohibited. If you
are not the intended recipient, please contact the sender by reply
email and delete and destroy all copies of the original message.
|