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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

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
  • 5 replies
  • 844 views
  • 0 likes
  • 3 in conversation