BookmarkSubscribeRSS Feed
David_Billa
Rhodochrosite | Level 12

I'm trying to understand the program which was written by other programmer. In the below code, I don't understand the purpose of do until before SET statement.
Any help to make me understand this code?

 

data want;
do until (last.nbr);
set have;
by id name;
if dt=. then dt=start_dt;
end;
run;
3 REPLIES 3
Tom
Super User Tom
Super User

The purpose of DO UNTIL() statement is a loop that repeats until the condition is met. Unlike a DO WHILE() loop a DO UNTIL() loop will always run at least once.

 

The code was written will create an empty dataset.  Since there is no variable NBR in the BY statement there will be no variable LAST.NBR created to flag when the end of the by group is reached.  So it value will be missing (false) and the DO loop will run forever.  The data step will end when the SET statement reads past the end of the input.  Most data steps end when they read past their inputs. So the implicit OUTPUT statement at the end of the step (if you do not code any OUTPUT statements SAS always runs one at the end of the data step) is never executed. So no observations are written.

 

The purpose of nesting a SET inside of a DO loop is to read multiple observations from the input data in a single iteration of the data step.  Using a BY statement and LAST.XXX in the UNTIL() condition will make it so all of the observations for a by group are processed in a single data step iteration.

 

Assuming they meant to use LAST.ID or LAST.NAME then probably the purpose of this step was to find the last non-missing value of DT_START per by group.  Either per value or ID or per value of ID and NAME combination.

PeterClemmensen
Tourmaline | Level 20

This is known as a DoW loop (Do Whitlock) named after Ian Whitlock (an absolute master). 

 

There are a few good resources on the topic out there. For example The DOW-Loop Unrolled.

yabwon
Onyx | Level 15

Or "The Magnificent DO": https://support.sas.com/resources/papers/proceedings13/126-2013.pdf

 

B-)

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 3 replies
  • 324 views
  • 3 likes
  • 4 in conversation