Solved
Contributor
Posts: 51

# if first.ID selecting observation from baseline only

Hi,

I have a longitudinal dataset of 800 subjects who got measurements at different time points over time. I want to create a dataset that includes only first measurements (baseline)....The data looks like the below:

ID      VISIT        feature

1           2               .

1           3             3.77

1           4             8.88

1           5             5.22

1           6             2.21

2           1              .

2           3              .

2           4             6.88

2           5             7.77

2           6             8.54

I am using the following code:

data want;

set have;

by ID VISIT;

if first.ID and first.VISIT;

run;

However, it doesn't seem to work. The data should look like this:

ID   VISIT   feature

1      3       3.77

2      4       6.88

any advice would be appreciated...

Thanks!

Accepted Solutions
Solution
‎10-28-2017 11:53 AM
Super User
Posts: 24,010

## Re: if first.ID selecting observation from baseline only

[ Edited ]

Because you want to ignore missing you should drop them as well.

``````data want;
set have (where =(not missing(feature)));
by ID VISIT;
if first.ID;

run;``````

All Replies
Solution
‎10-28-2017 11:53 AM
Super User
Posts: 24,010

## Re: if first.ID selecting observation from baseline only

[ Edited ]

Because you want to ignore missing you should drop them as well.

``````data want;
set have (where =(not missing(feature)));
by ID VISIT;
if first.ID;

run;``````

Contributor
Posts: 51

## Re: if first.ID selecting observation from baseline only

Thank you Reeza. This was very helpful.

Quick question: Is there also a way to pick only the third or fourth observation, and not just the first?

In other words is there a third.ID command?

Thanks.

Super User
Posts: 24,010

## Re: if first.ID selecting observation from baseline only

No, there’s first and last. But you can use first to set a counter and use the counter as desired.

If first.id then counter =1;

else counter+1;

Occasional Contributor
Posts: 19

## Re: if first.ID selecting observation from baseline only

You need to eliminate the "and first.VISIT".  The sort requires using the VISIT variable to order the records so the first visit is the first record for the ID.  But for the actual selection, you only need first.ID.

Super User
Posts: 2,512

## Re: if first.ID selecting observation from baseline only

Also note that

first.VIST

is always true when

first.ID

so

if first.ID and first.VISIT;

is the same as

if first.ID;

To summarise the suggestions:

``````data WANT;
set HAVE;
where FEATURE;
by ID VISIT;
if first.ID ;
run;``````
☑ This topic is solved.

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

Discussion stats
• 5 replies
• 604 views
• 2 likes
• 4 in conversation