Programming the statistical procedures from SAS

Difference between dates

Accepted Solution Solved
Reply
Regular Contributor
Posts: 163
Accepted Solution

Difference between dates

Hi

I have imported an excel file containing two dates columns, formated as yymmdd

I am trying to find the difference between these two columns

I used the code below but it is returning some strange results

data want;

set have;

format regtime start_date yymmdd10.;

NumDays = intck( 'day', start_date, regtime );

run;


Any help is appreciated


Regards



Accepted Solutions
Solution
‎01-21-2015 02:01 PM
Occasional Contributor
Posts: 15

Re: Difference between dates

I think it is the way that you are importing these.

If you just use a simple proc import, it will import these as numeric and not as SAS dates.

SAS dates are also numeric, but they reference the number of days since 1/1/1960.

What you are getting are the numeric values

Obs     Start_date        regtime

  1         120305         140224
  2         120319         140224
  3         120227         140224

You need to convert these to SAS dates or import them as SAS dates.

To convert them to SAS dates, you can do something like

data want;

     set have;

     sas_start_date = input(compress(put(start_date,best8.)),yymmdd6.);

     sas_regtime = input(compress(put(stat_date,best8.)),yymmdd6.);

     NumDays = sas_regtime - sas_start_date;

run;

View solution in original post


All Replies
Super Contributor
Posts: 490

Re: Difference between dates

post sample from the have dataset

Super Contributor
Posts: 276

Re: Difference between dates

Your CODE is Fine.

Better to see your example.

Trusted Advisor
Posts: 1,137

Re: Difference between dates

I believe the strange result is because of the date format used.

could you please convert the dates to date9. format and then use the intck function.

Thanks,

Jag

Thanks,
Jag
Respected Advisor
Posts: 4,021

Re: Difference between dates

The date format applied has no influence on the result returned by the intck() function as the function uses the internal numeric value.

Super User
Super User
Posts: 7,565

Re: Difference between dates

You don't actually need that function.  If both of your variables are date variables, then they are stored as the number of days since a certain timepoint.  Therefore if you subtract one from the other you get the number of days between:

num_days = start_date - regtime;

However, I also concur with the above posters, post example data as the term Regtime indicates that you are looking at a time variable not a date.  Or possible a date/time variable.  If so then just do:

num_days = start_date - datepart(regtime);

Regular Contributor
Posts: 163

Re: Difference between dates

Here is a sample of the file

The variables are not formated as date in the Excel file but I thought it wouldn't be a problem as I am applying a date format in the SAS code then

As I mentioned, the date formate here is yymmdd

Thanks in advance

Super User
Super User
Posts: 7,565

Re: Difference between dates

Yes, as BurntDirt has mentioned those are just numbers, and it is just guessing what they represent, is 120305 = 12Mar05, or 05Mar12 or something else??  Also little tip, don't use proc import as it basically guesses what you want to do.  There are many ways to get data in, I would advise that flat text file delimited, e.g. CSV, is best.  Then write a datastep import program specifically tailored to the data you know best.

Solution
‎01-21-2015 02:01 PM
Occasional Contributor
Posts: 15

Re: Difference between dates

I think it is the way that you are importing these.

If you just use a simple proc import, it will import these as numeric and not as SAS dates.

SAS dates are also numeric, but they reference the number of days since 1/1/1960.

What you are getting are the numeric values

Obs     Start_date        regtime

  1         120305         140224
  2         120319         140224
  3         120227         140224

You need to convert these to SAS dates or import them as SAS dates.

To convert them to SAS dates, you can do something like

data want;

     set have;

     sas_start_date = input(compress(put(start_date,best8.)),yymmdd6.);

     sas_regtime = input(compress(put(stat_date,best8.)),yymmdd6.);

     NumDays = sas_regtime - sas_start_date;

run;

🔒 This topic is solved and locked.

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

Discussion stats
  • 8 replies
  • 513 views
  • 4 likes
  • 7 in conversation