Reputation: 65
how can i convert a string with only numbers to numeric, i have this:
mydataset:
StringNumber
25
270
the length is $3
when i use this code, removing blanks.
data eg1;
set myDataSet;
number=input(strip(StrinNumber),8.);
run;
in the result the first obs is missing, and the second is 270 (ok).
i tried:
number=input(combpl(StrinNumber),8.);
number=input(compress(StrinNumber),8.);
and the result is same
Upvotes: 0
Views: 3595
Reputation: 63424
There's no particular reason your code shouldn't have worked, but in any event this is simpler:
data test;
length stringNumber $3;
input StringNumber $;
datalines;
25
270
;;;;
run;
data want;
set test;
number = input(stringNumber,3.);
put number=;
run;
The 3. is the length of the string, and you don't need to strip blanks off.
If this doesn't work, I would guess your 25 is not truly the number 25 plus a space, but instead has some non-space character (including NBSP, 'A0'x
, which is common on data grabbed from the web).
You can try:
data want;
set test;
number = input(compress(stringNumber,,'kd'),3.);
put number=;
run;
which will remove all non-digit characters from the string. If you're confident in your string having only numbers and spaces-of-some-form, then this should be safe; if it might contain other characters which might validly indicate a record you don't want (including things like "1.3" where this will return "13"), this could be risky without more detailed coding.
Upvotes: 1