NewUsr_stat
NewUsr_stat

Reputation: 2587

Unable to convert a character variable with numbers with a comma into numeric

I have a set of variables in SAS that should be numeric but are characters. Numbers are comma separated and I need a point. For example, I need 19,000417537 to be 19.000417537. I tried translate without success. the comma is still there and I'm not able to convert the variable to numeric using input(). Can anyone help me please?

Thank you in advance

Best

Upvotes: 0

Views: 710

Answers (3)

Tom
Tom

Reputation: 51621

You can use the COMMA informat to read strings with commas in them. But if you want it to treat the commas as decimal points instead of ignoring them then you probably need to use COMMAX instead (Or perhaps use the NLNUM informat instead so that the meaning of commas and periods in the text will depending on your LOCALE settings).

So if the current dataset is named HAVE and the text you want to convert is in the variable named STRING you can create a new dataset named WANT with a new numeric variable named NUMBER with code like this:

data want;
  set have;
  number = input(string,commax32.);
run;

Upvotes: 0

Mike
Mike

Reputation: 4400

In two steps you can replace the , with . with tranwrd and then use input to convert it to numeric.

data yourdf;
     set df;
     charnum2=tranwrd(charnum, ",", "."); /*replace , with .*/
     numvar = input(charnum2, 12.); /*convert to numeric*/
run;

Upvotes: 0

Reeza
Reeza

Reputation: 21294

Use INPUT() with the COMMAX informat.

data have;
length have $20.;
have = "19,000417537";
want = input(have, commax32.);
format want 32.8;
run;

proc print data=have;
run;
Obs have    want
1   19,000417537    19.00041754

Upvotes: 2

Related Questions