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条回答
放荡不羁爱自由
2楼-- · 2020-02-11 12:03

Formatting dates in SAS can be tricky. One method I've used in a macro is this:

/* get the date time */
%let start_date=%sysfunc(datetime().,10);
/* use the DATETIME informat to format the date */
%let fmt_start_date=%sysfunc(putn(&start_date, DATETIME.)); 
/* format the datetime as a date */
%put "&fmt_start_date."d;

There's a bunch of different ways to format dates. You could also use the FORMAT statement if you're in a data step:

FORMAT STARTDATE YYMMDD10.;

In this case, the format of the column in the data step would give you YYYY-MM-DD and then you can separate the values and reconstruct from there.

There's additional information about SAS informats for dates here: http://support.sas.com/documentation/cdl/en/etsug/60372/HTML/default/viewer.htm#etsug_intervals_sect008.htm

And here: http://support.sas.com/documentation/cdl/en/etsug/63348/HTML/default/viewer.htm#etsug_intervals_sect009.htm

If you need more info or examples, please let me know.

Best of luck!

查看更多
地球回转人心会变
3楼-- · 2020-02-11 12:07

My answer from a duplicate question:

You need to convert original SAS DATETIME value (think of it as data type) to SAS DATE value using DATEPART() function and apply appropriate format:

proc sql; 
 create table work.abc2
 as select *, DATEPART(a.Billing_Dt) format ddmmyy10. as Bill_date
from abc;
quit;

So the point is, as Keith pointed above, to apply appropriate type of format (e.g. ddmmyy10. is the SAS Date format) to appropriate values = variable content (e.g. (unformatted) 10 is 11th January 1960 if used as date, while it's 01JAN60:00:00:10 if used as Datetime value), so you have to be sure what your values represent and convert the values if needed.

查看更多
登录 后发表回答