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

Hi, thank you for any help. I need to convert character day and hour into hours

 

data one;
input day $1-13;
cards;
04days,5 hours

15days, 06 Hours
;
run;

 

what I want result is 

 

101 hours

366 hours

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

First you get rid of the alphabetic characters and spaces, leaving just the numbers and a comma, via the COMPRESS command. The first number is the days, you convert it to numeric via the INPUT command. The second number is the hours, you convert it to numeric via the INPUT command. Lastly, you compute total hours, which I leave to you as a homework assignment.

 

data one;
input day $1-16;
cards;
04days,5 hours
15days, 06 Hours
;

data want;
    set one;
    day=compress(day,' ','a');
    hours=input(scan(day,2,','),2.);
    days=input(scan(day,1,','),2.);
run;

 

--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

First you get rid of the alphabetic characters and spaces, leaving just the numbers and a comma, via the COMPRESS command. The first number is the days, you convert it to numeric via the INPUT command. The second number is the hours, you convert it to numeric via the INPUT command. Lastly, you compute total hours, which I leave to you as a homework assignment.

 

data one;
input day $1-16;
cards;
04days,5 hours
15days, 06 Hours
;

data want;
    set one;
    day=compress(day,' ','a');
    hours=input(scan(day,2,','),2.);
    days=input(scan(day,1,','),2.);
run;

 

--
Paige Miller
Seadrago
Obsidian | Level 7

 

data ds1;
  set one;

  /*seperate days and hours into 2 variables and convert to numeric*/

  days=input(scan(day, 1, "days,"), best.);

  hours=input(scan(scan(day, 2, "days,"), 1, " "), best.);

 

/*derive total hours using numeric variables*/

  tothours=(days*24)+hours;

run;

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 2 replies
  • 177 views
  • 2 likes
  • 3 in conversation