BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
SASuserlot
Barite | Level 11

I have the variable "Time" ( character text string) of minutes; I want to convert them into seconds ( numeric format). Unfortunately, I can not change the source data, and this is how it is written in the source.

I appreciate your help.

data have;
time = "08:00";
output;
time ="08:45";
output;
time = "27:38";
output;
time ="59:59";
output;
run;

Want

SASuserlot_0-1705126384305.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
  time = "08:00";
  output;
  time ="08:45";
  output;
  time = "27:38";
  output;
  time ="59:59";
  output;
run;

data want;
  set have;
  time_dt=input(time,stimer.);
run;
proc print;run;

Ksharp_0-1705221089110.png

 

View solution in original post

11 REPLIES 11
SASuserlot
Barite | Level 11

Sorry that’s a typo.

Patrick
Opal | Level 21

Shape your string into a form that's suitable for an informat.

data have;
  time = "08:00";
  output;
  time ="08:45";
  output;
  time = "27:38";
  output;
  time ="59:59";
  output;
run;

data want;
  set have;
  time_dt=input(cats('00:',time),time.);
run;

proc sql;
  select 
    time
    ,time_dt format=best32.
    ,time_dt format=time.
  from want
  ;
quit;

Patrick_0-1705146636471.png

 

Tom
Super User Tom
Super User

So you have strings in XX:XX style and you want to interpret that as MM:SS.

 

First let's make a dataset with some example data. Not need to write a lot of statements. Just use normal SAS code to read in the example values.

data have;
  input time :$5.;
cards;
08:00
08:45
27:38
59:59
;

The TIME informat will assume a string in XX:XX style is HH:MM , not MM:SS.

So you have two choices.  Prefix an extra 00: in front so the TIME informat knows you have strings in HH:MM:SS style.

Or just divide by 60 since there are 60 seconds in a minute.

 

Let's try both.

data want;
  set have;
  sastime=input(time,time5.);
  format sastime time12.;
  seconds1=sastime/60;
  seconds2=input(cats('00:',time),time8.);
run;

Result:

Obs    time          sastime    seconds1    seconds2

 1     08:00         8:00:00       480         480
 2     08:45         8:45:00       525         525
 3     27:38        27:38:00      1658        1658
 4     59:59        59:59:00      3599        3599

 

Ksharp
Super User
data have;
  time = "08:00";
  output;
  time ="08:45";
  output;
  time = "27:38";
  output;
  time ="59:59";
  output;
run;

data want;
  set have;
  time_dt=input(time,stimer.);
run;
proc print;run;

Ksharp_0-1705221089110.png

 

SASuserlot
Barite | Level 11

Thank you @Ksharp @Kurt_Bremser @Tom @Patrick . All of you are amazing. All your suggestions work.  I am choosing one solution to close this thread. Thanks again guys.

SASuserlot
Barite | Level 11

No. All the time is less than one hour. So no possibility of getting the hours information in the data.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 11 replies
  • 1275 views
  • 9 likes
  • 5 in conversation