BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Shradha1
Obsidian | Level 7
I have a liat of students. For each student at a particular tine there is a flag which takes value 0 or 1. The data is sorted in ascending order. Student time flag;
A. 1:20 0
A. 1:35. 1
A 2:40 0
B. 5:30. 0
B. 5:35. 0
B. 6:10. 1

I want to keep only those observations for a student where the flag is 1, and the observation prior to that. Like for this example I want to keep the first two obs for student A and last two for B.
How fo I achieve this? Please help
1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

data have;
input student $ time $ flag;
datalines;
A  1:20  0
A  1:35. 1
A  2:40  0
B  5:30. 0
B  5:35. 0
B  6:10. 1
;

data want;
    merge have 
          have(firstobs = 2 keep = flag rename = flag = f);
    if f or flag;
    drop f;
run;

View solution in original post

2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20

data have;
input student $ time $ flag;
datalines;
A  1:20  0
A  1:35. 1
A  2:40  0
B  5:30. 0
B  5:35. 0
B  6:10. 1
;

data want;
    merge have 
          have(firstobs = 2 keep = flag rename = flag = f);
    if f or flag;
    drop f;
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 930 views
  • 1 like
  • 2 in conversation