Date: Mon, 27 Apr 2009 17:47:23 -0400
Reply-To: Mike Rhoads <RHOADSM1@WESTAT.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Mike Rhoads <RHOADSM1@WESTAT.COM>
Subject: Re: findingout consecutive days
In-Reply-To: <1b4d9f80-43ea-4c92-ad6a-f5abd0799566@f1g2000prb.googlegroups.com>
Content-Type: text/plain; charset="us-ascii"
Jing,
The way I am thinking of this, you need to look at 3 records at the same time: the current record, the previous record, and the next record. This is actually similar to another recent SAS-L thread.
Once you get that set up, you can set the flag according to your requirements pretty easily:
if difday <= 30 AND (prev_difday <= 30 OR next_difday < = 30)
then flag = 'Y';
else flag = 'N';
The DATA step below uses the LAG function to get the values from the previous record and a lookahead merge to get the values from the next record. We also need a little code to make sure that the previous/next values are not used to set a Y unless they are for the same ID.
OPTIONS MERGENOBY = NOWARN;
data two;
merge one
one (FIRSTOBS=2 RENAME=(id=next_id difday=next_difday));
* BY statement intentionally omitted for lookahead merge;
length flag $ 1;
keep id difday flag;
prev_id = lag(id);
prev_difday = lag(difday);
if id NE prev_id then prev_difday = 999999; /* dummy value > 30 */
if id NE next_id then next_difday = 999999; /* dummy value > 30 */
if difday <= 30 AND (prev_difday <= 30 OR next_difday < = 30)
then flag = 'Y';
else flag = 'N';
run;
OPTIONS MERGENOBY = ERROR;
Hope this helps.
Mike Rhoads
RhoadsM1@Westat.com
-----Original Message-----
From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of jingtailan@gmail.com
Sent: Monday, April 27, 2009 4:34 PM
To: SAS-L@LISTSERV.UGA.EDU
Subject: findingout consecutive days
All;
I am working on the following question and can't get a correct code, please help. thanks a lot.
Jing tailan.
================================================================
data one;
input id difday;
datalines;
101 0
101 47
101 27
102 0
102 17
103 0
103 50
104 0
104 12
104 13
105 0
105 15
105 10
105 60
106 0
106 40
106 15
106 15
107 0
107 52
107 51
107 49
;
===================================================================
QUESTION: how to find out the subjid who has CONSECUTIVE difday with is <=30:
desired result:
101 0 n
101 47 n
101 27 n
102 0 y
102 17 y
103 0 n
103 50 n
104 0 y
104 12 y
104 13 y
105 0 y
105 15 y
105 10 y
105 60 n
106 0 n
106 40 n
106 15 y
106 15 y
107 0 n
107 52 n
107 51 n
107 49 n