0

I am trying to convert datatime20. from numeric to character value. Currently I have numeric values like this: 01Jan200:00:00:00 and I need to convert it to character values and received output like: 2020-01-01 00:00:00.0

What format and informat should be used in aboved ? I have tried used PUT function to convert numeric to character and tried many option, each time receiving other format. Should be also use DHMS function before PUT ?

1
  • Please show us what you've tried. Commented Oct 21, 2020 at 19:33

2 Answers 2

0

There is not a native format that produces that string exactly. But it it not hard to build it in steps using existing formats. Or you could use PICTURE statement in PROC FORMAT to build your own format.

If you don't really care about the time of day part of the datetime value then this is an easy and clearly understand way to convert the numeric variable DT with number of seconds into a new character variable in that style. Use DATEPART() to get the date (number of days) from the datetime value and then use the YYMMDD format to generate the 10 character string for the date and then just append the constant string of the formatted zeros.

length dt_string $21.;
dt_string = put(datepart(dt),yymmdd10.)||' 00:00:00.0';

If you need the time of day part then you could also use the TOD format.

dt_string = put(datepart(dt),yymmdd10.)||put(dt,tod11.1);

Or you could use the format E8601DT21.1 and then change the letter T between the date and time to a space instead.

dt_string = translate(put(dt,E8601DT21.1),' ','T');

If you want to figure out what formats exist for datetime values and what the formatted results look like you could run a little program to pull the formats from the meta data and apply them to a specific datetime value.

data datetime_formats;
   length format $50 string $80 ;
   set sashelp.vformat;
   where fmttype='F';
   where also fmtinfo(fmtname,'cat')='datetime';
   keep format string fmtname maxw minw maxd ;
   format=cats(fmtname,maxw,'.','-L');
   string=putn('01Jan2020:01:02:03'dt,format);
run;
Sign up to request clarification or add additional context in comments.

Comments

0

A custom format can be defined to return the result of a user defined function. Docs

proc format;
  value <format-name> (default=<width>)
    other = [<function-name>()]
  ;
run;

Example:

options cmplib=(sasuser.functions);

proc fcmp outlib=sasuser.functions.temporal;

  function E8601DTS (datetime) $21;
    return (
      translate (putn(datetime,'E8601DT21.1'),' ','T')
    );
  endsub;

run;

proc format;
  value E8601DTS (default=21)
    other = [E8601DTS()]
  ;
run;

data have;
  do dt = '01jan2020:0:0'dt to '10jan2020:0:0'dt by '60:00't;
    output;
  end;
  format dt datetime16.;
run;

ods html file='function-based-format.html';

proc print data=have(obs=4); title 'stock E8601DT';
proc print data=have(obs=4); title 'custom E8601DTS';
  format dt E8601DTS.;
run;
  
ods html close;

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.