BookmarkSubscribeRSS Feed
deleted_user
Not applicable
In other languages, like Visual Basic or FOCUS, you can write code that'll allow you to jump from one part of the code to another part. Can you do the same thing in a SP? i.e., ...If WOMBAT eq 1 then goto x else if WOMBAT = 2 then goto Z, else goto Y. Can that sort of thing be done?
2 REPLIES 2
Cynthia_sas
SAS Super FREQ
Hi:
You can look in the SAS Language documentation for how LINK works vs GO TO. I generally discourage the use of the GO TO statement in favor of making a LINK section. (I have written and maintained GO TO code in Fortran and Basic and it's too easy to write "spaghetti" code that is awful to maintain -- hence the reason I much prefer the LINK statement. I'm a big fan of maintainability.)

The nice thing about the LINK statement is that it could almost be considered like a separate sub-routine and control returns to where the LINK statement was issued (or more accurately, to the statement after where the LINK statement was issued. The GOTO statement on the other hand, returns control to the top of the DATA step program.

LINK provides you with a more structured programming approach to your logic. LINK and GOTO are both documented in a section entitled: "DATA Step Processing" --> "About DATA Step Execution".

Here's a program that illustrates using the LINK statement. Submit it from a code node in EG and then review the output. (Not intending for this program to be a stored process -- again, you need to understand how this works in "regular" SAS code and have a working program before you convert that program to be a stored process.)

cynthia
[pre]

** the code;
data stuff;
infile datalines;
input name $ WOMBAT;
if WOMBAT = 1 then do;
link DOX;
end;
else if WOMBAT = 2 then do;
link DOZ;
end;
else do;
link DOY;
end;
BOTVAR = 'all the same';
LINK FOR_ALL;
output stuff;
return;
DOX:
MIDVAR = 'inside X';
return;
DOY:
MIDVAR = 'inside Y';
return;
DOZ:
MIDVAR = 'inside Z';
return;
FOR_ALL:
WOMBAT2 = WOMBAT ** WOMBAT;
return;
datalines;
fred 1
joe 2
bob 3
zach 4
;
run;

proc print data=stuff;
title 'MIDVAR will be different depending on the value of the WOMBAT var';
title2 'BOTVAR will be the same for everybody';
run;
[/pre]
deleted_user
Not applicable
Cool! I appreciate the information and the heads up, Cynthia. Thank you so very much!

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 2 replies
  • 844 views
  • 0 likes
  • 2 in conversation