Write and run SAS programs in your web browser

Averaging time/duration with dates

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

Averaging time/duration with dates

I was wondering if anyone could help me find ea simple way or code that would give me the difference in dates in like months or any unit of time? For example, I need the first line of date 2 :28/05/2015 or (May 28 2015) subtracted from Date 1: 18/08/2007 or (August 18 2007) which would be approximately a duration of 8 years. Then for all the observations for those durations an average in years or months? Would I have to recode this date? Which procedure would I use?

 

 

 

 

Date 2

28/05/2015
15/06/2015
23/04/2015
12/12/14
18/06/2015
18/06/2015
11/3/15
4/12/14
14/04/2015
18/12/2014
12/3/15
23/04/2015

 

 

Date 1
18/08/2007
15/01/2013
24/07/2014
22/05/2013
1/12/12
26/03/2015
23/09/2014
7/8/13
7/3/08
17/12/2012
7/1/12
25/09/2014

 


Accepted Solutions
Solution
‎06-19-2017 08:55 PM
Super User
Posts: 18,528

Re: Averaging time/duration with dates

YOU DO NOT NEED TO TYPE IN ANY DATES. THAT WAS TO ILLUSTRATE AN EXAMPLE, USE YOUR DATA.

RUN THE EXACT CODE BELOW.

 

 

 

FILENAME REFFILE '/folders/myfolders/sasuser.v94/symptoms.xlsx';

PROC IMPORT DATAFILE=REFFILE
	DBMS=XLSX
	OUT=UGAStudent_DATA;
	GETNAMES=YES;
	SHEET="Master";
RUN;

data date_difference ; *New dataset being created;
  
set UGAStudent_DATA; *your imported data;

date_diff = art_date - int_date; *calculate date difference;

run;

View solution in original post


All Replies
Super User
Posts: 18,528

Re: Averaging time/duration with dates

1. Your dates are likely in character format. You're going to have make them consistent first, this usually means using INPUT to convert to a SAS date. 

2. Review how SAS stores dates - ie as the number of days since Jan 1, 1960

3. How do you know which dates you need to calculate from? 

4. Since #2 is true, you can subtract dates directly for duration.

 

data sample;
d1 = '01Jan2017'd;
d2 = '31Jan2017'd;

n_days = d2 - d1;
format d1 d2 mmddyy10.;

run;

proc print data=sample;
run;

 

 

Occasional Contributor
Posts: 8

Re: Averaging time/duration with dates

Thanks! I’ve been able to calculate one date, but I have 477 observations. Is their a way i can do multiple dates at one time? Everything Ive tried so far hasn’t worked. The only thing that shows are the first date.
Super User
Posts: 18,528

Re: Averaging time/duration with dates

Post what you've tried. And post what you want based on your supplied data above. 

 

 

Occasional Contributor
Posts: 8

Re: Averaging time/duration with dates

 The dates for d1(inital test)  and d2 (final test) are listed in column form like this and each line refers to one indivual. Im trying to get the duration for each individual. 

 

d2

04NOV2014

05JAN2015

28MAY2015

02JUN2015

02JUN2015

13APR2015

 

d1

05MAY2007

15OCT2012

26JUN2014

22FEB2013

19OCT2012

26NOV2014

12AUG2014

03JUL2012

 

 

Ive tried putting the multple dates in order and hoping they would correalte with eachother and give me the time in months in the final column 

data sample;

d1 = '05MAY2007' 15OCT2012'd;

 

d2 = '04NOV20145'  '05JAN2015'd;

 

n_month = d2 - d1;

n_month=intck('month',d1,d2);

format d1 d2 date9.;

 

run;

proc print data=sample;

run;

 

Ive also tried to list the dates vertixally as  i was able to conver the char variables to sas dates like below but nothing has seemed to work. 

 

d1 = '05MAY2007'd;

'15OCT2012'd;

 

d2 = '04NOV20145'd;

 '05JAN2015'd;

 

n_month = d2 - d1;

n_month=intck('month',d1,d2);

format d1 d2 date9.;

 

run;

proc print data=sample;

run;

Super User
Posts: 7,094

Re: Averaging time/duration with dates

Wait!

 

Do you have a text file that contains the data for two columns in succession?

Then you must first put these side-by-side.

 

Please post an example (use the {i} box for correct posting) of your input data as-is.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 8

Re: Averaging time/duration with dates

I have attached two files, one in an excel file and one with a word document if thats what you mean by text file? Heres the code im trying to use to calculate the difference. 

