BookmarkSubscribeRSS Feed
CharlesR
Calcite | Level 5
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
17 REPLIES 17
ieva
Pyrite | Level 9
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!
deleted_user
Not applicable
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;
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
CharlesR
Calcite | Level 5
i thought about a simple
but it also didn't work.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
CharlesR
Calcite | Level 5
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?
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
CharlesR
Calcite | Level 5
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.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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
CharlesR
Calcite | Level 5
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?
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
CharlesR
Calcite | Level 5
Where can i find how to do a binary sort with numeric values? Tried to google it, but not sure where to look exactly . . .
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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
CharlesR
Calcite | Level 5
hey thanks for all your help!!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 17 replies
  • 15411 views
  • 0 likes
  • 6 in conversation