- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hello all,
I have a column of values that are between 2-5 digits to the left of the decimal and two to the right of the decimal. What I want is to convert the number to a character but keep the comma and negative.
I tried the following and it comes close but not 100%. I need it into character for use in other places.
Thank you
Data Mine;
a = -4502.25;
b = put(a,8.2); /* this results in -4502.25 not -4,502.25 */
run;
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@SASGeek wrote:
Hello all,
I have a column of values that are between 2-5 digits to the left of the decimal and two to the right of the decimal. What I want is to convert the number to a character but keep the comma and negative.
I tried the following and it comes close but not 100%. I need it into character for use in other places.
Thank you
Data Mine;
a = -4502.25;
b = put(a,8.2); /* this results in -4502.25 not -4,502.25 */
run;
Not sure what you mean by "retain". Does the numeric variable currently display with thousand separators? Check what FORMAT is attached and use the same format. Or use the VVALUE() function and SAS will apply the format.
data want;
set have;
length charvar $32 ;
charvar=vvalue(numvar);
run;
To display 7 digits plus decimal place and optional negative sign and optional thousand's separator you will need 10 characters.
1601 data test; 1602 a=-4502.25; 1603 b=put(a,comma10.2); 1604 put a= b= ; 1605 run; a=-4502.25 b=-4,502.25
If you don't want the leading spaces in the variable B then either add the LEFT() function or add the -L modifier on the end of the format specification.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hello @SASGeek,
The COMMAw.d format will add the comma, provided that the width (w) is sufficient. You need w=10 for minus sign, 5+2 digits, comma and decimal point.
b = put(a,comma10.2);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@SASGeek wrote:
Hello all,
I have a column of values that are between 2-5 digits to the left of the decimal and two to the right of the decimal. What I want is to convert the number to a character but keep the comma and negative.
I tried the following and it comes close but not 100%. I need it into character for use in other places.
Thank you
Data Mine;
a = -4502.25;
b = put(a,8.2); /* this results in -4502.25 not -4,502.25 */
run;
Not sure what you mean by "retain". Does the numeric variable currently display with thousand separators? Check what FORMAT is attached and use the same format. Or use the VVALUE() function and SAS will apply the format.
data want;
set have;
length charvar $32 ;
charvar=vvalue(numvar);
run;
To display 7 digits plus decimal place and optional negative sign and optional thousand's separator you will need 10 characters.
1601 data test; 1602 a=-4502.25; 1603 b=put(a,comma10.2); 1604 put a= b= ; 1605 run; a=-4502.25 b=-4,502.25
If you don't want the leading spaces in the variable B then either add the LEFT() function or add the -L modifier on the end of the format specification.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content