BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Steelers_In_DC
Barite | Level 11

I am trying to stop a do loop once a value is divisible by three.  I have never come across the leave statement before but it looked like a good fit.  I'm running the code below and not getting the desired output. 

 

There's no error but the code runs as if the line with 'leave' was commented out.  If anyone can provide any insight on this function and what I'm doing wrong I'd appreciate it. 

 


data have;
input id mob co;
cards;
1 35 1
1 34 1
1 33 1
1 32 1
2 33 1
2 32 1
2 31 1
2 30 1
;
run;

data want;
input id mob co;
cards;
1 35 0
1 34 0
1 33 1
1 32 1
2 33 1
2 32 1
2 31 1
2 30 1
;
run;

data getting;
input id mob co;
cards;
1 35 0
1 34 0
1 33 1
1 32 0
2 33 1
2 32 0
2 31 0
2 30 1
;
run;

data code;
set have;
by id;
do i = 1 to 3;
if mod(mob,3) ne 0 then co = 0;
if mod(mob,3) = 0 then leave;
end;
drop i;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
Steelers_In_DC
Barite | Level 11

I'm moving on from the leave option.  I think this works fine:

 

data code;

set have;

by id;

count + 1;

if first.id then count = 1;

i = 1;

do while(i < 4);

if count = i and mod(mob,3) ne 0 then co = 0;

if mod(mob,3) = 0 then i = i + count;

i + 1;

end;

drop count i;

run;

View solution in original post

7 REPLIES 7
Reeza
Super User
What exactly are you looping over there? You only have one variable and I is never used?
Steelers_In_DC
Barite | Level 11

So right, sorry I forgot an important step:

 

data code;
set have;
by id;
count + 1;
if first.id then count = 1;
do i = 1 to 3;
if count = i and mod(mob,3) ne 0 then co = 0;
if mod(mob,3) = 0 then leave;
end;
drop i count;
run;

Steelers_In_DC
Barite | Level 11

I also tried doing the same thing inside a macro with no luck.  I tried using:

 

%goto leave;

%end;

leave:

 

and that didn't work either.

Reeza
Super User

I still don't understand why you have a loop.

 

I think this is what you want:

 


data code;
set have (drop=co);
by id;
retain co;
if first.id then co=0;

if mod(mob,3)=0 and co=0 then co=1;

run;
Steelers_In_DC
Barite | Level 11

This is an oversimplified version of the real data.  CO represents a charge-off amount for a customer.  I cannot drop the co variable in the set statement, it's not binary.

Steelers_In_DC
Barite | Level 11

I'm moving on from the leave option.  I think this works fine:

 

data code;

set have;

by id;

count + 1;

if first.id then count = 1;

i = 1;

do while(i < 4);

if count = i and mod(mob,3) ne 0 then co = 0;

if mod(mob,3) = 0 then i = i + count;

i + 1;

end;

drop count i;

run;

Astounding
PROC Star
You need to specify a little more of the logic. There is nothing in the program (let alone inside the loop) that could change the value of

mod(mob,3)

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 3787 views
  • 0 likes
  • 3 in conversation