SAS 9.3 DATETIME VARIABLE FORMAT AS DATE

2020-02-11 11:28发布

I have a datetime22.3 variable which I would like to display as date.

for eg I want to display 17JUL2006:00:00:00.000 as 07/17/2006

How do I do this?

Thanks.


additional info:

Thanks for all the replies.

Actually, I tried to output it in the date format within proc sql. The output is being printed as ********** (stars). I am not sure what is going on.

I am trying to use INTCK in the following manner but get error. I am not sure what I am doing wrong. I would appreciate your help. Thanks.

PROC FORMAT;
PICTURE DTFMT LOW-HIGH='%0m/%0d/%Y'  (DATATYPE=DATETIME);
RUN;

data want;
dt_val1='17JUL2006:00:00:00.000'dt;
dt_val2='17AUG2012:00:00:00.000'dt;
format dt_val1 dt_val2 dt_val3 dtfmt.;
dt_val3=intck('MONTH',dt_val1,dt_val2);
put dt_val3;
run; 

标签: datetime sas
8条回答
Anthone
2楼-- · 2020-02-11 11:52

If you want to display the variable's contents as a date (without changing the underlying value), use a FORMAT statement.

proc print;
    format dte mmddyys10.;
run;

proc means;
    class dte;
    format dte mmddyys10.;
run;

etc. Note that you can also put the FORMAT in a data step, in which case any uses of the variable will automatically pick it up.

data foo;
    format dte mmddyys10.;
run;
查看更多
Animai°情兽
3楼-- · 2020-02-11 11:52

Converts date/time var to char date var:

BLEndDatex = put(datepart(BLEndDateTime),yymmdd10.);

Create numeric sas date without time:

BLEndDate = mdy(SUBSTR(BLEndDatex,6,2),SUBSTR(BLEndDatex,9,2),SUBSTR(BLEndDatex,1,4));

Thanks to Rizier123 & Heemin posting above to the first portion.

查看更多
Bombasti
4楼-- · 2020-02-11 11:54
put(datepart(datetimevariable),yymmdd10.)
查看更多
欢心
5楼-- · 2020-02-11 12:01

use the same princpiple in a data step

data _null_;
   a='17JUL2006:00:00:00.000'd;
   put a;
   put 'formatted date='a MMDDYY10.;
run;

This is the output from my SAS 9.3

44   data _null_;
45   a = '17JUL2006:00:00:00:000'D;
46   put a;
47   put 'formatted ' a MMDDYY10.;
48   run;

16999
formatted 07/17/2006
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
查看更多
乱世女痞
6楼-- · 2020-02-11 12:02
data want;
dt_val1='17JUL2006:00:00:00.000'dt;
dt_you_want=input(substr(put(dt_val1,datetime22.3),1,9),date9.);
format dt ddmmyy10.;
run; 
查看更多
▲ chillily
7楼-- · 2020-02-11 12:03

You can't apply a standard date format directly against a datetime value, although there are some date formats you can prefix with 'DT' which will display a datetime as a date. Unfortunately the MMDDYY format is not one of these, however you could use DTDATE9. which would format your datetime as '17JUL2006'.

Another option is create your own format using the PICTURE statement, the example below will display the datetime as required.

proc format;
picture dtfmt low-high='%0m/%0d/%Y' (datatype=datetime);
run;

data want;
dt_val='17JUL2006:00:00:00.000'dt;
format dt_val dtfmt.;
run;
查看更多
登录 后发表回答