| Date: | Sat, 14 Jun 2008 22:22:25 -0400 |
| Reply-To: | "Howard Schreier <hs AT dc-sug DOT org>"
<schreier.junk.mail@GMAIL.COM> |
| Sender: | "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU> |
| From: | "Howard Schreier <hs AT dc-sug DOT org>"
<schreier.junk.mail@GMAIL.COM> |
| Subject: | Re: Date field |
|---|
On Tue, 27 May 2008 10:18:26 -0500, data _null_, <datanull@GMAIL.COM> wrote:
>I think a PUT function using DTWKDATX format will work nicely. But
>maybe you don't really need a new variable.
>
>538 data _null_;
>539 length weekday $9 ;
>540 do dt = datetime() by dhms(1,0,0,0) until(weekDay eq: 'Mon');
>541 put 'NOTE: ' dt=datetime18. @;
>542 weekDay = put(dt,dtwkdatx9.-l);
>543 put weekDay;
>544 end;
>545 run;
>
>NOTE: dt=27MAY08:10:15:39 Tuesday
>NOTE: dt=28MAY08:10:15:39 Wednesday
>NOTE: dt=29MAY08:10:15:39 Thursday
>NOTE: dt=30MAY08:10:15:39 Friday
>NOTE: dt=31MAY08:10:15:39 Saturday
>NOTE: dt=01JUN08:10:15:39 Sunday
>NOTE: dt=02JUN08:10:15:39 Monday
>
>
>On 5/27/08, Nanita <susana.urbano@gmail.com> wrote:
>> Hi there,
>>
>> I have a table with a date field (DATA) with the following format:
>>
>> Type - numeric
>> Group - date
>> Format - datetime 18.0
>>
>> and I want to create a new field (day_week) that gives me the day of
>> the week (monday,...).
>>
>> If I do this:
>>
>> _null_
>> set lib.file;
>> day_week = data;
>> /* It gives the sas date DATA= 01apr08:00:00:00) equal to day_week
>> =1522627200)*/
>> Run;
>>
>> I know that I have to format this day_week... but how?
>>
D.N. said "maybe you don't really need a new variable". I think it depends
on the intended usage or usages. Consider:
data demo;
do _n_ = 0 to 7;
atime = dhms( mdy( 5, 23, 1988 ) + _n_, 10, 20, 40 );
btime = atime;
ctime = atime;
dayname = put( atime, DTWKDATX9.-L );
output;
end;
format btime DATETIME20. ctime DTWKDATX9.;
run;
So ATIME, BTIME, and CTIME are the same integer internally, but have
different formats. DAYNAME is a string containing the name of the day. It
all looks line this:
atime btime ctime dayname
896005240 23MAY1988:10:20:40 Monday Monday
896091640 24MAY1988:10:20:40 Tuesday Tuesday
896178040 25MAY1988:10:20:40 Wednesday Wednesday
896264440 26MAY1988:10:20:40 Thursday Thursday
896350840 27MAY1988:10:20:40 Friday Friday
896437240 28MAY1988:10:20:40 Saturday Saturday
896523640 29MAY1988:10:20:40 Sunday Sunday
896610040 30MAY1988:10:20:40 Monday Monday
For PROC FREQ, the formatted datetime is fine:
proc freq data=demo;
tables ctime / nocum;
run;
Result:
ctime Frequency Percent
----------------------------------
Monday 2 25.00
Tuesday 1 12.50
Wednesday 1 12.50
Thursday 1 12.50
Friday 1 12.50
Saturday 1 12.50
Sunday 1 12.50
But if you want to be able to write simple WHERE conditions, you need the
new variable. Trying to get by with the formatted variable as in
proc print data=demo;
where ctime = 'Tuesday';
run;
gets you:
ERROR: Where clause operator requires compatible variables.
whereas there is no problem with:
proc print data=demo;
where dayname = 'Tuesday';
run;
Finally, here's a puzzle:
proc means data=demo n;
class ctime;
var atime;
run;
The output:
Analysis Variable : atime
N
ctime Obs N
----------------------------------
Monday, 23 May 1988 2 2
Tuesday, 24 May 1988 1 1
Wednesday, 25 May 1988 1 1
Thursday, 26 May 1988 1 1
Friday, 27 May 1988 1 1
Saturday, 28 May 1988 1 1
Sunday, 29 May 1988 1 1
----------------------------------
Notice that the two Mondays are rolled up correctly; in that respect the
numeric class variable with the DTWKDATX9. format worked as expected.
However, for display, SAS ignored the width spec and displayed date, month,
and year for each level. Why is that?
|