- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
mod(mob,3)