|
|
How to delete rows in a sas dataset
Hello,
So I have a longitudinal dataset with multiple observations.
One variables is whether subject had surgery at a certain time point:
character categorical variable (Yes, No).
So the observation is Y if the person had surgery and the rest are N.
Since I do not want to look at independent variables after surgery. I
want to delete all rows after the variable hits a Y. observations are
coded as N after surgery, as such if a Y is recorded I would like to
delete that along with the N at the rest of the time points for a
certain subject. Please look at how it looks below:
subject sx
1 N
1 N
1 Y how to Delete rows from here on for subject 1
1 N
1 N
2 N
2 Y how to Delete rows from here on for subject 2
2 N
3 N
3 N
3 Y how to Delete rows from here on for subject 3
3 N
3 N
|
|
0
|
|
|
|
Reply
|
jiji
|
3/1/2010 9:58:51 PM |
|
"jiji" <joelleahallak@gmail.com> wrote in message
news:c97d19f4-cbec-444f-927b-c7a444479944@x22g2000yqx.googlegroups.com...
> Hello,
>
> So I have a longitudinal dataset with multiple observations.
> One variables is whether subject had surgery at a certain time point:
> character categorical variable (Yes, No).
> So the observation is Y if the person had surgery and the rest are N.
>
> Since I do not want to look at independent variables after surgery. I
> want to delete all rows after the variable hits a Y. observations are
> coded as N after surgery, as such if a Y is recorded I would like to
> delete that along with the N at the rest of the time points for a
> certain subject. Please look at how it looks below:
>
> subject sx
> 1 N
> 1 N
> 1 Y how to Delete rows from here on for subject 1
> 1 N
> 1 N
> 2 N
> 2 Y how to Delete rows from here on for subject 2
> 2 N
> 3 N
> 3 N
> 3 Y how to Delete rows from here on for subject 3
> 3 N
> 3 N
Presumably, your data are sorted by subject and timepont. In that case,
something like this (untested) code should do:
data fee;
set fie;
by subject;
if first.subject then dleet = 0;
if sx = 'Y' then dleet = 1;
if dleet then delete;
run;
|
|
0
|
|
|
|
Reply
|
Lou
|
3/2/2010 12:59:50 AM
|
|
There is probably an easier way, but I think that the following will
do what you want:
data have;
input subject sx $;
cards;
1 N
1 N
1 Y
1 N
1 N
2 N
2 Y
2 N
3 N
3 N
3 Y
3 N
3 N
;
data want;
set have;
retain keep_going;
by subject;
if first.subject then keep_going=1;
if keep_going eq 1 then output;
if trim(left(sx)) eq "Y" then keep_going=0;
run;
HTH,
Art
-------------
On Mar 1, 4:58 pm, jiji <joelleahal...@gmail.com> wrote:
> Hello,
>
> So I have a longitudinal dataset with multiple observations.
> One variables is whether subject had surgery at a certain time point:
> character categorical variable (Yes, No).
> So the observation is Y if the person had surgery and the rest are N.
>
> Since I do not want to look at independent variables after surgery. I
> want to delete all rows after the variable hits a Y. observations are
> coded as N after surgery, as such if a Y is recorded I would like to
> delete that along with the N at the rest of the time points for a
> certain subject. Please look at how it looks below:
>
> subject sx
> 1 N
> 1 N
> 1 Y how to Delete rows from here on for subject 1
> 1 N
> 1 N
> 2 N
> 2 Y how to Delete rows from here on for subject 2
> 2 N
> 3 N
> 3 N
> 3 Y how to Delete rows from here on for subject 3
> 3 N
> 3 N
|
|
0
|
|
|
|
Reply
|
art297
|
3/2/2010 1:03:21 AM
|
|
> -----Original Message-----
> From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of Lou
> Sent: Monday, March 01, 2010 5:00 PM
> To: SAS-L@LISTSERV.UGA.EDU
> Subject: Re: How to delete rows in a sas dataset
>
> "jiji" <joelleahallak@gmail.com> wrote in message
> news:c97d19f4-cbec-444f-927b-
> c7a444479944@x22g2000yqx.googlegroups.com...
> > Hello,
> >
> > So I have a longitudinal dataset with multiple observations.
> > One variables is whether subject had surgery at a certain time point:
> > character categorical variable (Yes, No).
> > So the observation is Y if the person had surgery and the rest are N.
> >
> > Since I do not want to look at independent variables after surgery. I
> > want to delete all rows after the variable hits a Y. observations are
> > coded as N after surgery, as such if a Y is recorded I would like to
> > delete that along with the N at the rest of the time points for a
> > certain subject. Please look at how it looks below:
> >
> > subject sx
> > 1 N
> > 1 N
> > 1 Y how to Delete rows from here on for subject 1
> > 1 N
> > 1 N
> > 2 N
> > 2 Y how to Delete rows from here on for subject 2
> > 2 N
> > 3 N
> > 3 N
> > 3 Y how to Delete rows from here on for subject 3
> > 3 N
> > 3 N
>
> Presumably, your data are sorted by subject and timepont. In that case,
> something like this (untested) code should do:
>
> data fee;
> set fie;
> by subject;
> if first.subject then dleet = 0;
> if sx = 'Y' then dleet = 1;
> if dleet then delete;
> run;
You will need to add a retain statement to the data step (add it anywhere);
retain dleet;
Hope this is helpful,
Dan
Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204
|
|
0
|
|
|
|
Reply
|
NordlDJ
|
3/2/2010 1:52:40 AM
|
|
hi ... yet another idea (with the no+1 versus no=1, you can leave out the RETAIN statement) ...
data x;
input subject sx : $1.;
datalines;
1 N
1 N
1 Y
1 N
1 N
2 N
2 Y
2 N
3 N
3 N
3 Y
3 N
3 N
;
run;
data y;
set x;
by subject;
if first.subject then no=0;
if sx eq 'Y' then no+1;
if ^no;
drop no;
run;
--
Mike Zdeb
U@Albany School of Public Health
One University Place
Rensselaer, New York 12144-3456
P/518-402-6479 F/630-604-1475
> Hello,
>
> So I have a longitudinal dataset with multiple observations.
> One variables is whether subject had surgery at a certain time point:
> character categorical variable (Yes, No).
> So the observation is Y if the person had surgery and the rest are N.
>
> Since I do not want to look at independent variables after surgery. I
> want to delete all rows after the variable hits a Y. observations are
> coded as N after surgery, as such if a Y is recorded I would like to
> delete that along with the N at the rest of the time points for a
> certain subject. Please look at how it looks below:
>
> subject sx
> 1 N
> 1 N
> 1 Y how to Delete rows from here on for subject 1
> 1 N
> 1 N
> 2 N
> 2 Y how to Delete rows from here on for subject 2
> 2 N
> 3 N
> 3 N
> 3 Y how to Delete rows from here on for subject 3
> 3 N
> 3 N
>
|
|
0
|
|
|
|
Reply
|
msz03
|
3/2/2010 1:49:05 PM
|
|
|
4 Replies
1502 Views
(page loaded in 0.149 seconds)
|
|
|
|
|
|
|
|
|