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

Dear all,

 

I need to delete observations for a variable when a certain condition happens. Let's me give you an example of my dataset:

 

data have;

input id variable1 variable_year;

datalines;

 

1 0 2000

1 0 2001

1 0 2002

1 1 2003

2 0 2007

2 1 2008

3 0 2003

3 0 2004

3 0 2005

4 1 2006

4 . 2007

5 1 2010

5 . 2011

5 . 2012

6 1 2001

6 . 2002

6 . 2003

6 . 2004

6 . 2005

;

run;

 

What I want as a result would be something like that:

 

I need to delete observations for a variable when a certain condition happens. Let's me give you an example of my dataset:

 

data want;

input id variable1 variable_year;

datalines;

 

1 0 2000

1 0 2001

1 0 2002

1 1 2003

2 0 2007

2 1 2008

3 0 2003

3 0 2004

3 0 2005

 

 

;

run;

 

Si I want to delete the observations by id variable when variable1 = 1 and variable_year = first row. Does anyone could help ?

 

I have tried this, but it does not work because I just delete the row for that observation.

 

data want;

set have;

if variable1 = 1 and variable_year=first.variable_year then delete;

run;

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Assuming your data is sorted by ID year

 

data have;

input id variable1 variable_year;

datalines;
1 0 2000
1 0 2001
1 0 2002
1 1 2003
2 0 2007
2 1 2008
3 0 2003
3 0 2004
3 0 2005
4 1 2006
4 . 2007
5 1 2010
5 . 2011
5 . 2012
6 1 2001
6 . 2002
6 . 2003
6 . 2004
6 . 2005
;

run;

data want;
set have;
by id;
retain k;
if first.id then k=0;
if first.id and variable1=1 then k=1;
if k ne 1;
drop k;
run;

View solution in original post

4 REPLIES 4
novinosrin
Tourmaline | Level 20

Pretty straight forward but what is this?

 

variable_year = first row.

MFraga
Quartz | Level 8

It is just the first observation for the variable_year. When the "variable_year" is in the first line for the ID and "variable1" is equal to 1 then I want to delete all the observatins for that "id" that meet my condition.

novinosrin
Tourmaline | Level 20

Assuming your data is sorted by ID year

 

data have;

input id variable1 variable_year;

datalines;
1 0 2000
1 0 2001
1 0 2002
1 1 2003
2 0 2007
2 1 2008
3 0 2003
3 0 2004
3 0 2005
4 1 2006
4 . 2007
5 1 2010
5 . 2011
5 . 2012
6 1 2001
6 . 2002
6 . 2003
6 . 2004
6 . 2005
;

run;

data want;
set have;
by id;
retain k;
if first.id then k=0;
if first.id and variable1=1 then k=1;
if k ne 1;
drop k;
run;
MFraga
Quartz | Level 8

This works fine. Many thanks!

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 846 views
  • 1 like
  • 2 in conversation