The SAS Output Delivery System and reporting techniques

datetime format issue

Reply
Frequent Contributor
Posts: 131

datetime format issue

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

Super User
Posts: 1,235

Re: datetime format issue

Try next code:

data _NULL_;
   timestamp = '17JAN2017:18:36:00'dt;
   format timestamp datetime26.;
   put timestamp=;
run;
Super User
Posts: 789

Re: datetime format issue

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;

 

Respected Advisor
Posts: 4,608

Re: datetime format issue

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
Ask a Question
Discussion stats
  • 3 replies
  • 121 views
  • 0 likes
  • 4 in conversation