## Find first and last non-missing value by group

Solved
Frequent Contributor
Posts: 81

# Find first and last non-missing value by group

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!

Accepted Solutions
Solution
‎05-09-2017 11:20 AM
Super User
Posts: 6,785

## Re: Find first and last non-missing value by group

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.

All Replies
Solution
‎05-09-2017 11:20 AM
Super User
Posts: 6,785

## Re: Find first and last non-missing value by group

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.

Frequent Contributor
Posts: 81