How to calculate the elapse of time between two dates on SAS

Reply
Occasional Contributor
Posts: 16

How to calculate the elapse of time between two dates on SAS

I have a column called "date of first visit to the clinic" and "date of diagnosis".  My imported Excel Data Sheet has them as 12 28 2004 under the date of first visit to the clinic and 02 22 2008 under the date of diagnosis. How could I calculate the elapse of time in days, weeks and months in this case?

So far, I have been able to do PROC PRINT just to make sure that my SAS is reading those data right. I have them

The SAS System                               16

              Obs             DDT             MMT             YRT

                1              11              18            2012

                2              12              28            2011

                3               2               7            2012

                4               9              12            2008

                5               1              30            2012

                6               8               4            2000

                7               7              31            2009

                8              10               2            2000

I have another set that looks like this and need to figure out the elapse of time between the two sets of dates. Any advice is appreciated. I am a beginner SAS user going into the intermediate level.

Super Contributor
Posts: 1,636

Re: How to calculate the elapse of time between two dates on SAS

try:

time_diff=mdy(v_mm,v_dd,v_yy)-mdy(d_mm,d_dd,d_yy);

Frequent Contributor
Posts: 81

Re: How to calculate the elapse of time between two dates on SAS

You may try below code and see if it works for you.

data one;

  sday = 28; smonth = 12; syear = 2004;

  eday = 22; emonth = 2; eyear = 2008;

  sdate = mdy(smonth,sday,syear);

  edate = mdy(emonth,eday,eyear);

  *Duration in days;

  ndays = (edate-sdate) + 1;

  *Duration in weeks;

  nweeks = ndays/7;

  *Duration in months. I defined month as 30 days duration;

  nmonths = ndays/30;

  format sdate edate date9.;

run;

Super User
Posts: 17,899

Re: How to calculate the elapse of time between two dates on SAS

You'll need to convert the day, month and years to date values.

Then you'll need to merge the datasets together and you can subtract dates to get the difference between. Sometimes you need to add 1 to the difference depending on how you're calculating things.

Good luck.

Occasional Contributor
Posts: 16

Re: How to calculate the elapse of time between two dates on SAS

When people say that I need to convert the day, month and years to date values, what do they mean and how do I code this on SAS? My Excel spreadsheets has these values in separate columns. I have one column for just for month, another column for date and another column for years. I am going to change so that my dates will be all in one column in the format of MMDDYYYY.

Please help with figuring out the elapse of time. I am really struggling almost over a month.

Thanks,

GF

Super User
Posts: 17,899

Re: How to calculate the elapse of time between two dates on SAS

Use the mdy() function in SAS.

Both Nick and Linlin have posted examples.

It's equivalent to the date function in Excel but the parameters are in a different order.

Ask a Question
Discussion stats
  • 5 replies
  • 361 views
  • 0 likes
  • 4 in conversation