BookmarkSubscribeRSS Feed
spg
Obsidian | Level 7 spg
Obsidian | Level 7
Hi,

How can I write a program that will look for "25" in column B below and stop at the second "25" for the same group in column A? I have been using the index feature to account for the double entries in column B but am at a loss on how to proceed in this case.


A B
1 25
1 30
1 36
2 25, 52
2 40
2 25

Desired output:

A B
1 25
1 30
1 36
2 25, 52
2 40
3 REPLIES 3
Doc_Duke
Rhodochrosite | Level 12
You will need to set up an indicator flag using a RETAIN statement. You would set the indicator to 0 (off) for the first row in A and set it to 1 (on) for the first occurrence of 25. If you find another 25, use the STOP command to terminate building the output data set.

Read about RETAIN and BY-Group processing in the reference manual to understand how these things work together.

Something like this should work (untested code):

DATA new;
SET old;
BY a;
RETAIN flag;
IF first.a THEN flag=0;
IF INDEX(b,'25') GT 0 THEN DO;
IF flag=1 THEN STOP;
flag=1;
END;
RUN;
art297
Opal | Level 21
Doc already suggested code that will do what you asked, but do you really want to stop processing for all groups when two 25s are found? Or do you only want to not process any more of that group's records?

If it is the latter, I'd suggest the following:

DATA want;
SET have;
BY a;
if first.a then flag=0;
IF INDEX(b,'25') GT 0 THEN flag+1;
if flag lt 2 then output;
RUN;
spg
Obsidian | Level 7 spg
Obsidian | Level 7
You are right Art297. This is exactly what I wanted. I should've been more clear.
Anyway, thanks Doc! Thanks Art297! This was super helpful 🙂

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 3 replies
  • 950 views
  • 0 likes
  • 3 in conversation