Brad
Brad

Reputation: 705

How Do I Convert A String Into a Number in SAS?

I'm pretty new to SAS and struggling with the type conversions. They seem to be anything but intuitive to me. I need to do this within SAS PROC SQL statements.

Can someone help me figure out the best way to convert a field that is formatted like the below into a number so it can have some addition/subtraction performed and be compared to a numerical field?

Field format example: ' +3,820.00'

I've used strip() to get rid of the + sign, comma, and spaces and then input() to make it a number, but I'm not having any luck.

Upvotes: 1

Views: 1980

Answers (3)

Joe
Joe

Reputation: 63424

The commaw.d informat will deal with all of that without any stripping/etc. It happily takes the + sign as positive and the comma/decimal. This works in SQL as well, but easier to show example as below...

data _null_;
  x=' +3,820.00';
  y=input(x,comma12.);
  put x= y=;
run;

Upvotes: 4

Kurt Andrews
Kurt Andrews

Reputation: 79

The input is the proper way to convert strings to numerics and you can use input in proc sql select statements (See character to numeric conversion while sql extracting for example). There are informats that you can pass to the input function that will help you with commas & signs so that you don't have to strip them out prior to calling input. More information can be found here: Informats by Category.

If this doesn't help, please let me know.

Upvotes: 1

Bharath
Bharath

Reputation: 95

use compress rather than strip

field1=input(compress(field,'+,'),6.);

or

field1=input(compress(field,'+,'),comma10.2)format=comma10.2;

Upvotes: 1

Related Questions