SAS flow control - the goto statement
I just recently encountered the goto statement in SAS. I've used a similar construct in other programming languages. Although such control can be very useful, they need to be used sparingly because it makes program logic difficult to follow.
Phil Specter from Berkeley wrote an Introduction to the SAS System which I think sums up the goto statement clearly:
"You can use the goto statement to have SAS process statements in some other part of your program, by providing a label followed by a colon before the statements you wish to jump to. Label names follow the same rules as variable names, but have a different name space. When a labeled statement is encountered in normal processing, it is ignored."
He provides a short example:
data two;
set one;
if x ^= . then goto out;
x = (y + z) / 2;
out: if x > 20 then output;
run;
Similarly, on a SAS Google Group thread, another example was provided:
data class;
input name $ 1-8 sex $ 11 age height weight;
select (name);
when ('Alice') do;
goto Alice;
end;
when ('Becka') do;
goto Becka;
end;
when ('Gail') do;
goto Gail;
end;
end;
Alice: x=4;
return;
Becka: x=5;
return;
Gail: x=6;
return;
datalines;
Alice F 13 56.5 84.0
Becka F 13 65.3 98.0
Gail F 14 64.3 90.0
run;
More resources on Goto:
- See the above code in my snippet repository
- View more examples here
- Stephens Goto memories
- Goto Hell (Ha!)
- A comment on Anti-Goto Nazi's
References (1)
-
Response: Running list of blogs by SAS usersAs many of you know, the community of SAS usersĀ is a fun, dedicated bunch with a passion for sharing knowledge and code. You'll see that reflected in this group of blogs: DataGister: Delivering the essence of business intelligence insights.Data Steps: A


Reader Comments