## How to get the observations within a range.

Solved
Frequent Contributor
Posts: 81

# How to get the observations within a range.

Hi there,

Has anyone know how to get a specific range of data?

For example, I have this dataset:

Station  Year     Month Day Temp Count

1           2000         1     1      3.2      .

1           2000         1     2      4.2      .

1           2000         1     3      3.5      1

1           2000         1     4      3.7      .

1           2000         1     5      2.1      .

1           2000         1     6      3.0      .

1           2000         1     7      2.2      2

1           2000         1     8      3.1      .

1           2000         1     9      2.3      .

1           2000         1     10    3.2      .

......

I want to keep only the observations from count=1 to count=2, and I have several years and several stations.

Thank you for help!

Best,

Hua

Accepted Solutions
Solution
‎06-05-2016 10:25 PM
Super User
Posts: 6,753

## Re: How to get the observations within a range.

This should be workable:

data want;

do until (last.station);

set have;

by station;

if count=1 then found_1='Y';

if found_1='Y' and found_2 ne 'Y' then output;

if count=2 then found_2='Y';

end;

drop found_1 found_2;

run;

All Replies
Solution
‎06-05-2016 10:25 PM
Super User
Posts: 6,753

## Re: How to get the observations within a range.

This should be workable:

data want;

do until (last.station);

set have;

by station;

if count=1 then found_1='Y';

if found_1='Y' and found_2 ne 'Y' then output;

if count=2 then found_2='Y';

end;

drop found_1 found_2;

run;

Frequent Contributor
Posts: 81

## Re: How to get the observations within a range.

It works very well. Thanks!
Super User
Posts: 10,766

## Re: How to get the observations within a range.

``````data have;
input Station  Year     Month Day Temp Count;
cards;
1           2000         1     1      3.2      .
1           2000         1     2      4.2      .
1           2000         1     3      3.5      1
1           2000         1     4      3.7      .
1           2000         1     5      2.1      .
1           2000         1     6      3.0      .
1           2000         1     7      2.2      2
1           2000         1     8      3.1      .
1           2000         1     9      2.3      .
1           2000         1     10    3.2      .
1           2001         1     1      3.2      1
1           2001         1     2      4.2      .
1           2001         1     3      3.5      1
1           2001         1     4      3.7      .
1           2001         1     5      2.1      .
1           2001         1     6      3.0      .
1           2001         1     7      2.2      2
1           2001         1     8      3.1      .
1           2001         1     9      2.3      .
1           2001         1     10    3.2      1
;
run;
data want;
do until(not missing(count) or last.Year);
set have;
by Station  Year;
if first.Year then first=1;
end;
_count=count;
do until(not missing(count) or last.Year);
set have;
by Station  Year;
if not first and not missing(_count) and missing(count)then output;
end;
drop _count first;
run;``````
Frequent Contributor
Posts: 81

## Re: How to get the observations within a range.

Thank you for help! It helped me a lot!
Super User
Posts: 9,599

## Re: How to get the observations within a range.

Another way:

```data want (drop=flag);
set have;
retain flag;
if count=1 then flag=1;
if count=2 then flag=.;
if flag then output;
run;```
Frequent Contributor
Posts: 81