BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Hi
in my data set there is one column which repersents datetime .
I want its value should be in following format ..
YYYYMMDD HH24:MI:SS

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

arvind
1 ACCEPTED SOLUTION

Accepted Solutions
ChrisHemedinger
Community Manager

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.

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.

View solution in original post

11 REPLIES 11
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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
jf
Fluorite | Level 6 jf
Fluorite | Level 6
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;
deleted_user
Not applicable
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:MI:SS;

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

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....
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
jf
Fluorite | Level 6 jf
Fluorite | Level 6
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;
deleted_user
Not applicable
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...
Cynthia_sas
SAS Super FREQ
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
deleted_user
Not applicable
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....?
Cynthia_sas
SAS Super FREQ
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
Koos
Calcite | Level 5

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;

ChrisHemedinger
Community Manager

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.

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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