data sample;
d1 = '24JAN2002'd;


d2 = '26MAR2015'd;

n_month = d2 - d1;
n_month=intck('month',d1,d2);
format d1 d2 date9.;

run;

proc print data=sample;
run;
Super User
Posts: 18,528

Re: Averaging time/duration with dates

The sample was to demonstrate how to do a calculation. I didn't expect you to type in your data. 

 

For first step should be to import your data into SAS, that may mean using proc import or an import task. 

Once you have a SAS dataset you can refer to your variables directly.

 

Please see the tutorials here:

support.sas.com/training/tutorials

 

There are YouTube videos as well as a link to the free e-course.

Occasional Contributor
Posts: 8

Re: Averaging time/duration with dates

Yes  I have successfully imported my data and im using sas studio online.

 

/* Generated Code (IMPORT) */
/* Source File: symptoms.xlsx */
/* Source Path: /folders/myfolders/sasuser.v94 */
/* Code generated on: 6/16/17, 2:03 PM */

%web_drop_table(WORK.IMPORT);


FILENAME REFFILE '/folders/myfolders/sasuser.v94/symptoms.xlsx';

PROC IMPORT DATAFILE=REFFILE
	DBMS=XLSX
	OUT=WORK.IMPORT;
	GETNAMES=YES;
	SHEET="Master";
RUN;

PROC CONTENTS DATA=WORK.IMPORT; RUN;


%web_open_table(WORK.IMPORT);

 

 

The two variables of interest are in my data set which are Int_date and ART_date. I was able to copy and past all the values of each variable into the below command i found on the sas webside to convert them to sas dates and they gave me an output of converted dates. 

data work.import;
   input   chardate3 $10. ;
   datalines;
  21/09/2005 
;

data work.import;
   set one;
  
   sasdate3=input(chardate3,ddmmyy10.);
  
   format   sasdate3 date9.  ;
run;

proc print;
run;

 

I then took the output data and tried to copy it into the below code so I coud convert them but i am unablee to get more than one subtraction. 

 

 

data work.import;
ART_date = '24JAN2002'd;


Int_date = '26MAR2015'd;

n_month = Int_date - ART_date;
n_month=intck('month',ART_date, Int_date);
format ART_date Int_date date9.;

run;

proc print data=work.import;
run;
Super User
Posts: 18,528

Re: Averaging time/duration with dates

Your first set of code is correct, the latter is not.

 

It should be a single step and your actually erasing your imported data when you do that. 

 

data date_difference ; *New dataset being created;
  
set import; *your imported data;

date_diff = art_date - int_date; *calculate date difference;

run;
Occasional Contributor
Posts: 8

Re: Averaging time/duration with dates

I replaced the second half but I am still unable to input more than one date and get an output of more than one date. The code beow gives me an output where it shows the difference in days. Where should i add the additional dates because when i add them to the spots where '24JAN2003' and '26MAR2015' the code will not run

art_date = '24JAN2003'd;
int_date = '26MAR2015'd;


data date_difference ; *New dataset being created;
set import; *your imported data;
date_difference = int_date- art_date; *calculate date difference;

run;

proc print data=work.import;
run;
Solution
‎06-19-2017 08:55 PM
Super User
Posts: 18,528

Re: Averaging time/duration with dates

YOU DO NOT NEED TO TYPE IN ANY DATES. THAT WAS TO ILLUSTRATE AN EXAMPLE, USE YOUR DATA.

RUN THE EXACT CODE BELOW.

 

 

 

FILENAME REFFILE '/folders/myfolders/sasuser.v94/symptoms.xlsx';

PROC IMPORT DATAFILE=REFFILE
	DBMS=XLSX
	OUT=UGAStudent_DATA;
	GETNAMES=YES;
	SHEET="Master";
RUN;

data date_difference ; *New dataset being created;
  
set UGAStudent_DATA; *your imported data;

date_diff = art_date - int_date; *calculate date difference;

run;
Trusted Advisor
Posts: 1,137

Re: Averaging time/duration with dates

[ Edited ]

with reference to what @Reeza mentioned you could try the intck function as well to get the difference in day, month, year etc., in the intck function you have to replace the first argument with any of the above words

 

n_days=intck('day',d1,d2);

 

 

Thanks,
Jag
☑ This topic is solved.

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

Discussion stats
  • 12 replies
  • 156 views
  • 0 likes
  • 4 in conversation