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
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;
Hello
Simple strip of the " and convert it to number. Use substr function.
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 ;
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)/") ;
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;
Hi @FreelanceReinh ,
This code is very helpfull for me. Thank you for giving me this solution.
Regards,
Nirav Chaudhari
@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" ?
@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.);
Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.
Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.
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.
Ready to level-up your skills? Choose your own adventure.