| www.sas.com > Service and Support > Technical Support |
![]() |
![]() |
| TS Home | Intro to Services | News and Info | Contact TS | Site Map | FAQ | Feedback |
|
|
![]()
/***********************************************************************/
/* Title: Using the REVERSE function */
/* */
/* Goal : Read numeric values which contain a minus sign at the end of */
/* the value. */
/* */
/* First read the variable in as character. Use the REVERSE */
/* function to 'flip' the value, making the minus sign, if */
/* present, the first character in the string. Left justify */
/* the result with the LEFT fuction to remove any leading */
/* blanks. Use SUBSTR to extract the first character. */
/* */
/* If the extracted value is a minus sign, COMPRESS the minus */
/* sign from the character value and use the INPUT function */
/* with the W.D informat to convert the character value to */
/* numeric, and multiply the result by -1. */
/* */
/* If the extracted character is not a minus sign, create the */
/* numeric variable by using the INPUT function with the W.D */
/* informat. */
/* */
/* Note : Starting with SAS 9.0, the TRAILSGNw. informat is available */
/* to read numeric values that contain a trailing minus sign. */
/* */
/***********************************************************************/
data amounts;
input @1 charamt $char8.;
sign=substr(left(reverse(charamt)),1,1);
if sign='-' then amount=input(compress(charamt,'-'),8.)*-1;
else amount=input(charamt,8.);
keep charamt amount;
datalines;
3010.05-
99.10-
70.35
0.00
549.22-
;
proc print;
run;
/* RESULTS */
Obs charamt amount
1 3010.05- -3010.05
2 99.10- -99.10
3 70.35 70.35
4 0.00 0.00
5 549.22- -549.22
|
|||