sas >> AW: Difference in days between two different SAS dates: Code results in wrong difefrences

by lfisher » Thu, 20 Nov 2003 02:29:44 GMT

Hi,
I've checked all your wonderful proposols, however, none works.
Code sample:
data DIFF ;
DIFFDAY = range(DATEVAR1, datepart(DATEVAR2) );
set DATA ;
run ;
All result in wrong results. Just see for yourself. You happen to have
an explanation?!

Sample results:
Obs DIFFDAY DATEVAR1 DATEVAR2
1 . 24/11/97 04AUG1997:00:00:00
2 112 31/07/00 21FEB2000:00:00:00
3 161 17/07/00 10DEC1999:00:00:00
4 220 14/03/00 15DEC1999:00:00:00
5 90 29/09/00 09DEC1999:00:00:00
6 295 18/02/00 21FEB2000:00:00:00

Gerhard, your solutions looks very good. I have about 700 entries,
however, I cannot hardcode them like that. Cheers, Les

sas >> AW: Difference in days between two different SAS dates: Code results in wrong difefrences

by ghellrieg » Thu, 20 Nov 2003 02:58:43 GMT


On Wed, 19 Nov 2003 19:29:44 +0100, Leslie Fisher <lfisher@METHOD-



Les, I assumed that you HAVE that data already! I have it not so what
should I do to get examples?
So if you have problems, I think it's not related to all the simple and
good solutions, but in your data!

So lets have a look on your first line:



467 data test;
468 a="24nov97"d;
469 b="04AUG1997:00:00:00"dt;
470 put "a=" a 8. " b=" b 10.;
471 run;

a= 13842 b= 1186272000

Do you see the same if you put the numbers a and b in numeric (note date or
datetime format)?
If you see something like 24/11/97 and the type TEXT, you are wrong and
have to convert that to real dates and datetimes!
Ok, let's hope, everything is all right!

477 data test;
478 a="24nov97"d;
479 b="04AUG1997:00:00:00"dt;
480 dif=range(a,datepart(b));
481 put dif=;
482 run;

dif=112


You get a missing value!

If you find char-variables instead of numerics, you have to convert that:

483 data test;
484 a="24/11/97";
485 b="04AUG1997:00:00:00"dt;
486 dif=range(input(a,ddmmyy8.),datepart(b));
487 put dif=;
488 run;

dif=112

here a is a char variable and the INPUT function is used to convert it to a
date-variable.



**************************************************************
BUT WHAT I REALLY ASSUME: you should try to FIRST open your dataset and
THEN calculate differences, e.g.:

data diff;
set data;
diff=range(....);
run;



and you'll see, all (correct) results will be in the correct observations,
not in the next one!!! - like a miracle!

sas >> AW: Difference in days between two different SAS dates: Code results in wrong difefrences

by peter » Thu, 20 Nov 2003 03:02:33 GMT

On Wed, 19 Nov 2003 19:29:44 +0100, Leslie Fisher <lfisher@METHOD-


Not having your dataset DATA, I created this code
with input cards pasted from the earlier postings
data testdates;
attrib datevar1 format=date9. informat=ddmmyy10.
datevar2 format=datetime19. informat=datetime19. ;
infile cards firstobs=2 truncover ;
input datevar1 datevar2 @1 card $char60.;
DIFFDAY = range(DATEVAR1, datepart(DATEVAR2) );
put card= @70 diffday=;
cards;
DATEVAR1 DATEVAR2 The result could look like DIFFDAY
24/11/97 25NOV1997:00:00:00 1
14/12/00 16DEC2000:00:00:00 2
17/02/00 21FEB2000:00:00:00 4
17/07/00 10DEC1999:00:00:00 etc.
;

This worked, producing these lines in my log
card=24/11/97 25NOV1997:00:00:00 1 DIFFDAY=1
card=14/12/00 16DEC2000:00:00:00 2 DIFFDAY=2
card=17/02/00 21FEB2000:00:00:00 4 DIFFDAY=4
card=17/07/00 10DEC1999:00:00:00 et DIFFDAY=220
NOTE: The data set WORK.TESTDATES has 4 observations and 4 variables.

