BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hua
Obsidian | Level 7 hua
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

6 REPLIES 6
Astounding
PROC Star

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;

hua
Obsidian | Level 7 hua
Obsidian | Level 7
It works very well. Thanks!
Ksharp
Super User
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;
hua
Obsidian | Level 7 hua
Obsidian | Level 7
Thank you for help! It helped me a lot!
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
hua
Obsidian | Level 7 hua
Obsidian | Level 7
Thanks for reply!
Could you mind to tell me what does "retain" do? I always confusing about retain function.

sas-innovate-white.png

🚨 Early Bird Rate Extended!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Lock in the best rate now before the price increases on April 1.

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.

SAS Training: Just a Click Away

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

Browse our catalog!

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