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

## How to convert inches into numeric ?

Hi All,

I have one column.

There is some value like 1",2",1/2",1 1/2".......

Can anyone explain how to convert this value in numeric ?

Thank you,

Nirav

1 ACCEPTED SOLUTION

Accepted Solutions
Jade | Level 19

## Re: How to convert inches into numeric ?

Hi @NiravC,

@NiravC wrote:

There is some value like 1",2",1/2",1 1/2".......

Can anyone explain how to convert this value in numeric ?

Do you want, e.g., 1.5 for 1 1/2"? If so, try this:

``````data have;
input inches_c \$10.;
cards ;
1"
1/2"
1 1/2"
12 7/8"
123 13/16"
;

data want;
set have;
inches=input(resolve(cats('%sysevalf(',translate(trim(inches_c),'+',' "'),')')),32.);
run;``````
7 REPLIES 7
Meteorite | Level 14

## Re: How to convert inches into numeric ?

Hello
Simple strip of the " and convert it to number. Use substr function.

SAS Super FREQ

## Re: How to convert inches into numeric ?

Here's a solution using regular expression matching

``````data got ;
input inches \$6. ;
cards ;
1"
2"
1/2"
1 1/2"
2"
3 3/4"
;

data want ;
retain regExpID ;
if _n_=1 then do ;
/* Create regular expression to search for i n/d format e.g. 1, 1/2 or 2 1/2 */
regExpID=prxparse("/(\d \d\/\d)|(\d\/\d)|(\d)/") ;
put regExpID= ;
end ;
set got ;
int=0 ;
numerator=0 ;
denominator=0 ;
/* does the input string inches fit the formats */
position=prxmatch(regExpID, inches);
if position then do ;
paren=prxparen(regExpID);
/* If the matching format is i */
if paren=3 then do ;
int=inputn(scan(inches,1,' /"'),"8.") ;
end ;
/* If the matching format is n/d */
else if paren=2 then do ;
numerator=inputn(scan(inches,1,' /"'),"8.") ;
denominator=inputn(scan(inches,2,' /"'),"8.") ;
end ;
/* If the matching format is i n/d */
else do ;
int=inputn(scan(inches,1,' /"'),"8.") ;
numerator=inputn(scan(inches,2,' /"'),"8.") ;
denominator=inputn(scan(inches,3,' /"'),"8.") ;

end ;
end ;
put int= numerator= denominator= ;
run ;

``````
SAS Super FREQ

## Re: How to convert inches into numeric ?

Slight bug in my prior code as I didn't account for more than a single digit for inches.
Need to add the * metacharacter to the regular expression definition:

``regExpID=prxparse("/(\d* \d\/\d)|(\d\/\d)|(\d)/") ;``
Jade | Level 19

## Re: How to convert inches into numeric ?

Hi @NiravC,

@NiravC wrote:

There is some value like 1",2",1/2",1 1/2".......

Can anyone explain how to convert this value in numeric ?

Do you want, e.g., 1.5 for 1 1/2"? If so, try this:

``````data have;
input inches_c \$10.;
cards ;
1"
1/2"
1 1/2"
12 7/8"
123 13/16"
;

data want;
set have;
inches=input(resolve(cats('%sysevalf(',translate(trim(inches_c),'+',' "'),')')),32.);
run;``````
Obsidian | Level 7

## Re: How to convert inches into numeric ?

This code is very helpfull for me. Thank you for giving me this solution.

Regards,

Nirav Chaudhari

Super User

## Re: How to convert inches into numeric ?

@NiravC wrote:

Hi All,

I have one column.

There is some value like 1",2",1/2",1 1/2".......

Can anyone explain how to convert this value in numeric ?

Thank you,

Nirav

Do any of these "inch" measurements happen to also include feet? Such as 1' 8 1/2" ?

Jade | Level 19

## Re: How to convert inches into numeric ?

@ballardw wrote:
Do any of these "inch" measurements happen to also include feet? Such as 1' 8 1/2" ?

Good point!

A slight extension of the nested-functions approach can deal with this possibility:

``inches=input(resolve(cats('%sysevalf(',tranwrd(translate(trim(inches_c),'+',' "'),"'","*12"),')')),32.);``

Discussion stats
• 7 replies
• 674 views
• 4 likes
• 5 in conversation