Help using Base SAS procedures

How to get the observations within a range.

Accepted Solution Solved
Reply
Contributor hua
Contributor
Posts: 66
Accepted Solution

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: 5,503

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;

View solution in original post


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

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;

Contributor hua
Contributor
Posts: 66

Re: How to get the observations within a range.

Posted in reply to Astounding
It works very well. Thanks!
Super User
Posts: 10,023

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;
Contributor hua
Contributor
Posts: 66

Re: How to get the observations within a range.

Thank you for help! It helped me a lot!
Super User
Super User
Posts: 7,942

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;
Contributor hua
Contributor
Posts: 66

Re: How to get the observations within a range.

Thanks for reply!
Could you mind to tell me what does "retain" do? I always confusing about retain function.
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 445 views
  • 5 likes
  • 4 in conversation