- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Is it possible to define your own datetime format? Or is there an existing format that I didn't find? My goal is "yyyy-mm-dd hh:mm:ss", for example, "2018-09-15 14:52:22". My example below prints a couple close options, but not exactly what I would like. And I'm actually more interested in writing this format to a csv file, if somehow that permits a different solution than printing.
Thanks,
Chris
data a;
mydt = '15sep2018 14:52:22'dt;
proc print data=a; /* 1852642342 */
proc print data=a;
format mydt e8601dt25.0; /* 2018-09-15T14:52:22 */
proc print data=a;
format mydt nldatms.; /* 09/15/2018 14:52:22 */
run;
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Yes very possible:
proc format; picture customdt low-high ='%Y-%0m-%0d %0H:%0M:%0S' (datatype=datetime); run; data a; mydt = '15sep2018 14:52:22'dt; run; proc print data=a; format mydt customdt.; run;
You left out some details though. For the time component do you want leading zero: 08:15:30 or 8:15:30?
In the Picture the % starts a directive. Proc format has a bunch of these related to different possible appearance of date, time or datetime options. The characters such a : or blank are treated literally so you can insert which ever separator you might like or none as needed.
Caution: this is one of the few places that you really want to use single quotes around the definition as if you use double quotes then SAS will think you may be attempting to use macros, which are almost certainly not defined, or if they are defined aren't for use in the picture statement.
You have to provide the data type so the range of the underlying numeric value is treated correctly.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Yes very possible:
proc format; picture customdt low-high ='%Y-%0m-%0d %0H:%0M:%0S' (datatype=datetime); run; data a; mydt = '15sep2018 14:52:22'dt; run; proc print data=a; format mydt customdt.; run;
You left out some details though. For the time component do you want leading zero: 08:15:30 or 8:15:30?
In the Picture the % starts a directive. Proc format has a bunch of these related to different possible appearance of date, time or datetime options. The characters such a : or blank are treated literally so you can insert which ever separator you might like or none as needed.
Caution: this is one of the few places that you really want to use single quotes around the definition as if you use double quotes then SAS will think you may be attempting to use macros, which are almost certainly not defined, or if they are defined aren't for use in the picture statement.
You have to provide the data type so the range of the underlying numeric value is treated correctly.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You can use the PICTURE statement within PROC FORMAT. There may be a format out there already, but sometimes you have to make your own.
data a;
mydt = '15sep2018 14:52:22'dt;
run;
proc format;
picture customtm
other = '%Y-%0m-%0D %0H:%0M:%0S' (datatype = datetime);
run;
Obs mydt 1 2018-09-15 14:52:22
Here is a link to the PICTURE statement documentation. You can look around underneath the round header and see all the custom formatting you can do with dates and times.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
https://sasnrd.com/sas-date-datetime-proc-format-example/
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content