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

Hi,

 

I have an extract like this with multiple bans and records. It is sorted by ban and ent_seq_no. What i want to do is only keep records for a ban after the DF step only (including DF). So in this case the first 3 records would be deleted...

 

I am thinking that deleting the records where ent_seq_no is less than the seq no for the record that has code DF is the way but i am not sure how to write it...

 

BANENT_SEQ_NOCOL_ACTV_CODECOL_ACTV_DATE
10987941020677D204JUL2019:00:00:00
10987941953583D310JUL2019:00:00:00
109879423843523S12JUL2019:00:00:00
10987942902222DF16JUL2019:00:00:00
10987943965758E23JUL2019:00:00:00
109879443521841S25JUL2019:00:00:00
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Retain a flag variable:

data want;
set have;
by ban;
retain keepflag;
if first.ban then keepflag = 0;
if col_actv_code = 'DF' then keepflag = 1;
if keepflag;
drop keepflag;
run;

View solution in original post

6 REPLIES 6
Kurt_Bremser
Super User

Retain a flag variable:

data want;
set have;
by ban;
retain keepflag;
if first.ban then keepflag = 0;
if col_actv_code = 'DF' then keepflag = 1;
if keepflag;
drop keepflag;
run;
TheNovice
Quartz | Level 8

It worked! I was trying this to no avail


DATA TEST1;
SET TEST;
count +1;
do until (first.ban);
BY BAN;
IF COL_ACTV_CODE = 'DF' THEN COUNT= 0;
end;
RUN;

ballardw
Super User

This may get you started.

 

data want;
   set have;
   by ban ent_seq_no;
   retain flag;
   if first.ban then flag=0;
   if flag=0 and col_actv_code='DF' then flag=1;
   /* this keeps records after the flag is set*/
   if flag=1;
run;

You may want to drop the Flag variable after verifying that this works.

The BY sets special variable First. and Last. that you can check to do something for the group, in this case reset a flag. The flag should only be reset to 1 when 1) it is currently zero and 2) the value of DF is encountered. The Flag will stay at 1 until the next BAN group is encountered.

The last IF indicates which values get written to the output set.

TheNovice
Quartz | Level 8

thank you so much. your response is the same as above. The explanation helps. I think i have a much better understanding of how to work problems like this now.

novinosrin
Tourmaline | Level 20

data have;
input BAN	ENT_SEQ_NO	COL_ACTV_CODE $	COL_ACTV_DATE :datetime20.;
format COL_ACTV_DATE datetime20.;
cards;
10987	941020677	D2	04JUL2019:00:00:00
10987	941953583	D3	10JUL2019:00:00:00
10987	942384352	3S	12JUL2019:00:00:00
10987	942902222	DF	16JUL2019:00:00:00
10987	943965758	E	23JUL2019:00:00:00
10987	944352184	1S	25JUL2019:00:00:00
;

proc sql;
create table want as
select a.*
from have a ,(select ban,min(COL_ACTV_DATE) as d from have where COL_ACTV_CODE='DF' group by ban) b
where a.ban=b.ban and a.COL_ACTV_DATE>=d;
quit;
TheNovice
Quartz | Level 8

this is clever! I ran this and found that my data has duplications for DF. But I would want to keep the DF step with the latest sequence number. The other thing is that i got some duplicate outputs in my result for some reason. I will go with the data step solution but thank you for this. i have never thought about joining a table back on itself...

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1049 views
  • 0 likes
  • 4 in conversation