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