DATA Step, Macro, Functions and more

Character to numeric

Reply
Super Contributor
Posts: 647

Character to numeric

How to convert the values to numeric,at times there will be a comma sometimes there will not be a comma.
29,652 units
60mg
Valued Guide
Posts: 2,175

Re: Character to numeric

informat COMMAw. will deal with embedded commas correctly, but you might have a problem with "mg" tight against the numbers.

PeterC
Super Contributor
Posts: 359

Re: Character to numeric

z = input( prxchange('s/[A-Z a-z]/ /',-1, x), comma6.);
Super Contributor
Posts: 647

Re: Character to numeric

Thanks Flip.
Super Contributor
Posts: 647

Re: Character to numeric

Thanks Peter
Super Contributor
Posts: 647

Re: Character to numeric

Thanks Flip.
Contributor
Posts: 44

Re: Character to numeric

Hi,

I have the same problem. I want to convert a character-Variable into a numeric. But the tip above doesnt work correctly in my case.

My data is like (character-format):

0,8
1,2
12,3
10
1,2
.
.
.
after converting i want my data like (numeric-format):
0.8
1.2
12.3
10
1.2
.
.
.
if i do like above i get:
8
12
123
10
12
.
.
.

Any idea?

Thank you in advance. Marie Message was edited by: marieK
Respected Advisor
Posts: 3,894

Re: Character to numeric

Use informat commaX.

data have;
input varHave $;
datalines;
0,8
1,2
12,3
10
1,2
;

data want;
set have;
varWant=input(varHave,commax8.);
put varWant=;
run;
Contributor
Posts: 44

Re: Character to numeric

Juhuuu Smiley Happy thanks!
Super User
Posts: 9,682

Re: Character to numeric

Hi.
For the first question, you can use function translate() to convert ',' into '.',then use input() to get the numeric value.
[pre]
data _null_;
infile datalines;
input char $;
num= translate(char,'.',',');
put num=;
datalines;
0,8
1,2
12,3
10
1,2
;
run;
[/pre]
Note: num is also characteric value.

For the second question,you can use compress() to get rid of comma.then can achieve the numeric value.
[pre]
data _null_;
infile datalines;
input char $;
num= compress(char,',');
put num=;
datalines;
0,8
1,2
12,3
10
1,2
;
run;
[/pre]
Note: num is also characteric value.


Ksharp
Contributor
Posts: 44

Re: Character to numeric

Thank you! The translate-function is good Smiley Happy
Ask a Question
Discussion stats
  • 10 replies
  • 277 views
  • 0 likes
  • 6 in conversation