Desktop productivity for business analysts and programmers

Need Date Format like YYYYMMDD HH24:MI:SS

Accepted Solution Solved
Reply
N/A
Posts: 0
Accepted Solution

Need Date Format like YYYYMMDD HH24:MI:SS

Hi
in my data set there is one column which repersents datetime .
I want its value should be in following format ..
YYYYMMDD HH24:MISmiley FrustratedS

if this format is not available then please tell me How can i create it ....
i have tried with proc format....

arvind

Accepted Solutions
Solution
‎07-31-2017 08:34 AM
Community Manager
Posts: 2,884

Re: Need Date Format like YYYYMMDD HH24:MI:SS

SAS supports a number of ISO formats for datetimes that are common to transaction logs, web logs, XML, and JSON, and other interchanges.  

 

Here's an example from the documentation (linked above):

 

data _null_;
input dtB :b8601dt15. dtE :e8601dt19.;
put dtB=b8601dt. dtE=e8601dt.;
datalines;
20120402T124022 2012-04-02T12:30:22
;
run;

Output:

 

dtB=20120402T124022 dtE=2012-04-02T12:30:22

Usually one of those formats will suit, as you're trying to conform to another standard.  

 

However, you discovered that you can create your own custom datatime format like this:

proc format;
picture BI_DTIME_FMT
other = '%Y%0m%0d %0H:%0M:%0S' (datatype=datetime);
run;

data _null_;
/*sampdtime=1551022320;*/
sampdtime='23FEB2009:15:32:00'dt;
put sampdtime= BI_DTIME_FMT.;
run;






 

To make that format "permanent", place it in a libname that is found in your OPTIONS FMTSEARCH path.

View solution in original post


All Replies
Super Contributor
Super Contributor
Posts: 3,174

Re: Need Date Format like YYYYMMDD HH24:MI:SS

Have a look at the SAS Procedures Guide, and the PROC FORMAT chapter -- the discussion on using datatype=datetime and defining the "directives" for the particular format you require. You may also find other technical paper and SAS-hosted documentation references on the SAS support website http://support.sas.com/ regarding this topic.

Scott Barry
SBBWorks, Inc.

SAS PROC FORMAT discussion:
http://support.sas.com/onlinedoc/913/getDoc/en/proc.hlp/a000063536.htm
Contributor jf
Contributor
Posts: 22

Re: Need Date Format like YYYYMMDD HH24:MI:SS

If the character variable is OK for you, you can try the following code.

data dateformat;
format date datetime20. day yymmdd10. time time8.;
date = '30apr2007:13:23:45'dt;
day = datepart(date);
time = timepart(date);
day_char = put(day, yymmdd10.);
YYYYMMDD = put(substr(day_char, 1, 4)||substr(day_char, 6, 2)||substr(day_char, 9, 2), $8.);
HHMMSS = put(time, time8.);
YMDHMS = put(YYYYMMDD||' '||HHMMSS, $17.);
format YYYYMMDD $8. HHMMSS $8. YMDHMS $17.;
run;
N/A
Posts: 0

Re: Need Date Format like YYYYMMDD HH24:MI:SS

Thanks jf

in my SAS data set there is a column like
colA numeric(8) .
it contain date.
i want its format like YYYYMMDD HH24:MISmiley FrustratedS;

the code which you sent ...i have used it ..using this we can create a variable "ymdhms" which contain date as YYYYMMDD HH24:MISmiley FrustratedS.

but i have to use it in proc format...

i have tried the following code to create my own format...

proc format library = mylib;
value ymdhms OTHER = [time.];
quit;

data _null_;
x=datetime();
format x ymdhms.;
put x=;
run;

after running this code the output was :
15:32:25

it was diplaying only time...because in [] brakets i have time. format.....

i need your help to explore your concept and implement it in my requirment.

once again thanks..waiting for reply....
Super Contributor
Super Contributor
Posts: 3,174

Re: Need Date Format like YYYYMMDD HH24:MI:SS

Did you even consider the DOC link and using the PROC FORMAT documentation to investigate using the "datatype=" keyword on the PICTURE statement? The different techniques for coding a desired datetime format are explained in detail in the DOC reference provided.

Scott Barry
SBBWorks, Inc.
Contributor jf
Contributor
Posts: 22

Re: Need Date Format like YYYYMMDD HH24:MI:SS

Since your data is like that one column contains different types of data ( I assume that column contains regular numerical data and date type data), so you may need to create a new column (or rename it back to original name if nasassery) using "if... then". You don't need to use "proc format" (I barely use it).

