BookmarkSubscribeRSS Feed
ESpiel
Calcite | Level 5

My data contains many rows, with multiple rows for some ids. I would like to create a new date variable based on a date column I have, such that all rows with the same id will display the EARLIEST date for that id. I've attached an example of what I mean.

 

What would be the best way to accomplish this? Do I need to use PROC SQL and a join, or is there an easy way to do this in SAS?

3 REPLIES 3
novinosrin
Tourmaline | Level 20
data  want;
 set have;
 by id;
 retain need;
 if first.id then need=date;
run;
ESpiel
Calcite | Level 5

Sorry, I realized after trying your solution that there is one more element I forgot in the sample. I uploaded a new one.

 

My issue is it may not actually be the earliest date for that id in the whole dataset, but I need the earliest date for each id where a certain indicator=1.

 

This code doesn't work, but the idea would be something similar to:

 

data want;
  set have;
  by id;
  retain need;
  if first.id AND indicator=1 then need=date;
else need=.; run;
novinosrin
Tourmaline | Level 20

Hi @ESpiel  Alright please try this-

data want;
  set have;
  by id;
  retain need;
  if first.id then need=.;
  if not need and indicator=1 then need=DATE_HAVE; 
  format need mmddyy10.; 
run;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 3 replies
  • 1657 views
  • 0 likes
  • 2 in conversation