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

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!

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.

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
  • 3 replies
  • 945 views
  • 0 likes
  • 4 in conversation