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;
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;
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;
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.
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;
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.
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;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.