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

I have a variable has values like 0,100 miles, 200 miles,3000 miles. I want to remove the miles part.

I used substr function.

Miles_new=substr(miles,1,length(miles)-5)

However, it gave me the error Character has been converted to numeric values and Invalid numeric data.

 

How can I fix that?

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Use the TRANWRD() function.

 

Y=tranwrd(x,'Miles','');

 
--
Paige Miller

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

Use the TRANWRD() function.

 

Y=tranwrd(x,'Miles','');

 
--
Paige Miller
bayoote
Calcite | Level 5

Thanks! It works!

Shmuel
Garnet | Level 18

Please post the log of your code using the {i} icon window.

 

You can get same result by miles_new = compress(miles,,'kd');

Reeza
Super User

Use COMPRESS() to remove all alphabetic characters from the string and keep only the numbers. 

 


@bayoote wrote:

I have a variable has values like 0,100 miles, 200 miles,3000 miles. I want to remove the miles part.

I used substr function.

Miles_new=substr(miles,1,length(miles)-5)

However, it gave me the error Character has been converted to numeric values and Invalid numeric data.

 

How can I fix that?

Thanks!


 

PaigeMiller
Diamond | Level 26

The  difference between TRANWRD and COMPRESS is that COMPRESS will remove all characters specified, rather than just the word "Miles", as in the original request. For this problem, maybe both work, but in general you can't interchange COMPRESS and TRANWRD.

 
--
Paige Miller
Patrick
Opal | Level 21

Below code will convert your source string into a numerical variable and though allow you to use it for calculations.

data have;
  infile datalines truncover;
  input miles $20.;
  datalines;
0,100 miles
200 miles
000 miles
;

data want;
  set have;
  miles_new=input(scan(miles,1,' '),commax16.);
run;

proc print data=want;
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 6 replies
  • 698 views
  • 0 likes
  • 5 in conversation