BookmarkSubscribeRSS Feed
michellel
Calcite | Level 5

Hi,

I have a question about how to calculate day difference from the first date within a group (ID). Thanks much for your answer.

My data looks like as below. There are two columns (ID, date) and some other columns, which I did not show here but I need keep. I need create a new variable date_diff to show day difference from the first date within each ID. I showed a example of ID=1 as below for how I need calculate.

ID     date               date_diff

1     06JUL2006          0

1     23JUL2006     23JUL2006 - 06JUL2006

1     08AUG2007     08AUG2007 - 06JUL2006

2     03DEC2004

2     01JAN2005

2 REPLIES 2
Astounding
PROC Star

Assuming your data are sorted properly, by ID DATE, here's an elegant way:

data want;

   set have;

   by id;

   date_diff + dif(date);

   if first.id then date_diff=0;

run;

(Of course, beauty is in the eye of the beholder.)  Good luck.

michellel
Calcite | Level 5

it works! Thanks Astounding!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 2 replies
  • 2438 views
  • 2 likes
  • 2 in conversation