BookmarkSubscribeRSS Feed
Fluorite | Level 6

Hi Everyone, I am learning SAS from carpenters guide. there I found this. this is one of the ways to do a table look up. But I dint understand the use of Do while. Can some one explain


data one;
input id sex $;
10 F
12 M
13 M
14 F

proc sort data = one;
by sex;

proc sort data = two;
by sex;

data two;
input sex $ class $;
M eng
F Soc
L mat

data withnames(keep= sex class ID);
set one (rename=(sex=Gender));
if Gender=Sex then output;
do while(Gender>Sex);
set two(keep=sex class);
if Gender=Sex then output;


Super User Tom
Super User

You can read about WHILE() in a DO statement in the documentation.


I would ignore that example program.  It does not appear to be doing anything of value.  It appears to have been created with the express purpose of confusing the reader.


Look at the examples in the documentation instead.


Super User

Is your question about doing the table look up or the actual Do While?


"Do While" repeats a block of code until a condition changes.

"Do Until" repeats a block of code until a condition is met.

Closely related. Generally there is something inside the block of code controlled by the loop that updates or sets the value(s) used in the condition(s) in Do While/Until statement.


Caution: These two statements are likely the cause of most infinite loops as a poorly stated condition, or not changing the values used in the conditions correctly, means the end condition is never met.


Strongly suggest testing such code with very small data sets with known values so the expected loop behavior should have small run times. If the set doesn't finish in a short time you are likely in an infinite loop and have to interrupt the running code. Hint: SAVE the code before running. You may end up finding it easier to end the SAS process than to just interrupt the code depending on your set up.


Do While, Do Until and Do loops in general are common programming structures and not just SAS but many other language references of such apply. The difference is typically how the conditions are specified. This means you can find lots of examples if not limiting to just SAS.



Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg



Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 2 replies
  • 3 in conversation