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

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
FreelanceReinh
Jade | Level 19

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;

View solution in original post

7 REPLIES 7
Sajid01
Meteorite | Level 14

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

AMSAS
SAS Super FREQ

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 ;

AMSAS
SAS Super FREQ

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)/") ;
FreelanceReinh
Jade | Level 19

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;
NiravC
Obsidian | Level 7

Hi @FreelanceReinh ,

 

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

 

Regards,

Nirav Chaudhari

ballardw
Super User

@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" ?

FreelanceReinh
Jade | Level 19

@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.);

 

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
  • 7 replies
  • 645 views
  • 4 likes
  • 5 in conversation