Contributor
Posts: 56

# Infinite Loop

In the following code the statement DO UNTIL(LAST,B) run into an infinite loop( wait 2 min and the program still running).

DATA TEST;
INPUT A \$ B \$;

DATALINES;
JOHN Y
BETTY N
JOHN N
BETTY N
BETTY N
ERIC N
HALLY N
HALLY Y
HALLY N
MARY Y
ERIC N
;
RUN;
PROC SORT OUT=TEMP; BY A DESCENDING B;
RUN;

DATA OUT1 ;
SET TEMP; BY A DESCENDING B;

IF FIRST.A AND B IN('Y','N') THEN OUTPUT ;
ELSE IF FIRST.A AND B='N' THEN DO UNTIL(LAST.B);
OUTPUT;
END;

RUN;

All I want is the output

A B

BETTY N
BETTY N
BETTY N
ERIC N
ERIC N
HALLY Y
JOHN Y
MARY Y

Why DO UNTIL(LAST.B) is not correct ? Any idea on what should be the correct code to achieve this output?

Thanks
David
Super Contributor
Posts: 359

## Re: Infinite Loop

Your do loop is acting on a single observation, so you will never hit last.b

I think you just want else if last.b then output;
Contributor
Posts: 24

## Re: Infinite Loop

the line "ELSE IF FIRST.A AND B='N' THEN DO UNTIL(LAST.B);" caused you a infinite loop. By the first OBS of dataset temp, the first.A and B="N" is true, then do untill loop will never be finished, because the last.B will never reached-----
the last.B will be true after two interations, but the program will never get there.
Super Contributor
Posts: 3,176

## Re: Infinite Loop

And, consider where your SET statement is located, relative to the DO UNTIL / END logic -- your DATA step flow will not iterate back to the top without encountering a SET / BY operation.

Suggest you re-think your DATA step flow. Also, make use of SAS diagnostic statements (one or more), such as:

PUTLOG ">DIAG-nnn>"_ALL_;

at important points (you increment "nnn" for identification in the log) in your DATA step so you can see how the flow is going at any one point.

Scott Barry
SBBWorks, Inc.
Contributor
Posts: 56

Contributor
Posts: 24

## Re: Infinite Loop

You may try this, even If there are more posibble values exist for B :

DATA TEST;
INPUT A \$ B \$;

DATALINES;
JOHN Y
BETTY N
JOHN N
BETTY N
BETTY N
ERIC N
HALLY N
HALLY Y
HALLY N
MARY Y
ERIC N
HALLY Q
HALLY Q
LOHN Z
;
RUN;
PROC SORT OUT=TEMP; BY A DESCENDING B;
RUN;

DATA OUT1 ;
SET TEMP;
BY A DESCENDING B;

DROP flag;
RETAIN flag 0;

IF FIRST.A THEN DO;
IF B='N' THEN DO;
flag=1;
IF not LAST.B THEN OUTPUT;
END;
ELSE DO;
flag=0;
IF B = 'Y' then output;
END;
END;

ELSE IF B='N' AND FLAG=1 THEN OUTPUT;

RUN;
Contributor
Posts: 74

## Re: Infinite Loop

the reason for infinite looping was exactly as other people pointed out.

if I understand you correctly, what you wanted is that, for any name (A) if the B value is 'Y' then output only once, otherwise output all records (i.e. B='N').

my approach is as follows and see if this works:

proc sort data=test out=temp;
by A descending B;
run;

data out1;
set temp;
by A descending B;

if not first.B and B='Y' then delete;
run;
Discussion stats
• 6 replies
• 2240 views
• 0 likes
• 5 in conversation