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

Hi Everyone,

I try to find the first date (row) with non-missing value for each id.

So in my sample data below, id 1 first valid date is 1. and id 2 first valid date is 3.

Still haven't figure out a good way to do it.

Thanks,

HHC

data have; 
  input date id v1;
datalines;
1 1 30
2 1 .
3 1 20
1 2 .
2 2 .
3 2 1
4 2 6
5 2 .
;
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Assuming you meant non-missing value of V1 then just use BY group processing.

data want;
  set have;
  where not missing(v1);
  by id date;
  if first.id;
run;

View solution in original post

5 REPLIES 5
ballardw
Super User

What do you expect the output to look like?

 

Is "date" ever missing? If so, what then?

pink_poodle
Barite | Level 11
proc sql;
create table want as
  select * 
  from have
  where not missing(v1)
  group by id, date
  having min(date)
  order by id
;
quit;
Tom
Super User Tom
Super User

Assuming you meant non-missing value of V1 then just use BY group processing.

data want;
  set have;
  where not missing(v1);
  by id date;
  if first.id;
run;
hhchenfx
Rhodochrosite | Level 12

Thank you all for helping.

@Tom : I follow your suggestion for the other topic and I think I get the idea.

Will post it once it is done.

HHC

mkeintz
PROC Star

Given your data is already sorted by id/date, this is a simple task:

 

data have; 
  input date id v1;
datalines;
1 1 30
2 1 .
3 1 20
1 2 .
2 2 .
3 2 1
4 2 6
5 2 .
;

data want;
  set have (where=(not missing(v1)));
  by id;
  if first.id;
run;

I take it that the "non-missing value" criterion refers to V1.

 

Although I assume data are sorted by ID/DATE, they really only have to be sorted by DATE within each ID.  The ID's can be in any order.  In this case you would change "BY ID;" to "BY ID NOTSORTED;".

 

And as to @ballardw question about missing dates - you could revise the WHERE parameter to ignore missing dates: 

  set have (where=(not missing(date) and not missing(v1)));

  

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 5 replies
  • 1808 views
  • 1 like
  • 5 in conversation