Help using Base SAS procedures

Import number as time

Reply
Regular Contributor
Posts: 152

Import number as time

Hi

I am using SAS 9.1 and importing a .txt file using infile and input statements. I am then exporting the file to an MS-Access database.

I've got everything figured out except the time fields. Each time field in the file is in a time increment of a 24 hour clock but without the colon i.e. 2130, 1055, 1544. How do I import so that SAS recognizes it as a time? What format do I then use upon export?

Thanks for any and all assistance.
PROC Star
Posts: 1,561

Re: Import number as time

No std informat i think to read such a time. You have to tweak it a bit.

x='2130';
sastime=input(x,2.)*3600+input(substr(x,3,2),2.)*60;
or
sastime=input( substr(x,1,2)||':'||substr(x,3,2)||':00', time.);

The export format will depend on what Access wants. time. or hhmm. would seem adequate.
Super Contributor
Super Contributor
Posts: 3,174

Re: Import number as time

For more structured support, use the HMS function after reading up the components of the hour and minute portion - example below:

data _null_;
input hh 2. mm 2.;
time = hms(hh,mm,0);
put time= time5. ;
datalines;
2359
run;


Scott Barry
SBBWorks, Inc.
PROC Star
Posts: 1,561

Re: Import number as time

Indeed, Scott's suggestion to let the hms function do the calculation is a bit tidier. You now have:

x='2130';
sastime=input(x,2.)*3600+input(substr(x,3,2),2.)*60;
or
sastime=hms( input(x,2.), input(substr(x,3,2),2.), 0);
or
sastime=input( substr(x,1,2)||':'||substr(x,3,2)||':00', time.);

So many ways with sas! Smiley Happy
Frequent Contributor
Posts: 128

Re: Import number as time

But if I have a :

105 (time: 01:05)

how can I do?
Frequent Contributor
Posts: 128

Re: Import number as time

well, it will be more easier in excel
change it in excel 105-> 0105
Super Contributor
Super Contributor
Posts: 3,174

Re: Import number as time

Forum posting Suggestion: next time create a new post as you being the AUTHOR, and paste a link if needed back to some forum archive reference.

With SAS 9.2, you have the ISO 8601 INFORMAT B8601TM demonstrated below.

Otherwise, you will need to separate the minute- and hour-portion and use the HMS function to derive a SAS numeric TIME variable.

Scott Barry
SBBWorks, Inc.


68 data _null_;
69 c_hmm = '105';
70 n_hmm = input(c_hmm,best.);
71 c_hhmm = put(n_hmm,z6.);
72 n_hmm_sec = input(c_hhmm,b8601tm.);
73 format n_hmm_sec time8.;
74 putlog _all_;
75 run;

c_hmm=105 n_hmm=105 c_hhmm=000105 n_hmm_sec=0:01:05 _ERROR_=0 _N_=1
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

SAS 9.2 Language Reference, Informats by Category
http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a001239776.htm Message was edited by: sbb
Ask a Question
Discussion stats
  • 6 replies
  • 192 views
  • 0 likes
  • 4 in conversation