DATA Step, Macro, Functions and more

How to read the timestamp in SAS

Reply
N/A
Posts: 0

How to read the timestamp in SAS

Hi ,
iam trying to read the data for time stamp which is in below format
yyyy-mm-dd-hh.mm.ss.hshshs,please tell me which format can i use to read the data ,one more thing (field is numeric).
Respected Advisor
Posts: 4,173

Re: How to read the timestamp in SAS

Posted in reply to deleted_user
You have to use a INformat to read data. Formats are used to write data.
ANYDTDTMw. is a very convenient informat to read datetime values as it digests a number of different datetime STRINGS and converts them to a number representing a SAS datetime.
http://support.sas.com/onlinedoc/913/getDoc/en/lrdict.hlp/a001239776.htm

What do you mean by "field is numeric"? Where do you try to read the data from? Could it be that this is more a data conversion problem (i.e. from ORACLE to SAS)?

If the field you want to read is already in SAS and contains a datetime value then it's only about:
newvar=datetimevar;
...and you might want to use a format to WRITE the values of this new var, i.e"
format newvar datetime21.;
http://support.sas.com/onlinedoc/913/getDoc/en/lrdict.hlp/a001263753.htm

HTH
Patrick

Message was edited by: Patrick
N/A
Posts: 0

Re: How to read the timestamp in SAS

thanx for your reply,
actually i am reading the data from text file to sas dataset ,then i need to convert that into datetime format
In first dataset after file reader field is Character,after converting field is numeric
value i ma getting from file is 2008-09-06-03.30.45.101010
SAS Employee
Posts: 174

Re: How to read the timestamp in SAS

Posted in reply to deleted_user
Perhaps this small sample can give you some hints
[pre]
data test;
datetime_txt = '2008-09-06-03.30.45.101010';

date_num = input(substr(datetime_txt,1,10), yymmdd10.);
time_num = input(substr(datetime_txt,12), time15.);

datetime_sas = date_num * 86400 + time_num;

put date_num date. time_num time. datetime_sas datetime22.;
run;
[/pre]
You have many ways to import and format your datetime, date and time values. Your 'problem' is that you got the year first in your datetime text, where's SAS normally got it last.
If you can find a SAS date function or informat/format then use it, and not the substr I use in the small sample above (its quick and dirty - like my 'dirty' datetime transformation).
Respected Advisor
Posts: 4,173

Re: How to read the timestamp in SAS

Posted in reply to GertNissen
data want;
infile datalines;
input MyDateTimeVar ANYDTDTM.;
put 'As a number: ' MyDateTimeVar= ' Formatted: ' MyDateTimeVar= datetime21.;
datalines;
2008-09-06-03.30.45.101010
;

HTH
Patrick
SAS Employee
Posts: 174

Re: How to read the timestamp in SAS

Hi Patrick

Your right - ANYDTDTM. is a much better solution - I have learned something new, I didt knew that informat - tx.
N/A
Posts: 0

Re: How to read the timestamp in SAS

Thanx patrick
N/A
Posts: 0

Re: How to read the timestamp in SAS

Posted in reply to GertNissen
ThanX GeniZ
i will try to user this now.........
Ask a Question
Discussion stats
  • 7 replies
  • 1948 views
  • 0 likes
  • 3 in conversation