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

Dear Community, 

I would like to learn how to calculate the mean from string variables some of them contain comments and range.

For example, the data I have is as 

ID hours
1 7-8 hours after play
2 0-1
3 10-12
4 11

And the data wanted is 

ID hours hours_m
1 7-8 hours after play 7.5
2 0-1 0.5
3 10-12 11
4 11 11

 

Thank you so much.

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Use the COMPRESS function to get rid of anything but digits and hyphens, then count the "words", then calculate or just input:

data have;
infile datalines dlm="09"x dsd truncover;
input ID $ hours :$30.;
datalines;
1	7-8 hours after play
2	0-1
3	10-12
4	11
;

data want;
set have;
string = compress(hours,"-","kd");
if countw(string,"-") = 2
then hours_m = (input(scan(string,2,"-"),best.) + input(scan(string,1,"-"),best.)) / 2;
else hours_m = input(string,best.);
drop string;
run;

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User

Use the COMPRESS function to get rid of anything but digits and hyphens, then count the "words", then calculate or just input:

data have;
infile datalines dlm="09"x dsd truncover;
input ID $ hours :$30.;
datalines;
1	7-8 hours after play
2	0-1
3	10-12
4	11
;

data want;
set have;
string = compress(hours,"-","kd");
if countw(string,"-") = 2
then hours_m = (input(scan(string,2,"-"),best.) + input(scan(string,1,"-"),best.)) / 2;
else hours_m = input(string,best.);
drop string;
run;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1055 views
  • 1 like
  • 2 in conversation