BookmarkSubscribeRSS Feed
Q1983
Lapis Lazuli | Level 10

data test(keep=mydate);

set test2;

run;

sample data

mydate

17JAN2017:18:36:00.000000

 

I wish to display mydate as

01/17/2017 :18:36:00.000000

 

I tried using mmddyy10 hh:mm:ss and get syntax error.  By default it displays in date9 plus the hr min sec interval.  Just want to display as mmddyy format as above in bold

3 REPLIES 3
Shmuel
Garnet | Level 18

Try next code:

data _NULL_;
   timestamp = '17JAN2017:18:36:00'dt;
   format timestamp datetime26.;
   put timestamp=;
run;
mkeintz
PROC Star

I don't think that's what the op wants.  In particular, it seems they want this format:

 

    mm/dd/yyyy :hh:mm:ss.ffffff

 

You can get close to this by making a custom format via the PICTURE statement in proc format.  It has the "datatype=datetime" option, which tells it to obey datetime-related directives (the % signed elements below):

 

proc format;

  picture mydtfmt low-high='%Y%0m%0d %0H:%0M:%0S' (datatype=datetime) ;

run;

data _null_;

  dt="17jan2017:18:36:00.300000"dt;

  put dt=mydtfmt26.6;

run;

 

Unfortunately, this generates a value with no decimals (because there are no datetime directives for fractions of a second):

 

  dt=20170117 18:36:00

 

 

So you have to make a text variable using the mydtfmt format above concatenated with the fractional component, as in:

 

proc format;
  picture mydtfmt 
    low-high='%Y%0m%0d %0H:%0M:%0S' (datatype=datetime)   ;
run;
     
data _null_;
  dt="17jan2017:18:36:00.300000"dt;

  fraction=substr(cats(mod(dt,1),'000000'),2,7) ;
  date_text=put(dt,mydtfmt20.)||fraction;
  put date_text=;

  /** or more compactly (without intermediate variable "fraction" .... **/
  date_text2=put(dt,mydtfmt20.)||substr(cats(mod(dt,1),'000000'),2,7);
  put date_text2=;

run;

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
PGStats
Opal | Level 21

You can do this (sort of) with a picture format:

 

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

data _NULL_;
   timestamp = '17JAN2017:18:36:05'dt;
   put timestamp =  myDt26.;
run;
PG

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1506 views
  • 0 likes
  • 4 in conversation