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

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!

  

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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;

 

View solution in original post

5 REPLIES 5
Reeza
Super User

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;

 

Jest
Obsidian | Level 7

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.

 

Reeza
Super User

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;

CurlerBob
Fluorite | Level 6

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.

ChrisNZ
Tourmaline | Level 20

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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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