SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
johnsonjf
Calcite | Level 5

I (SAS 9.3) have a dataset for a training course where students' block grades are in individual rows. If a student fails a block they are held back until the next class where they reattempt the block and must pass before they can continue in training.

 

I am wanting to create a "first-attempt only" dataset that contains only their block grades up until their point of failure. Conceptually I want SAS to look at each ID and within ID retain only the rows associated with the student's first training attempt. First training attempt is defined as their performance in the training class they started in (e.g., 202001).

 

For example, in the dataset below, student 1001 progressed through training in class 202001 until Block_3 where they failed. They then resumed training in class 202002 and finished all five blocks. For student 1001 I want to keep their performance in class 202001 (their first training attempt) and drop all observations of subsequent training classes (202002).

 

This is an example of my dataset. My goal is, within each ID, keep only the observations associated with the trainees first class. For 1001 that's class 202001, for 1002 that's class 202004, etc.

IDClassTraining_BlockGradeStatus
1001202001Block_188PASS
1001202001Block_276PASS
1001202001Block_368FAIL
1001202002Block_371PASS
1001202002Block_486PASS
1001202002Block_590PASS
1002202004Block_150FAIL
1002202006Block_173PASS
1002202006Block_291PASS
1002202006Block_395PASS
1002202006Block_471PASS
1002202006Block_578PASS
1003202003Block_176PASS
1003202003Block_289PASS
1003202003Block_380PASS
1003202003Block_495PASS
1003202003Block_597PASS
1004202008Block_174PASS
1004202008Block_270PASS
1004202008Block_360FAIL
1004202009Block_382PASS
1004202009Block_468FAIL
1004202010Block_472PASS
1004202010Block_590PASS

 

This is what I'm conceptually wanting SAS to do. Keep all observations with first class value within each ID and drop all subsequent class values within each ID:

IDClassTraining_BlockGradeStatus
1001202001Block_188PASS
1001202001Block_276PASS
1001202001Block_368FAIL
1001202002Block_371PASS
1001202002Block_486PASS
1001202002Block_590PASS
1002202004Block_150FAIL
1002202006Block_173PASS
1002202006Block_291PASS
1002202006Block_395PASS
1002202006Block_471PASS
1002202006Block_578PASS
1003202003Block_176PASS
1003202003Block_289PASS
1003202003Block_380PASS
1003202003Block_495PASS
1003202003Block_597PASS
1004202008Block_174PASS
1004202008Block_270PASS
1004202008Block_360FAIL
1004202009Block_382PASS
1004202009Block_468FAIL
1004202010Block_472PASS
1004202010Block_590PASS

 

This is my desired output:

IDClassTraining_BlockGradeStatus
1001202001Block_188PASS
1001202001Block_276PASS
1001202001Block_368FAIL
1002202004Block_150FAIL
1003202003Block_176PASS
1003202003Block_289PASS
1003202003Block_380PASS
1003202003Block_495PASS
1003202003Block_597PASS
1004202008Block_174PASS
1004202008Block_270PASS
1004202008Block_360FAIL

 

I feel like I've done this before and it involved do loops and first.dot and last.dot processing, but for the life of me I can't figure out how to do it this time.

 

Thanks in advance for help!

1 REPLY 1
Shmuel
Garnet | Level 18

Try next code:

data want;
 set have;
    by id ;
         retain flag;
        if first.id then flag=0;
        if status = 'FAIL' then flag=1;
        if not flag=1;
run;

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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
  • 1 reply
  • 348 views
  • 0 likes
  • 2 in conversation