Help using Base SAS procedures

Loops?

Reply
Contributor spg
Contributor
Posts: 61

Loops?

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
Trusted Advisor
Posts: 2,116

Re: Loops?

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;
PROC Star
Posts: 7,480

Re: Loops?

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;
Contributor spg
Contributor
Posts: 61

Re: Loops?

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 Smiley Happy
Ask a Question
Discussion stats
  • 3 replies
  • 123 views
  • 0 likes
  • 3 in conversation