- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi there!
Novice SAS user... looking for some help...
I have a large data set that is set up similar to this super simplified example below:
Student ID Date of Test Test Type
A 01/01/00 3
A 02/01/00 4
A 04/01/00 2
B 02/01/00 1
B 03/01/00 2
C 02/01/00 2
C 03/01/00 4
C 04/01/00 4
C 05/01/00 1
D 04/01/00 3
D 05/01/00 2
D 06/01/00 1
D 06/02/00 2
Within each student ID, I am interested in the first occurrence of test type 2 and the records following this.
So I am looking for a way to get rid of the observations that come before the first occurrence of test type 2, and keep the rest within each ID.
Ideally this is what I would want in the end:
Student ID Date of Test Test Type
A 04/01/00 2
B 03/01/00 2
C 02/01/00 2
C 03/01/00 4
C 04/01/00 4
C 05/01/00 1
D 05/01/00 2
D 06/01/00 1
D 06/02/00 2
Thanks!!!
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Try this
data have;
input StudentID $ DateofTest :ddmmyy8. TestType;
format DateofTest ddmmyy8.;
datalines;
A 01/01/00 3
A 02/01/00 4
A 04/01/00 2
B 02/01/00 1
B 03/01/00 2
C 02/01/00 2
C 03/01/00 4
C 04/01/00 4
C 05/01/00 1
D 04/01/00 3
D 05/01/00 2
D 06/01/00 1
D 06/02/00 2
;
data want;
set have;
by StudentID;
if first.StudentID then _iorc_ = 0;
if TestType = 2 then _iorc_ = 1;
if _iorc_ = 1;
run;
Result:
StudentID DateofTest TestType A 04/01/00 2 B 03/01/00 2 C 02/01/00 2 C 03/01/00 4 C 04/01/00 4 C 05/01/00 1 D 05/01/00 2 D 06/01/00 1 D 06/02/00 2
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Try this
data have;
input StudentID $ DateofTest :ddmmyy8. TestType;
format DateofTest ddmmyy8.;
datalines;
A 01/01/00 3
A 02/01/00 4
A 04/01/00 2
B 02/01/00 1
B 03/01/00 2
C 02/01/00 2
C 03/01/00 4
C 04/01/00 4
C 05/01/00 1
D 04/01/00 3
D 05/01/00 2
D 06/01/00 1
D 06/02/00 2
;
data want;
set have;
by StudentID;
if first.StudentID then _iorc_ = 0;
if TestType = 2 then _iorc_ = 1;
if _iorc_ = 1;
run;
Result:
StudentID DateofTest TestType A 04/01/00 2 B 03/01/00 2 C 02/01/00 2 C 03/01/00 4 C 04/01/00 4 C 05/01/00 1 D 05/01/00 2 D 06/01/00 1 D 06/02/00 2
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
This worked perfectly, thank you!!!!