简体   繁体   中英

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.

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;

use compress rather than strip

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

or

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM