BookmarkSubscribeRSS Feed
osbornejo
Obsidian | Level 7

I want to format my data so that decimals are only shown for non-whole numbers.  e.g.

 

Count

1,000

2

578

7.1

67,253.52

22,000

3,211

...

 

I've tried playing with the commaw.d format, but that appends trailing zeros to every number depending on the value of d.

 

Edit:

A bit more context - my data is count/sum data grouped by a categorical variable, and while the vast majority of the rows are, by nature, whole numbers, some of the items can have decimal counts.  However, because the decimal items are so sparse, I'd rather not include decimals for every number since it's largely unnecessary and distracting.

 

Adjusted example numbers to use commas.

6 REPLIES 6
Tom
Super User Tom
Super User

A little more context will help if finding the right solution for you.   It does not really make much sense to not show the zeros if the numbers are all of the same type.  But if the values in that variable are really from different things and you have some other variable that indicates which type they are then you could use that to help you create a text value that looks the way you want.  Or perhaps with PROC REPORT produce a report that looks like you want from the existing numeric variable.

If you do not already have something to tell you which style to display the number then test if the COUNT is an integer or not and then generate the new character variable based on that test.

data want;
  set have;
   length string $10.;
   if count=int(count) then string=put(count,7.);
   else count=put(count,10.2);
run;
osbornejo
Obsidian | Level 7
See my edit for more context
Tom
Super User Tom
Super User

Did you try the BEST format?

1     data have;
2       input Count;
3       put count best12. ;
4     cards;

           1
           2
           5
         7.1
        6.52
           2
           3
osbornejo
Obsidian | Level 7
I didn't realize that BEST could format each row differently. However, I didn't use a very good example - some of my numbers are in the thousands, so I'd like to have commas.
SASKiwi
PROC Star

I suggest you try a user-defined PICTURE format, using the BEST format for numbers less than 1,000 and create your own COMMA format for numbers 1,000 or greater.

Tom
Super User Tom
Super User

Don't really understand how you can have some values with fractions and the rest as whole numbers.

 

If the fractional values only appear in small values then you could use a custom format.

 

proc format ;
value commad 
  0<-<1
 ,1<-<2
 ,2<-<3
 ,3<-<4
 ,4<-<5
 ,5<-<6
 ,6<-<7
 ,7<-<8
 ,8<-<9
 ,9<-<10
  = [10.2]
  other = [comma10.]
;
run;

data test;
  input count :comma.;
  put count commad.;
cards;
1,000
2
578
7.1
67,253.52
22,000
3,211
;

But that would get a little large if you wanted support values like 67,253.52.

 

Otherwise just generate text.

175   data test;
176     input count :comma.;
177     string = put(count,comma10.2);
178     string = tranwrd(string,'.00',' ');
179     put count comma10.2 +1 string ;
180   cards;

  1,000.00 1,000
      2.00 2
    578.00 578
      7.10 7.10
 67,253.52 67,253.52
 22,000.00 22,000
  3,211.00 3,211

 

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 881 views
  • 2 likes
  • 3 in conversation