SAS Programming

DATA Step, Macro, Functions and more
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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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