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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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