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

Hello All,

 

Now I have a question regarding how to to conditional deletion based on previous values.

my table looks like this.  this is recording if someone breaks rule for each month.

 

have 

ID   seq   happen

1      1            0

1       2           0

1       3           1

2       1           0

2       2           1

2       3           0

2       4           1 

 

want

 

ID seq happen

1      1            0

1       2           0

1       3           1

2       1           0

2       2           1

 

 

 

as long as there is the first 1 appears, I would like to delete the following rows against each ID.

In term, what are to be left is a table with 0(never happen) and first 1 (happen the first time).

 

now my way of doing it is 

1. add cumulative number after happen the first time

2, delete observation with value more than 1 from the original table

3, find the first 1 from the original table

4. merge the tables from 2 and 3

 

but I felt this is too redundent yet can find a more efficient way....

Please share with me how would you do this with a smarter processing idea.

 

I thougt of to set table until 1 against each ID and then ignore the rest boservations for this current ID and then

jump to the next ID with its first observation read,

but I don't know to code this in SAS....

 

Please share with me any thoughts...

 

thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
mohamed_zaki
Barite | Level 11
data want;
set have;
retain delflag;
by ID;
if first.id then delflag=0;
if delflag=0 then output;
if happen=1 then delflag=1;
run;

View solution in original post

2 REPLIES 2
mohamed_zaki
Barite | Level 11
data want;
set have;
retain delflag;
by ID;
if first.id then delflag=0;
if delflag=0 then output;
if happen=1 then delflag=1;
run;
Lulus
Obsidian | Level 7

thank you very much 

 

if delflag=0 then output;
if happen=1 then delflag=1;

in the above two statement,

the lines with delflag=0 have been written to the want dataset and

how does the next if clause still truncate the rest of observations against the same id?

and even leave the first "happen=1" in the dataset?

 

 

I am trying to learn this logic.  your response is very appreicated.

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
  • 2 replies
  • 2115 views
  • 1 like
  • 2 in conversation