Help using Base SAS procedures

Date & Time Conversion

Reply
Contributor
Posts: 65

Date & Time Conversion

Hi All,

I have got a text file which has date & timing which relates to the process starting time and time it took for it to complete. Now I want to maintain all this timing in a table. But the problem that I am facing is with the date and time in a number format. For ex:
1. Process_Dt has a value of 010911 which can be intrepreted as a Jan 9th, 2011. I want to convert this 010911 into 01/09/11 or 01/09/2011 format. I tried something like this:
date = PROCESS_DT;
PUT date = mmddyys10.;
but it didn't work. Can someone provide me some guidance on how can I achieve this?

2. Process_Time: So similar to date the timing in the txt file is like 1720 or 0820 which can be read as 17:20:00 PM or 08:20:00 AM. I am not able to figure how to convert this into time. I tried creating a providing a format of Time9. when I imported the file but all in vain.

This is what I tried for time:
FORMAT PROCESS_TIME TIME9.

It gave me some value like 0:28:40 which obviously doesn't make sense. Can you please provide share your knowledge on how can I convert the time ?

Thanks for your support.
Regular Contributor
Posts: 165

Re: Date & Time Conversion

It looks like PROCESS_DT and PROCESS_TIME are character variables (given the leading zeros). Do you want the result to be a SAS date/time variable or a character variable?
Contributor
Posts: 65

Re: Date & Time Conversion

Hey Rick:

No they don't have a character variable. I gave the example which I think created the confusion. They are in numeric format.

I want it to store Process_Dt in Date format and Process_Time in Time format in SAS.
Regular Contributor
Posts: 165

Re: Date & Time Conversion

Would Process_Dt still have leading zeros for day of the month? For example Jan. 9th 2011 would be 10911 or would it be 1911?
Contributor
Posts: 65

Re: Date & Time Conversion

Well, that's a good question Rick.

I have both sort of values in my txt file. ex: 010911 and 100110

The date in the txt file are 6 digits in length.
Regular Contributor
Posts: 165

Re: Date & Time Conversion

You could do something like month=floor(PROCESS_DT/10000), day=floor(PROCESS_DT/100)-month*100, etc. and then use date=MDY(month,day,year) to get a SAS date. There is a similar function for time, HMS(Hours, Minutes, Seconds).

Good luck!
Super Contributor
Super Contributor
Posts: 3,174

Re: Date & Time Conversion

Using a DATA step technique, take advantage of SAS INPUT function and use the appropriate INFORMAT to convert your numeric data-string (or otherwise a character data-string) into a SAS NUMERIC variable (DATE type) with the desired FORMAT applied for reporting.

If you truly have a SAS numeric variable with mmddyy, then you will need to also use a PUT within the INPUT, such as:

DATA _NULL_;
FORMAT MYDATE YYMMDDS10.;
MYDATE = INPUT(PUT(010111,Z6.),ANYDTDTE.);
PUT MYDATE= ;
RUN;

And, for numeric TIME data-string conversion, do something similar again with INPUT and PUT, using the appropriate INFORMAT.

Suggest you start by reviewing SAS support website technical documentatoin and supplemental reference materials on this topic -- see search argument below.

Scott Barry
SBBWorks, Inc.

Suggested Google advanced search argument, this topic / post:

sas dates introduction site:sas.com
Contributor
Posts: 65

Re: Date & Time Conversion

Hi Sbb,

Thanks for your reply. It was really helpful. For the Process_Time I did something like this:

DATA _NULL_;
FORMAT MYTIME TIME9.;
YY = PUT(00,Z2.);
MYTIME = INPUT(PUT(0820,Z4.)||YY, ND8601TM10.);
PUT MYTIME=;
PUT _ALL_;
RUN;

The reason why I am concatenating 00 is b'coz i just have "1820" or "0820" in my text file.

Do let me know if you have better way of doing it.

Thanks again.

@ Everyone: Thanks for checking out and helping me. Really appreciate your time.
Super User
Posts: 9,681

Re: Date & Time Conversion

I am afraid you need to make a delimiter between hour and minute to let sas know it is time.
[pre]
data temp;
input date : mmddyy6. char_time : $6.;
time=input(catx(':',substr(char_time,1,2),substr(char_time,3,2)),time.) ;
format date mmddyy10. time timeampm.;
drop char_time;
cards;
010911 1720
020310 0820
;
run;
[/pre]

Ksharp
Ask a Question
Discussion stats
  • 8 replies
  • 220 views
  • 0 likes
  • 4 in conversation