Hello All,
I tried to find the first and last non-missing value in a dataset by group, but still have some trouble to do so. I hope someone in the SAS community groups can help. Thanks a lot!
I have a dataset looks like this:
stn | year | month | day | var |
1 | 2000 | 12 | 29 | 0.5 |
1 | 2000 | 12 | 30 | . |
1 | 2000 | 12 | 31 | 0 |
1 | 2001 | 1 | 1 | 0.2 |
1 | 2001 | 1 | 2 | 1 |
1 | 2001 | 1 | 3 | . |
2 | 2000 | 12 | 29 | . |
2 | 2000 | 12 | 30 | . |
2 | 2000 | 12 | 31 | . |
2 | 2001 | 1 | 1 | 0 |
2 | 2001 | 1 | 2 | 1.5 |
2 | 2001 | 1 | 3 | 3 |
And I want to find the first and last non-missing observation(var) for each stn so that I could know the nonmissing var for each stn is from what time to when. What I means is, in this example, I want to find for stn 1 the first is in 12/29/2000 and the last is 1/2/2001. And for stn 2, the first is 01/01/2001, and the last is 01/03/2001.
If any one can assist in this problem I would greatly appreciate it!
Thank you and anxiously awaiting a reply!
SAS has tools that make this easy:
data want;
set have;
by stn;
where var > .;
if first.stn or last.stn;
run;
The combination of WHERE + BY sets up the BY variables based on the observations that meet the WHERE condition.
SAS has tools that make this easy:
data want;
set have;
by stn;
where var > .;
if first.stn or last.stn;
run;
The combination of WHERE + BY sets up the BY variables based on the observations that meet the WHERE condition.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Early bird rate extended! Save $200 when you sign up by March 31.
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.
Ready to level-up your skills? Choose your own adventure.