Date: Fri, 2 Apr 2010 12:21:21 -0700
Reply-To: "Schwarz, Barry A" <barry.a.schwarz@BOEING.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "Schwarz, Barry A" <barry.a.schwarz@BOEING.COM>
Subject: Re: Sorting of dates with and without the time part
In-Reply-To: <j2hc2192a611004021126ha0d123b7s8a3738e7b8415179@mail.gmail.com>
Content-Type: text/plain; charset="us-ascii"
You are mixing apples and oranges and compounding the problem by calling them pears and pineapples.
Data set test starts out with two numeric variables only, a and b, in one observation. You then add two character variables to this observation:
time = '2009 10-06'
time1 = '2009-09 18T12:10:00'
You seem to think that SAS should somehow recognize the meaning of the numeric variables in test1. That is not how it works. Date and datetime values are simply numbers. Proc Sort has no idea that the smaller number represents a date in October while the larger one represents a date in September. Assigning a format to the variable would not help because that only determines how the number is converted to readable characters in the put function or the put statement.
If you want to sort test1 in chronological order, you will have to give the sort program values that are "chronologically consistent". One way would be to produce a character value for each numeric value as you did in test. (The fact that some do not have a time component will not interfere with sorting by date.) A second way would be to convert only the datetime values to date values with something like a = datepart(a). In both cases, your code has to be able to distinguish between a date value and a datetime value, perhaps by checking for a value larger than 1,000,000. (This is not appropriate if you have values near the start of the epoch but then you should never have used the same variable for both date and datetime unless you already had a way to tell the difference.)
-----Original Message-----
From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of SAS_learner
Sent: Friday, April 02, 2010 11:27 AM
To: SAS-L@LISTSERV.UGA.EDU
Subject: Sorting of dates with and without the time part
Hello all,
I have a dataset with Character dates some of them have time part and some
of them do not. When I make those char dates as Numeric and sort them the
dates with missing time part are coming up (giving me wrong sort order ) how
do I approach this problem
Here is example what I am talking
data test ;
a = 18176 ;
b = 1568895000 ;
Run;
Data test ;
set test ;
time = Put (b , Is8601dt. ) ;
time1 = Put (a , Is8601da. ) ;
run;
data test1 ;
a = 18176 ;
a = 1568895000 ;
Run;
*Sorting this dataset would give me 18176 first and 1568895000 last *;
Proc Sort data = test1 ;
By a ;
Run ;