Your SAS programs, embedded in web apps and elsewhere

Can you jump from one place to another in a SP?

Reply
N/A
Posts: 0

Can you jump from one place to another in a SP?

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?
SAS Super FREQ
Posts: 8,739

Re: Can you jump from one place to another in a SP?

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]
N/A
Posts: 0

Re: Can you jump from one place to another in a SP?

Cool! I appreciate the information and the heads up, Cynthia. Thank you so very much!
Ask a Question
Discussion stats
  • 2 replies
  • 199 views
  • 0 likes
  • 2 in conversation