How to calculate a duration from 2 dates?

Accepted Solution Solved
Reply
Contributor
Posts: 20
Accepted Solution

How to calculate a duration from 2 dates?

Heyall!

Can anybody help me?

I have a big dataset with over 300 observations and I have the startingdate and the endingdate dd/mm/yyyy and I want to calculate the duration for each observation. Does anybody know how I can calulate the duration time in days from those two dates?

Thank you so much! Cheers, Mary


Accepted Solutions
Solution
‎01-10-2018 10:49 AM
Super User
Posts: 12,148

Re: How to calculate a duration from 2 dates?

Posted in reply to marysmith

Your date variables are character. You will need to convert them in data step before calculating the duration.

 

Sdate = input(startdate,ddmmyy10.);

Edate= input(enddate,ddmmyy10.);

 

would create the SAS valued date variables and use Sdate and Edate for the duration.

If you want to see the values of Sdate and Edate in a date format apply that such as

Format sdate edate ddmmyy10.);

View solution in original post


All Replies
Super User
Posts: 5,963

Re: How to calculate a duration from 2 dates?

Posted in reply to marysmith

If your dates are stored the way SAS expects dates to be stored, you can simply use:

 

duration = ending_date - starting_date;

 

Depending on what you consider the duration to be you could conceivably add 1 to that number.

 

If your dates are character strings, you need them converted to the form that SAS expects to find for dates.  You could use:

 

duration = input(ending_date, ddmmyy10.) - input(starting_date, ddmmyy10.);

Contributor
Posts: 20

Re: How to calculate a duration from 2 dates?

Posted in reply to Astounding
Thank you so much for your quick respond. Smiley Happy
Unfortunately its still not working. It tells me: ERROR: Libref SAS is not assigned.
So sorry to bother you, but I am totally new at this and have to learn it all by myself for my new job.
This is what I have so far:

Libname saspms "C:\SAS_PMS";

PROC IMPORT OUT= SASPMS.DATENSATZ_PMS
DATAFILE= "C:\SAS_PMS\Datensatz_PMS.xls"
DBMS=EXCELCS REPLACE;
RANGE="Sheet1$";
SCANTEXT=YES;
USEDATE=YES;
SCANTIME=YES;
RUN;

Proc print
data = saspms.datensatz_pms;
run;


data saspms.datensatz_pms;
set saspms.datensatz_pms (keep=NIS_Nummer Art_der_Anzeige notificationdate Medicinalproduct StartingDate EndingDate Numberofpatients Numberofphysicians);
run;
Proc print
data = saspms.datensatz_pms;
run;

data sas.datensatz_pms;
set sas.datensatz_pms;
duration = Endingdate - Startingdate;
run;
Thank you so much!
Super User
Posts: 5,963

Re: How to calculate a duration from 2 dates?

Posted in reply to marysmith

Take a look at the last two steps in  your program.  You are working with a data set named:

 

saspms.datensatz_pms

 

Then you changed the name to something else that doesn't exist:

 

sas.datensatz_pms

 

Go back to the correct name, and we'll see what happens.  It might work, or the dates might need some conversion.  It's not possible to tell right now.  Excel handles dates differently than SAS.

Super User
Posts: 8,589

Re: How to calculate a duration from 2 dates?

Posted in reply to marysmith

marysmith wrote:
Thank you so much for your quick respond. Smiley Happy
Unfortunately its still not working. It tells me: ERROR: Libref SAS is not assigned.


Very simple. You assigned library "saspms" and used it (and I guess successfully), and then you suddenly use library "sas" in the last step.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 20

Re: How to calculate a duration from 2 dates?

Posted in reply to KurtBremser
Ah yeah! You`re right. Thank you so much!
But still unfortunately not working.
This ist the dataset, its an excel file
[cid:image001.png@01D38A2D.03B6CF30]

##- Please type your rely above this line. No attachments. -##
Super User
Posts: 8,589

Re: How to calculate a duration from 2 dates?

Posted in reply to marysmith

Do never post data as pictures. Convert SAS datasets to data steps, using the macro from https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... or write the data step yourself. Post the data step code according to the advice in https://communities.sas.com/t5/help/faqpage/faq-category-id/posting?nobounce

Note: Excel files are not datasets and cannot be reasonably used to represent datasets; they have no defined vertical structure, so important attributes of SAS datasets are lost when data is exported.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 5,963

Re: How to calculate a duration from 2 dates?

Posted in reply to marysmith

Your sample program includes a PROC PRINT toward the end.  Are you able to post a few lines from that, showing the Starting Date and the Ending Date? 

 

It would also be helpful if you could run a PROC CONTENTS on  your final data set, and post the information generated about those two variables.

PROC Star
Posts: 1,067

Re: How to calculate a duration from 2 dates?

Posted in reply to marysmith

Provide some sample of your data. That makes it so much easier to help you Smiley Happy

Contributor
Posts: 20

Re: How to calculate a duration from 2 dates?

This is the Dataset in ExcelThis is the Dataset in Excelproc_contents.JPG

Solution
‎01-10-2018 10:49 AM
Super User
Posts: 12,148

Re: How to calculate a duration from 2 dates?

Posted in reply to marysmith

Your date variables are character. You will need to convert them in data step before calculating the duration.

 

Sdate = input(startdate,ddmmyy10.);

Edate= input(enddate,ddmmyy10.);

 

would create the SAS valued date variables and use Sdate and Edate for the duration.

If you want to see the values of Sdate and Edate in a date format apply that such as

Format sdate edate ddmmyy10.);

Super User
Posts: 5,963

Re: How to calculate a duration from 2 dates?

While converting the dates is a good idea, there is an alternative.  Instead, you can use the formula from my original post:

 

duration = input(ending_date, ddmmyy10.) - input(starting_date, ddmmyy10.);

Contributor
Posts: 20

Re: How to calculate a duration from 2 dates?

Thank you so much! It worked!
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 12 replies
  • 134 views
  • 2 likes
  • 5 in conversation