Solved
Contributor
Posts: 63

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: 13,925

Re: How to calculate a duration from 2 dates?

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.);

All Replies
Super User
Posts: 6,928

Re: How to calculate a duration from 2 dates?

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: 63

Re: How to calculate a duration from 2 dates?

Thank you so much for your quick respond.
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: 6,928

Re: How to calculate a duration from 2 dates?

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: 10,557

Re: How to calculate a duration from 2 dates?

marysmith wrote:
Thank you so much for your quick respond.
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
How to convert datasets to data steps
How to post code
Contributor
Posts: 63

Re: How to calculate a duration from 2 dates?

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]

Super User
Posts: 10,557

Re: How to calculate a duration from 2 dates?

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
How to convert datasets to data steps
How to post code
Super User
Posts: 6,928

Re: How to calculate a duration from 2 dates?

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,400

Contributor
Posts: 63

Re: How to calculate a duration from 2 dates?

This is the Dataset in Excel

Solution
‎01-10-2018 10:49 AM
Super User
Posts: 13,925

Re: How to calculate a duration from 2 dates?

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: 6,928

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: 63

Re: How to calculate a duration from 2 dates?

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