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;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of 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
  • 6 replies
  • 916 views
  • 0 likes
  • 5 in conversation