BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
desireatem
Pyrite | Level 9

I have this date time variable as below. It is shown as numeric ( Num 8, datetime16) rather than Character.  I tried to covert it to numeric but it won't work: DTa=input(DT,datetime16.); Any help??

25DEC20:09:00:00
28SEP20:02:02:00
25Dec2020 7:31:00
 
 
 
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

So your variable is NOT named DT (as you wrote it in your first post, quote:

DTa=input(DT,datetime16.);

), but MEWSO2_DT.

So let's play around with your actual variable:

data test;
set gg1;
date = datepart(MEWSO2_DT);
time = timepart(MEWSO2_DT);
dt_string = put(MEWSO2_DT,e8601dt19.);
new_dt = intnx('dtmonth',MEWSO2_DT,1,"s");
format
  date yymmdd10.
  time time8.
  new_dt e8601dt19.
;
run;

and then look at the newly created variables in dataset TEST.

 

View solution in original post

20 REPLIES 20
Kurt_Bremser
Super User

@desireatem wrote:

I have this date time variable as below. It is shown as numeric ( Num 8, datetime16) rather than Character.  I tried to covert it to numeric but it won't work: DTa=input(DT,datetime16.); Any help??

25DEC20:09:00:00
28SEP20:02:02:00
25Dec2020 7:31:00
 
 
 

With a type of numeric and this format, it is already a SAS datetime value, so there's no need for any conversion.

desireatem
Pyrite | Level 9

If there is no need for conversion, how do I convert it to SAS DateTime to use numerical computations?

desireatem
Pyrite | Level 9

Then how do I convert it to SAS datetime to use for computation?

 

ballardw
Super User

@desireatem wrote:

I have this date time variable as below. It is shown as numeric ( Num 8, datetime16) rather than Character.  I tried to covert it to numeric but it won't work: DTa=input(DT,datetime16.);


If it is shown as numeric why did you think you needed to convert it to numeric???

desireatem
Pyrite | Level 9

I think I have to convert it to numeric because this: 25DEC20:09:00:00 is not numeric despite it state numeric. There are characters in it?

PaigeMiller
Diamond | Level 26

I think I have to convert it to numeric because this: 25DEC20:09:00:00 is not numeric despite it state numeric. There are characters in it?

 

When a format (such as datetime16.) is applied to a numeric value, this changes the appearance of your numeric value and it is possible that it now appears to have non-numerics in the value, but only the appearance is changed. The value remains unchanged, and remains numeric.

 

 

--
Paige Miller
Kurt_Bremser
Super User

@desireatem wrote:

I think I have to convert it to numeric because this: 25DEC20:09:00:00 is not numeric despite it state numeric. There are characters in it?


This is the human-readable display of the datetime value as determined by the format.

ballardw
Super User

@desireatem wrote:

I think I have to convert it to numeric because this: 25DEC20:09:00:00 is not numeric despite it state numeric. There are characters in it?


Format. Format. Controls display. It looks like a datetime because it has a datetime format.

The values don't change just because you have a format. The value type doesn't change because of the format.

Formats are how SAS allows you to create meaningfull-to-human text from a value.

Consider this program. You can copy and run it. The value variable is numeric. The default format is BEST12. But I override the default format to use a custom format in the Proc Tabulate report body.

proc format;
value weirdness
1='Some text'
2='Other text'
3='Off the wall';
run;

data junk;
  input id value;
datalines;
1 1
34 2
;

proc tabulate data=junk;
   class id;
   var value;
   table (id all='Total')*value,
        sum='Sum'*f=weirdness.;
run;

Repeat to yourself until it sinks in: the format is how values are displayed.

Or this one:

Proc freq data=sashelp.class;
   tables name;
   format name $1.;
run;

Which will let me know how many students start with each letter present in the data. I did not change any names. I could assign the format $1. to the variable name in a copy of the Sashelp.Class data set (bad idea to attempt to do such to the original) and you would only see by default 1 letter for each name. But the full names are still in the data set (unless you actually do change them in the copy for some reason, but the format will not do that).

xxformat_com
Barite | Level 11

Hi,

Either remove the format permanently in a data step or using proc datasets, or when you print it.

 

Example : removing the formats of all the variables.

 

proc print data=mydata;
format _all_;
run;
desireatem
Pyrite | Level 9

Nothing seems to work. I want to transfer the datetime to SAS datetime so that I can use for computation, but since it is numeric when it supposed to be character, I am confused.

Kurt_Bremser
Super User

Run this:

proc contents data=<your dataset name>;
run;

data _null_;
set <your dataset name>;
if dt ne .
then do;
  put dt 20.3;
  stop;
end;
run;

and post the output and the complete log here.

desireatem
Pyrite | Level 9
Alphabetic List of Variables and Attributes
# Variable Type Len Format Label
1 MEWSO2_DT Num 8 DATETIME16. MEWSO2_DT


NOTE: PROCEDURE CONTENTS used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds


140
141 data _null_;
142 set gg1;
143 if dt ne .
144 then do;
145 put dt 20.3;
146 stop;
147 end;
148 run;

NOTE: Variable dt is uninitialized.
NOTE: There were 13 observations read from the data set WORK.GG1.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

Kurt_Bremser
Super User

So your variable is NOT named DT (as you wrote it in your first post, quote:

DTa=input(DT,datetime16.);

), but MEWSO2_DT.

So let's play around with your actual variable:

data test;
set gg1;
date = datepart(MEWSO2_DT);
time = timepart(MEWSO2_DT);
dt_string = put(MEWSO2_DT,e8601dt19.);
new_dt = intnx('dtmonth',MEWSO2_DT,1,"s");
format
  date yymmdd10.
  time time8.
  new_dt e8601dt19.
;
run;

and then look at the newly created variables in dataset TEST.

 

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
  • 20 replies
  • 2430 views
  • 0 likes
  • 6 in conversation