so there is something about the attributes
in your DATA

Similar Threads

1. Difference in days between two different SAS dates: Code results in wrong difefrences

2. Difference in days between two different SAS dates: Code resu lts in wrong difefrences

Leslie:
Try:
       data  DIFF ;
          set DATA ;
             DIFFDAY  = range(DATEVAR1, datepart(DATEVAR2) );
       run ;
Sig
-----Original Message-----
From: Leslie Fisher [mailto: XXXX@XXXXX.COM ]
Sent: Wednesday, November 19, 2003 1:30 PM
To:  XXXX@XXXXX.COM 
Subject: AW: Difference in days between two different SAS dates: Code
results in wrong difefrences


Hi,
I've checked all your wonderful proposols, however, none works. Code sample:
       data  DIFF ;
       DIFFDAY  = range(DATEVAR1, datepart(DATEVAR2) );
       set DATA ;
       run ;
All result in wrong results. Just see for yourself.  You happen to have an
explanation?!

Sample results:
Obs         DIFFDAY     DATEVAR1             DATEVAR2
  1                            .       24/11/97      04AUG1997:00:00:00
  2                       112       31/07/00      21FEB2000:00:00:00
  3                       161       17/07/00      10DEC1999:00:00:00
  4                       220       14/03/00      15DEC1999:00:00:00
  5                         90       29/09/00      09DEC1999:00:00:00
  6                       295       18/02/00      21FEB2000:00:00:00

Gerhard, your solutions looks very good. I have about 700 entries, however,
I cannot hardcode them like that. Cheers, Les

3. Difference in days between two different SAS dates

4. AW: Friday Contest: What's wrong with this code?

5. SOLVED: Difference in days between SAS date and datetime THANK YOU

6. How to Compare two different informat dates using SAS

Data _Null_ ;

Date1 = '09OCT2006:09:03' ;
Date2 = '09OCT2006:09:03:00'dt ;

Compare = ( Input( Date1 , DateTime15. ) = Date2 ) ;

Put _All_ ;
Run ;



Toby Dunn


You never know what is enough, until you know what is more than enough.
~William Blake, Proverbs of Hell


When the student is ready, the master appears.  ~Buddhist Proverb


Only that in you which is me can hear what I'm saying.  ~Baba Ram Dass





From: Suthakar Iyer < XXXX@XXXXX.COM >
Reply-To: Suthakar Iyer < XXXX@XXXXX.COM >
To:  XXXX@XXXXX.COM 
Subject: How to Compare two different  informat  dates using SAS
Date: Thu, 2 Nov 2006 17:12:16 -0800

Hi,

   date1 = 09OCT2006:09:03     this date in the $16. informat
   date2 = 09OCT2006:09:03:00   this date in the datetime20. informat.

   In the date1 and date2 , I want to compare only the first 15 positions in
both dates.
   Since both are in different informats, I am not able to do it.

   Thanks for the help .

   Suthakar


---------------------------------
Get your email and see which of your friends are online - Right on the  new
Yahoo.com

_________________________________________________________________
Use your PC to make calls at very low rates
https://voiceoam.pcs.v2s.live.com/partnerredirect.aspx

7. How to Compare two different informat dates using SAS

8. how to calculate days by using different format date variables

I have two date variables. One is best12.(format), 12. (informat);
another is DDYYMM6.(Both format and informat)

When I the sas code as follows, it does not work, why?

Data a;
set b;
format A yymmdd10. B yymmdd10.;
c=B-A;
run;

or,

Data a;
set b;
format A date9. B date9.;
c=B-A;
run;

or

Data a;
set b;
format A best12. B best12.;
c=B-A;
run;

Could you please tell me what's wrong with this? and how can I get the
days between A and B?

Thanks,
Chunyuan