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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 695 views
  • 0 likes
  • 3 in conversation