BookmarkSubscribeRSS Feed
namrata
Fluorite | Level 6

Hi

If this is an easy question I am asking, then my SAS beginner skills are really getting rusty Smiley Happy

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!

5 REPLIES 5
PGStats
Opal | Level 21

Why is co_name=3 excluded?

PG
namrata
Fluorite | Level 6

Ohh!I did not mean to exclude it. Even co_name 3 gets in the subset if it has code=0.

PGStats
Opal | Level 21

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

PG
namrata
Fluorite | Level 6

Thanks Hai.kuo and PGStats Smiley Happy

Haikuo
Onyx | Level 15

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1166 views
  • 0 likes
  • 3 in conversation