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
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.);
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.);
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.
@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.
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.
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.
Provide some sample of your data. That makes it so much easier to help you 🙂
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.);
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.);
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.