I have a DateTime variable (default formatting), and I would like to format it to a format to I receive from a string parameter.
I normally do something similar to: {myDate:yyyy-MM-dd}, and it works properly.
Now I have a lot of possible date formats and need to format according to the chosen one.
I have tried the following but returned garbage (ae0aor0aa):
string testFormat = "yyyy. MM. dd.";
{myDate:testFormat }
I have also tried to convert the date to string and back to date with ParseExact, but gave me an invalid date exception. NB: the date in myDate is valid, as I have checked it with the debugger.
Can you kindly advise?
Thanks to apc, it was easily solved by myDate.ToString(testFormat)
I have a column having date format in DD/MM/YYYY format in Progress 4 GL.
How can I extract YEAR from this column.
DISP YEAR(column_name) does not return anything.
Can someone please help?
You really should show your real code and provide the actual data definitions.
And the actual text of error messages. Even if you think they are meaningless.
Just because the data is formatted like a date doesn't mean it is actually stored as a date. This sounds like it is probably a character field with a converted date stored. Although if that were the case you should have also seen an error message.
Does
display substring( column_name, 7, 4 ).
show you the result that you are hoping for?
If you have standard format DD/MM/YYYY, then you could extract year value with :
DEFINE VARIABLE strYear AS CHARACTER NO-UNDO.
/* Convert it to date again when you need to store it to DB */
strYear = ENTRY(3, STRING(column_name), "/").
DISP strYear.
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;
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;
put(datepart(datetimevariable),yymmdd10.)
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
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!
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;
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.
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;
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.
I want SAS to format datetimes in a POSIX format. The following gives "2009-11-25 14:44:56" how can I display the milliseconds ?
proc format;
picture POSIX other='%0Y-%0m-%0d %0H:%0M:%0S' (datatype=datetime);
run;
data test;
mydatetime='25nov2009 14:44:56.300'dt;
format newdt POSIX.;
newdt=mydatetime;
put mydatetime= newdt=;
run;
EDIT : I am even taker of ANY way to format datetimes to YYYY-MM-DD HH:M:SS.sss (ISO8601) amazing I can't find this in less than 1 minute
E8601DT23.3 should display your values as you wish, except for the extra "T" separator; SAS seems to require that. If you're putting to a character value you can remove the "T" easily; if you are trying to use a formatted numeric value, I think you would have to live with it.
data test;
mydatetime='25nov2009 14:44:56.356'dt;
format newdt E8601DT23.3;
newdt=mydatetime;
put mydatetime= newdt=;
run;
SAS guide on ISO8601:
http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a003169814.htm
Edit: SAS-L came through on this one ( http://listserv.uga.edu/cgi-bin/wa?A1=ind1303c&L=sas-l#8 ). If you have 9.3 this should work (not in 9.2 or earlier).
proc format;
picture POSIX other='%0Y-%0m-%0d %0H:%0M:%0s' (datatype=datetime);
run;
data test;
mydatetime='25nov2009 14:44:56.300'dt;
format newdt POSIX23.3;
newdt=mydatetime;
put mydatetime= newdt=;
run;
Little s, and include the decimal, and it should work as expected. Thanks to data null for the tip.
This solution only works for values of second from 10-59. It does not work for values of second 0-9. There is no leading zero for values of second 0-9.
I am trying to display a datetime in the format yyyy-mm-dd hh:mm (e.g. 2012-12-31 23:59)
In PHP I would normally use the format YYYY-mm-dd HH:ii to get what I want. I have been looking through the SAS knowledge base and the closest I can get is E8601DTw.d which provides 2008-09-15T15:53:00 which includes seconds as well as a "T" where I'd like a space.
Is there a format to do what I'd like? If not, is there a way to create my own? I don't know that much SAS myself I'm just trying to modify an existing system. Any help is appreciated.
If the standard datetime formats provided do not meet your requirements you can create a new format:
PROC FORMAT;
picture MyMSdt other='%0Y-%0m-%0d %0H:%0M' (datatype=datetime);
RUN;
DATA TEST;
mydatetime='25nov2009 14:44:56'dt;
format newdt MyMSdt.;
newdt=mydatetime;
put mydatetime= newdt=;
RUN;
Taken from this example that you can easily customize.