Hi
If this is an easy question I am asking, then my SAS beginner skills are really getting rusty
My dataset is this:
co_name year cost
1 1999 4
1 2000 3
1 2001 1
1 2002 0
1 2003 1
1 2004 1
2 2001 1
2 2002 0
2 2003 1
3 2003 2
3 2004 0
The filter rule is cost=0. However, I not only want the rows where cost=0 but also atleast two rows preceding and two rows following that row...something like this:
co_name year cost
1 2000 3
1 2001 1
1 2002 0
1 2003 1
1 2004 1
2 2001 1
2 2002 0
2 2003 1
Thanks a lot!
Why is co_name=3 excluded?
Ohh!I did not mean to exclude it. Even co_name 3 gets in the subset if it has code=0.
There is also the SQL solution:
proc sql;
create table want as
select h.*
from
have as h inner join
(select * from have where cost=0) as z
on h.co_name=z.co_name and h.year between z.year-2 and z.year+2
order by co_name, year;
select * from want;
quit;
PG
Thanks Hai.kuo and PGStats
Of course there would be many solutions, while I would take it as a typical 2XDOW scenario.
data have;
input co_name $ year cost;
cards;
1 1999 4
1 2000 3
1 2001 1
1 2002 0
1 2003 1
1 2004 1
2 2001 1
2 2002 0
2 2003 1
3 2003 2
3 2004 0
;
data want;
do _n_=1 by 1 until (last.co_name);
set have;
by co_name notsorted;
if cost=0 then _0=_n_;
end;
do _n_=1 by 1 until (last.co_name);
set have;
by co_name notsorted;
if _0-2 <= _n_ <= _0+2 then output;
end;
drop _0;
run;
Haikuo
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.