Hello,
I am trying to create a process that would select records from a table until a field is flagged, then skip to the next customer;
OBS FLAG Customer
1 0 11111
2 0 11111
3 1 11111
4 0 11111
5 0 22222
6 0 22222
7 1 22222
8 0 22222
9 0 22222
10 1 33333
11 0 33333
In this sample it would pull observations 1,2,3,5,6,7,and 10. After the flag is equal to 1, I would like it to go to the next customer.
Is there a do loop that would complete this action?
Hello @DBAgFinance,
Try this:
data have;
input FLAG Customer;
cards;
0 11111
0 11111
1 11111
0 11111
0 22222
0 22222
1 22222
0 22222
0 22222
1 33333
0 33333
;
data want;
do until(last.customer);
set have;
by customer;
if not finished then output;
if flag then finished=1;
end;
drop finished;
run;
Actually, this code reads all records, but it stops outputting them after flag=1 has been encountered for the respective customer.
Hello @DBAgFinance,
Try this:
data have;
input FLAG Customer;
cards;
0 11111
0 11111
1 11111
0 11111
0 22222
0 22222
1 22222
0 22222
0 22222
1 33333
0 33333
;
data want;
do until(last.customer);
set have;
by customer;
if not finished then output;
if flag then finished=1;
end;
drop finished;
run;
Actually, this code reads all records, but it stops outputting them after flag=1 has been encountered for the respective customer.
Thank you for the help. I was trying to implement the last. variable in a do loop and could not get it to work.
Very much appreciated!
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.