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;
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;
Pretty straight forward but what is this?
variable_year = first row.
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.
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;
This works fine. Many thanks!
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.