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;
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!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: