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;
@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.
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);
@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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.