SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
rangermollie
Calcite | Level 5

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!!!

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

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

View solution in original post

2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20

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
rangermollie
Calcite | Level 5

This worked perfectly, thank you!!!!

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 581 views
  • 1 like
  • 2 in conversation