Help using Base SAS procedures

nested if-then/else grouping

Reply
Contributor
Posts: 58

nested if-then/else grouping

Hello,
I've got the following if-then/else data grouping.

[
if Runner3BId=0 then
_if Runner2BId=0 then
___if Runner1BId=0 then do;
_______baserunners = 0; /*bases empty*/
_________end;
_______else do; baserunners = 1; /*man on 1b*/
_______end;
__else if Runner2BId ^= 0 then do;
____yada, yada
end;
] this part works fine and assigns the baserunners variable correctly

but the second half of the Runner3BId has the correct nesting structure but doesn't assign the variable. so the problem has to be in the first 2 line here (i think) but for the life of me, i can't see what is wrong.
[
_else if Runner3BId ^= 0 then do;
__if Runner2BId=0 then
_____if Runner1BId=0 then do;
________baserunners = 3; /*man on 3b*/
________end;
_____else do; baserunners = 5; /*men on 1b&3b*/
_____end;
__else if Runner2BId ^= 0 then do;
yada, yada
__end;
_end;
run;
]

Anyone see what i'm doing wrong?

damn it, why don't they allow the leading spaces on the lines? let's try underscore Message was edited by: CharlesR
Frequent Contributor
Posts: 82

Re: nested if-then/else grouping

It is very hard to read your code and understand if-then logic there. One thing – maybe instead of

if Runner2BId=0 then
if Runner1BId=0 then do;

you should write

if Runner2BId=0 and
if Runner1BId=0 then do;

And maybe you should check the logic of the code – where does do-end start and end, maybe code is skipping some important part.

Good luck in fixing your code!
N/A
Posts: 0

Re: nested if-then/else grouping

also the sequencing of if-then statement.


if 'condition1' then do ; .... end;
else if 'condition2' then do ; .... end;
else if 'condition3' then do ; .... end;
.
.
.
.
else do; .... end;
Super Contributor
Super Contributor
Posts: 3,174

Re: nested if-then/else grouping

Also, for diagnosing your own SAS program, consider using PUTLOG _ALL_; and/or issue a PUTLOG command with a unique text-message in one or more of your DO/END code paragraphs.

Scott Barry
SBBWorks, Inc.
Contributor
Posts: 58

Re: nested if-then/else grouping

i thought about a simple
but it also didn't work.
Super Contributor
Super Contributor
Posts: 3,174

Re: nested if-then/else grouping

Your attempts to use nested/stacked IF / THEN without using an associated DO/END is causing your code path to fail. Suggest you simplify your code by using complete DO/END structure for all code paths, even if only one line of code.

Scott Barry
SBBWorks, Inc.
Contributor
Posts: 58

Re: nested if-then/else grouping

I'm not sure i totally understand what you're saying. i'd like to think that i understand, but, i'd be wrong, lol.

what is an associated DO/END? specifically, how is it different from what i have done so far?
Super Contributor
Super Contributor
Posts: 3,174

Re: nested if-then/else grouping

You have a hierarchy of IF THEN ; and attempting to use that structure also with nested ELSE/DO/END and/or ELSE IF THEN code paragraphs, intermixed. Because of your coding technique, those nested logic paths may or may not fire (based on your logic) and by using DO/END for each of the nested code path conditions, you will see more predictable code execution. Also, for your own debugging, by using the DO/END logic, you can then insert PUTLOG commands to more easily test all logic paths, where messages get echo'd to the SASLOG.

Scott Barry
SBBWorks, Inc.
Contributor
Posts: 58

Re: nested if-then/else grouping

Ah. I'm quite new to SAS and was somehow under the impression that my IF/THEN loop had to have the DO statement in order to work.

So i can essentially write a series of DO loops which will assign the values correctly. I would then assume i have to have the data sorted appropriately, meaning sorted by Runner3BId, Runner2BId, Runner1BId. i could essentially have something like this:

do until(EndoFile);
__do until Runner3BId ^= 0;
___do until Runner2BId ^= 0
_____do until Runner1BId ^= 0;
_______baserunners = 0; /*bases empty*/
_____END;
_____baserunners = 1; /*man on 1st*/
___END;

etc. etc.
Super Contributor
Super Contributor
Posts: 3,174

Re: nested if-then/else grouping

Uhhh, you're going to need a SET statement somewhere inside the outer DO/END loop, and I suggest exploring the use of LEAVE and CONTINUE, as well, depending on your circumstances. Also, the STOP; statement possibly for premature DATA-step termination.

Although, you also need to understand about SAS DATA step pass behavior when running a DO/END with an interior SET statement -- effectively you only execute one DATA step pass (depends on the application rqmt though).

Scott Barry
SBBWorks, Inc.

Suggested Google advanced search argument, this topic / post:

data step programming intro site:sas.com
Contributor
Posts: 58

Re: nested if-then/else grouping

i am doing this all under data, so it begins with

data testdata;
__set testdata;

then goes into the do loop.

as for leave and continue, the problem with them is that they will exit the loop correct? i note how you say depending on the circumstances, the biggest problem is that i'm not sure how to incorporate them when i want the loop to cycle through all the data.

also, how can i sort the data in a binary fashion? i.e. i want it to sort by Runner3BId as to whether it = 0 or is > 0, then within that as to whether Runner2BID is = 0 or >0 and then the same with Runner1BId, as opposed to everything running numerically. Am i better off simply creating a new variable which simply indicate whether there is someone on 1st, 2nd and/or 3rd?
Super Contributor
Super Contributor
Posts: 3,174

Re: nested if-then/else grouping

Numeric variables yield a binary sort or if you are more concerned, yes, detect some condition and set a 1 or 0, and sort on that as a BY variable.

And, so, if you do not want to exit a DO/END loop, use some combination of variables in an expression, to get you into the loop and then set a flag-variable to get you out.

Scott Barry
SBBWorks, Inc.
Contributor
Posts: 58

Re: nested if-then/else grouping

Where can i find how to do a binary sort with numeric values? Tried to google it, but not sure where to look exactly . . .
Super Contributor
Super Contributor
Posts: 3,174

Re: nested if-then/else grouping

Honestly, you should open a new forum thread...

Start here with basic SAS PROC SORT documentation, then review the particulars for your OS with the SAS Companion guide.

Google advanced search argument, this topic / post:

proc sort documentation site:sas.com

proc sort numeric variables binary collation site:sas.com

numeric variables binary collation sort site:sas.com


Scott Barry
SBBWorks, Inc. Message was edited by: sbb
Contributor
Posts: 58

Re: nested if-then/else grouping

hey thanks for all your help!!
Ask a Question
Discussion stats
  • 17 replies
  • 6529 views
  • 0 likes
  • 6 in conversation