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!!!
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
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
This worked perfectly, thank you!!!!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.