BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
marysmith
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

12 REPLIES 12
Astounding
PROC Star

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

marysmith
Calcite | Level 5
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!
Astounding
PROC Star

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.

Kurt_Bremser
Super User

@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.

marysmith
Calcite | Level 5
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. -##
Kurt_Bremser
Super User

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.

Astounding
PROC Star

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.

PeterClemmensen
Tourmaline | Level 20

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

marysmith
Calcite | Level 5

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

ballardw
Super User

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

Astounding
PROC Star

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

marysmith
Calcite | Level 5
Thank you so much! It worked!

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
  • 12 replies
  • 5468 views
  • 2 likes
  • 5 in conversation