BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
SASGeek
Obsidian | Level 7

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;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@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.

 

View solution in original post

3 REPLIES 3
FreelanceReinh
Jade | Level 19

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);
Tom
Super User Tom
Super User

@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.

 

SASGeek
Obsidian | Level 7
I guess when I'm tired I over-think it. Thanks so much for the help!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 1034 views
  • 2 likes
  • 3 in conversation