- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Use the TRANWRD() function.
Y=tranwrd(x,'Miles','');
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks! It works!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Please post the log of your code using the {i} icon window.
You can get same result by miles_new = compress(miles,,'kd');
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;