BookmarkSubscribeRSS Feed
Kwok
Calcite | Level 5
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
6 REPLIES 6
Flip
Fluorite | Level 6
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;
SUN59338
Obsidian | Level 7
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.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
Kwok
Calcite | Level 5
Thanks to all your explanation.
SUN59338
Obsidian | Level 7
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;
abdullala
Calcite | Level 5
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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

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
  • 6 replies
  • 4706 views
  • 0 likes
  • 5 in conversation