Hi,
I have written the following code (just a simple test case of a much bigger program). I have a dataset that has a variable of type character string but in fact its values are timestamp. I need to add another variable as some temporary variable for some further calculations in this data step with the real SAS timestamp data type so that I can use SAS corresponding functions for timestamp/date type (extract year, hour, min, etc.)
Here is the code:
data test; input timestamp_str :$17.; datalines; 18-01-12T14:22:31 19-05-17T20:34:10 19-07-04T14:24:43 run; data test; set test; timestamp_value = input(timestamp_str, ANYDTDTM17.); run;
Years in the dataset test include only two digits so for example the 2nd observation which is 19-05-17T20:34:10 is to be interpreted in the following way:
year = 2019 month = 05 (May) day = 17 hour = 20 minute = 34 seconde = 10
The problem is once the program has been run the new variable timestamp_value is empty. I don't get any error message but I suspect that this should be related to the format I've specified: ANYDTDTM17. If that is the case could someone kindly guide me about the correct format?
thanks in advance
I am, of course, still baffled that there are still people who use 2-digit years after all the hassle we had with the Y2K scare.
I have words for such sloppiness that I don't want to use here, otherwise a moderator would have to delete my post.
Still, both my method and the one suggested by @andreas_lds work:
data have;
input timestamp_str :$17.;
datalines;
18-01-12T14:22:31
19-05-17T20:34:10
19-07-04T14:24:43
;
data want;
set have;
format timestamp_value1 timestamp_value2 e8601dt19.;
timestamp_value1 = input('20' !! timestamp_str,e8601dt19.);
timestamp_value2 = input(timestamp_str,ymddttm17.);
run;
proc print data=want noobs;
run;
Result:
timestamp_str timestamp_value1 timestamp_value2 18-01-12T14:22:31 2018-01-12T14:22:31 2018-01-12T14:22:31 19-05-17T20:34:10 2019-05-17T20:34:10 2019-05-17T20:34:10 19-07-04T14:24:43 2019-07-04T14:24:43 2019-07-04T14:24:43
Try this
data test;
input timestamp_str :$17.;
datalines;
18-01-12T14:22:31
19-05-17T20:34:10
19-07-04T14:24:43
run;
data test1;
set test;
timestamp_value = input(tranwrd(timestamp_str, 'T', ' '), ANYDTDTM17.);
format timestamp_value datetime20.;
run;
Or try ymddttm18. as informat.
Prepend “20“ and use the e8601dt19. informat.
@Kurt_Bremser wrote:
Prepend “20“ and use the e8601dt19. informat.
Isn't informat e8601dt19. for a pattern of YYYY-MM-DDThh:mm:ss.nnnnnn but the pattern here is DD-MM-YYThh:mm:ss.nnnnnn
Ignore above.
@Patrick, I think the pattern is YY-MM-DDThh:mm:ss.nnnnnn
I am, of course, still baffled that there are still people who use 2-digit years after all the hassle we had with the Y2K scare.
I have words for such sloppiness that I don't want to use here, otherwise a moderator would have to delete my post.
Still, both my method and the one suggested by @andreas_lds work:
data have;
input timestamp_str :$17.;
datalines;
18-01-12T14:22:31
19-05-17T20:34:10
19-07-04T14:24:43
;
data want;
set have;
format timestamp_value1 timestamp_value2 e8601dt19.;
timestamp_value1 = input('20' !! timestamp_str,e8601dt19.);
timestamp_value2 = input(timestamp_str,ymddttm17.);
run;
proc print data=want noobs;
run;
Result:
timestamp_str timestamp_value1 timestamp_value2 18-01-12T14:22:31 2018-01-12T14:22:31 2018-01-12T14:22:31 19-05-17T20:34:10 2019-05-17T20:34:10 2019-05-17T20:34:10 19-07-04T14:24:43 2019-07-04T14:24:43 2019-07-04T14:24:43
Note that FORMATs always convert values into text. I think you are looking for an INFORMAT instead.
Hello everyone,
Sorry for the delay, I would like to thank everyone for your time and your attention to my problem and the comments and solutions you provided. I really appreciate your kind help.
Regards,
I strongly agree with the idea that using two digits for indicating the year value is not clear and appropriate. The problem is that the data I'm talking about is part of a set of an API log files that I don't produce but I can only read them to extract some data for doing statistics. Unfortunately I don't have neither access to the source code nor the power to impose this best practice rule. Thanks a lot for your help and your comment. Indeed by concatenation you suggested, standard formats based on SAS documentation can be used: http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a003169814.htm Thanks a lot.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.