e.g.
data d1;
a = 123;
date = '30apr2007:13:23:45'dt;
format date datetime20.;
run;
data d2;
set d1;
if VFORMAT(date) = 'DATETIME20.' then do;
format date datetime20. day yymmdd10. time time8. ;
day = datepart(date);
time = timepart(date);
day_char = put(day, yymmdd10.);
YYYYMMDD = put(substr(day_char, 1, 4)||substr(day_char, 6, 2)||substr(day_char, 9, 2), $8.);
HHMMSS = put(time, time8.);
YMDHMS = put(YYYYMMDD||' '||HHMMSS, $17.);
format YYYYMMDD $8. HHMMSS $8. YMDHMS $17.;
output;
drop day time day_char YYYYMMDD HHMMSS;
end;
run;
proc print data = d2; run;
N/A
Posts: 0

Re: Need Date Format like YYYYMMDD HH24:MI:SS

i have created My own datetime format. using the following code in BASE SAS.

proc format;
picture BI_DTIME_FMT
other = '%Y%0m%0d %0H:%0M:%0S' (datatype=datetime);
run;

data _null_;
/*sampdtime=1551022320;*/
sampdtime='23FEB2009:15:32:00'dt;
put sampdtime= BI_DTIME_FMT.;
run;

After creating this code ..What are the next step to make it permanently....

i have one SAS DATA SET , i want to change the format of its column like START_DATE .

so that it store the value according to created format....


i tried it in "work" library but its not working ..can u give me right solution...
SAS Super FREQ
Posts: 8,814

Re: Need Date Format like YYYYMMDD HH24:MI:SS

Hi:
The documentation has a long section on storing and accessing formats in permanent format libraries, including the use of the FMTSEARCH option to establish a search path in different SAS libraries for format catalogs.
http://support.sas.com/documentation/cdl/en/proc/59565/HTML/default/a000146279.htm

In addition, the documenation has examples for PROC FORMAT that includes one whose title is, "Example 7: Retrieving a Permanent Format". And, of course, since you have to create the permanent format before you retrieve it, this example also shows how to create a permanent format.

cynthia
N/A
Posts: 0

Re: Need Date Format like YYYYMMDD HH24:MI:SS

Once made it as permanent, can i use it to change the format of column of any SAS data set already created in SAS DI studio....?
SAS Super FREQ
Posts: 8,814

Re: Need Date Format like YYYYMMDD HH24:MI:SS

Hi:
Generally, as long as the variable values fall into the ranges defined by your format, and assuming the format library is available to your servers, then yes, you can assign that format to any variable in a SAS dataset.

However, in the context of the SAS Enterprise Intelligence Platform, which is the context in which SAS DI Studio exists, there is a special location under your application server context directory that is a pre-defined location for SAS formats. If you put your format catalog in that location, then you should not need a FMTSEARCH option in your code.

The Platform Administrator's guide and DI Studio documentation should outline the process for storing and accessing user-defined formats on the platform. There are some previous forum postings on this subject which may also be of use to you:
http://support.sas.com/forums/thread.jspa?messageID=11981⻍
http://support.sas.com/forums/thread.jspa?messageID=3658๊
http://support.sas.com/forums/thread.jspa?messageID=8216‘

cynthia
New Contributor
Posts: 4

Re: Need Date Format like YYYYMMDD HH24:MI:SS

Hi,

Some good suggestions above. For cahnging the underlying value to a specific format, I normally use the nldate, nltime or nldatm functions. For the format I would use the suggestion as above.

proc format;

picture ymdH24MS

other = '%Y-%0m-%0d %0H:%0M:%0S' (datatype=datetime);

run;

data tmp;

  x=datetime();

  format x ymdH24MS.;

  y=nldatm(x, '%Y-%m-%d %H:%M:%S');

run;

Solution
‎07-31-2017 08:34 AM
Community Manager
Posts: 2,884

Re: Need Date Format like YYYYMMDD HH24:MI:SS

SAS supports a number of ISO formats for datetimes that are common to transaction logs, web logs, XML, and JSON, and other interchanges.  

 

Here's an example from the documentation (linked above):

 

data _null_;
input dtB :b8601dt15. dtE :e8601dt19.;
put dtB=b8601dt. dtE=e8601dt.;
datalines;
20120402T124022 2012-04-02T12:30:22
;
run;

Output:

 

dtB=20120402T124022 dtE=2012-04-02T12:30:22

Usually one of those formats will suit, as you're trying to conform to another standard.  

 

However, you discovered that you can create your own custom datatime format like this:

proc format;
picture BI_DTIME_FMT
other = '%Y%0m%0d %0H:%0M:%0S' (datatype=datetime);
run;

data _null_;
/*sampdtime=1551022320;*/
sampdtime='23FEB2009:15:32:00'dt;
put sampdtime= BI_DTIME_FMT.;
run;






 

To make that format "permanent", place it in a libname that is found in your OPTIONS FMTSEARCH path.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 11 replies
  • 13553 views
  • 1 like
  • 6 in conversation