BookmarkSubscribeRSS Feed
Saravanan
Fluorite | Level 6

Hi Friends,

I have a simple problem, I wrote the logic but the logic fails to work properly when i run it in the actual data.

All i have to do is delete the rows after flag within the group. I have pasted my data(grouped by sno) , desired output and my code below. The code works fine with this data but fails on the real data which might have other combinations.

In the below data, all i have to do is delete all the rows which follows the first occurence of flag=1 within the group(grouped by sno).

I appreciate your time and help.

data test_del;
input sno A B flag;
cards;
1 1 2 0
1 1 1 0
1 1 1 1
1 1 1 0
1 1 1 0
2 2 2 0
2 2 2 1
2 2 2 0
3 3 3 1
3 3 3 0
3 3 3 1
3 3 3 0
4 4 4 0
4 4 4 0
4 4 4 1
;
run;

my output should look like:

sno A B flag

1 1 2 0

1 1 1 0

1 1 1 1

2 2 2 0

2 2 2 1

3 3 3 1

4 4 4 0

4 4 4 0

4 4 4 1

My code which fails to run on real data but runs on this test data.
data test_del1;
set test_del;
by sno;
retain fl;
if first.sno & flag ne 1 then fl=0;
if first.sno & flag =1 then output;
if fl ne 1 then output;
if flag =1 then fl =1;
run;

Sincerely,

Saravanan

11 REPLIES 11
art297
Opal | Level 21

How about?:

data test_del1 (drop=fl);

  set test_del;

  by sno;

  retain fl;

  if fl ne 1 then do;

    output;

    if flag eq 1 then fl=1;

  end;

  if last.sno then fl=0;

run;

Saravanan
Fluorite | Level 6

Thanks for your reply. It works perfect.

Much appreciated.

Saravanan

Linlin
Lapis Lazuli | Level 10

data test_del;

input sno A B flag;

cards;

1 1 2 0

1 1 1 0

1 1 1 1

1 1 1 0

1 1 1 0

2 2 2 0

2 2 2 1

2 2 2 0

3 3 3 1

3 3 3 0

3 3 3 1

3 3 3 0

4 4 4 0

4 4 4 0

4 4 4 1

;

run;

data want;

  set test_del;

  by sno ;

  retain c;

  if first.sno then c=1;

  if dif(flag)=-1 then c+1;

  if c<2;

 

  run;

  proc print;run;

Saravanan
Fluorite | Level 6

Thanks for your reply. It works fine.

Much appreciated for your time and effort.

Saravanan

Astounding
PROC Star

Here's my attempt:

data test_del1;

   set test_del;

   by sno;

   if first.sno then delete_me='N';

   retain delete_me;

   if delete_me='N' then output;

   if flag=1 then delete_me='Y';

   drop delete_me;

run;

Good luck.

Linlin
Lapis Lazuli | Level 10

I am using dif(). Don't you think I am a good learner?

Astounding
PROC Star

Ahh, yes I see you are paying good attention Linlin.  Remember, genius is 1% inspiration, 98% perspiration, and 2% attention to detail.

Upon closer inspection, I think Art's suggestion and mine are extremely similar.  I would guess that mine is easier to read, but Art's is slightly faster.

art297
Opal | Level 21

: I'm glad to see you learning new functions but, in this case, your logic will not always work correctly.  e.g., it will not exclude any records in the following scenario when, I think, it should only be keeping the first record for each sno:

data test_del;

input sno A B flag;

cards;

1 1 2 1

1 1 1 1

1 1 1 1

1 1 1 1

1 1 1 1

2 2 2 1

2 2 2 1

2 2 2 1

3 3 3 1

3 3 3 1

3 3 3 1

3 3 3 1

4 4 4 1

4 4 4 1

4 4 4 1

;

run;

data want;

  set test_del;

  by sno ;

  retain c;

  if first.sno then c=1;

  if dif(flag) then c+1;

  if c<2;

run;

Linlin
Lapis Lazuli | Level 10

Hi Art,

You are right. In that case my code would not work. Would you please read your post in the link (Soap box): https://communities.sas.com/thread/32053

I was scared to death by your comments when I just joined the forum a year ago. The good thing is that I am not afraid of you anymore!:smileylaugh::smileylaugh::smileylaugh:

art297
Opal | Level 21

Linlin,  I've never intended to scare anyone with my comments and glad to hear that you're not afraid of me.

About my soapbox comment, however long ago that was, it doesn't apply here as the requirement was to keep the first record (within a sno) that had a flag=1, but delete all remaining records.

Saravanan
Fluorite | Level 6

Thanks for your effort and time. It works fine.

Saravanan.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 11 replies
  • 1351 views
  • 4 likes
  • 4 in conversation