BookmarkSubscribeRSS Feed
nxmogil
Obsidian | Level 7

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 $;
datalines;
10 F
12 M
13 M
14 F
;
run;

proc sort data = one;
by sex;
run;

proc sort data = two;
by sex;
run;

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

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;
end;
run;

 

2 REPLIES 2
Tom
Super User Tom
Super User

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

https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/p1awxgleif5wlen1pja0nrn6yi6i.htm

 

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.

 

ballardw
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.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 508 views
  • 2 likes
  • 3 in conversation