DATA Step, Macro, Functions and more

Filter records after a flagged field

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

Filter records after a flagged field

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?


Accepted Solutions
Solution
‎03-28-2016 09:52 AM
Trusted Advisor
Posts: 1,115

Re: Filter records after a flagged field

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.

View solution in original post


All Replies
Solution
‎03-28-2016 09:52 AM
Trusted Advisor
Posts: 1,115

Re: Filter records after a flagged field

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.

Occasional Contributor
Posts: 5

Re: Filter records after a flagged field

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!

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 201 views
  • 1 like
  • 2 in conversation