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!
Use the TRANWRD() function.
Y=tranwrd(x,'Miles','');
Thanks! It works!
Please post the log of your code using the {i} icon window.
You can get same result by miles_new = compress(miles,,'kd');
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!
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.
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;
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!
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.