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

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

12 REPLIES 12
Reeza
Super User

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;

 

 

UGAstudent
Calcite | Level 5
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.
Reeza
Super User

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

 

 

UGAstudent
Calcite | Level 5

 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;

Kurt_Bremser
Super User

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.

UGAstudent
Calcite | Level 5

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;
Reeza
Super User

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.

UGAstudent
Calcite | Level 5

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;
Reeza
Super User

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;
UGAstudent
Calcite | Level 5

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;
Reeza
Super User

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;
Jagadishkatam
Amethyst | Level 16

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

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 1641 views
  • 0 likes
  • 4 in conversation