Help using Base SAS procedures

Time Formats from Text

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

Time Formats from Text

Hi there

 

I am trying to convert this text so I can use the time element in SAS.?

 

12:30PM:Tuesday:Weekday

7:30AM:Tuesday:Weekday

6:60AMSmiley Frustratedaturday:Weekend

 

 

 

currently stored as CHAR $25

 

I have managed to extract the time by doing the following but it is still text so cant use to sort by time 

 

time= Substr(text_,1,index(text_,"M:"));

 

 

How can I then convert to a SAS format

 

thanks

 

 


Accepted Solutions
Solution
‎07-14-2016 10:44 AM
Super User
Posts: 17,840

Re: Time Formats from Text

Apply a format.

 

format time_sas time8.;

View solution in original post


All Replies
Super Contributor
Posts: 268

Re: Time Formats from Text

Your data needs a day, month and a year.

Occasional Contributor
Posts: 12

Re: Time Formats from Text

The date sits in a separate text field 

 

September 1, 2015

Super Contributor
Posts: 268

Re: Time Formats from Text

Are you just wanting this?
12:30PM
7:30AM
.
.
Occasional Contributor
Posts: 12

Re: Time Formats from Text

Hi yes and I have managed to get that but it is in a char format so when I sort by it, it's not sorting correctly... ie I need 7:30am before 12.30pm etc

Super Contributor
Posts: 268

Re: Time Formats from Text

not elegant but it works.

 

data one;
informat time $25. ;
input time ;
cards;
12:30PM:Tuesday:Weekday
7:30AM:Tuesday:Weekday
6:60AMSmiley Frustratedaturday:Weekend
;
run;
data want;
set one;
time2=scan(time,1,'M')||'M';
run;
proc print;
run;

Occasional Contributor
Posts: 12

Re: Time Formats from Text

Excellent thanks this works great Smiley LOL

Super User
Posts: 17,840

Re: Time Formats from Text

In general, the idea is the same as your earlier date question. Use input() with appropriate informat. 

Assuming you've isolated the time portiom correctly it will be something like:

 

Time_sas = input(time, time.);

Occasional Contributor
Posts: 12

Re: Time Formats from Text

Thanks @Reeza that works and allows me to sort but is there a way of showing it in a format that is easy to understand ie 10:00AM and not 36000

Solution
‎07-14-2016 10:44 AM
Super User
Posts: 17,840

Re: Time Formats from Text

Apply a format.

 

format time_sas time8.;

Occasional Contributor
Posts: 12

Re: Time Formats from Text

Excellent thank you!

 

 

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 419 views
  • 3 likes
  • 3 in conversation