BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mike24
Calcite | Level 5

hi community,

what technique can i use to get rid of zeros in sas numbers.

 

for eample, i have inputs:

 

1.500

-0.400

1.000

0.100

99.90

-99.90

 

 

and need these outputs,

 

1.5

-.4

1.0

.1

99.9

-99.9

 

what's the best way to do this?

all i want to do is remove the zeros from the numbers.

 

appreciate any help from the community on this.

thanks.

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

So to adjust that for just a single decimal place you could use:

proc format;
picture nozeros (fuzz=0)
   -1 < - < 0 = '9' (prefix='-.' mult=10)
    0  - < 1  = '9' (prefix='.' mult=10)
    other = [17.1]
;
run;

Make sure to round the values first.  Use the -L modifier on the format specification if you want the leading spaces removed.

824   data test;
825     do i=-1234567,-123.456,-1,-.99,0,.1,.9999,1,1.1,1.234,123456789;
826        j=round(i,0.1);
827        put i best10. ' | ' i nozeros.-l @40 j best10. ' | ' j nozeros.-l ;
828     end;
829   run;

  -1234567 | -1234567.0                  -1234567 | -1234567.0
  -123.456 | -123.5                        -123.5 | -123.5
        -1 | -1.0                              -1 | -1.0
     -0.99 | -.9                               -1 | -1.0
         0 | .0                                 0 | .0
       0.1 | .1                               0.1 | .1
    0.9999 | .9                                 1 | 1.0
         1 | 1.0                                1 | 1.0
       1.1 | 1.1                              1.1 | 1.1
     1.234 | 1.2                              1.2 | 1.2
 123456789 | 123456789.0                123456789 | 123456789.0

View solution in original post

11 REPLIES 11
AMSAS
SAS Super FREQ
SAS only stores the number without leading/trailing zeros, so unless this is a character variable then the zeros are not stored.
Having said that zeros could be displayed if it's a numeric variable and you have a format on it e.g. z10.5
AMSAS
SAS Super FREQ

Here's some sample code:

 

data got ;
	infile cards ;
	input nVar cVar $ ;
	put nVar= cVar= ;
cards;
1.500 1.500
-0.400 -0.400
1.000 1.000
99.90 99.90
-99.90 -99.90 
;
run ;

When you run that you will get the follow in the log:

35   data got ;
36       infile cards ;
37       input nVar cVar $ ;
38       put nVar= cVar= ;
39   cards;

nVar=1.5 cVar=1.500
nVar=-0.4 cVar=-0.400
nVar=1 cVar=1.000
nVar=99.9 cVar=99.90
nVar=-99.9 cVar=-99.90

As you can see the zeros are only kept in the character variable (cVar) and not in the numeric variable (nVar)

mike24
Calcite | Level 5
Hi,

I ran the example, this helped out.

Thanks.



##- Please type your reply above this line. Ni o attachments. -##
mike24
Calcite | Level 5
Hi,

Thanks for the info.


mklangley
Lapis Lazuli | Level 10

Are these numeric values or character strings? If numeric, try this:

num2 = round(original_num, 0.1);

If character strings, try this:

num2 = round(input(original_num, 8.3), 0.1);

 

mike24
Calcite | Level 5
Useful rounding mechanism...

Thanks!


ballardw
Super User

 

If you want to display 1 decimal place then use a format that has .1 for the decimals:

data junk;
   input x;
   put x= 6.1;
datalines;
1.500
-0.400
1.000
0.100
99.90
-99.90
;

For example. If you want no zeroes for values between -1 and 1 you will have to spend some time working on a custom format that is also misleading to some extent. When ever I see a value like .4 is wonder what got left off.  The 0 in 0.4 is significant and for almost any real purpose should not be removed.

 

So, what use case do you have that requires 0.4 to display as .4 where a leading 0 causes and error or actual problem?

 

mike24
Calcite | Level 5
Hi ballardw,



This helps out some, I went back and changed my program to use 10.1 numbers.

These numbers are for simple display only.



I found a picture format in SAS 9.4 windows help, but I'm still studying it to see if it'll work for me.

Thanks.


proc format;
picture nozeros (fuzz=0)
low - -1 = '000.00' (prefix='-')
-1 < - < -.99 = '0.99' (prefix='-.' mult=100)
-0.99 <-< 0 = '99' (prefix='-.' mult=100)
0 = '9.99'
0 < -< .99 = '99' (prefix='.' mult=100)
0.99 - < 1 = '0.99' (prefix='.' mult=100)
1 - high = '09.99';



run;


Tom
Super User Tom
Super User

So to adjust that for just a single decimal place you could use:

proc format;
picture nozeros (fuzz=0)
   -1 < - < 0 = '9' (prefix='-.' mult=10)
    0  - < 1  = '9' (prefix='.' mult=10)
    other = [17.1]
;
run;

Make sure to round the values first.  Use the -L modifier on the format specification if you want the leading spaces removed.

824   data test;
825     do i=-1234567,-123.456,-1,-.99,0,.1,.9999,1,1.1,1.234,123456789;
826        j=round(i,0.1);
827        put i best10. ' | ' i nozeros.-l @40 j best10. ' | ' j nozeros.-l ;
828     end;
829   run;

  -1234567 | -1234567.0                  -1234567 | -1234567.0
  -123.456 | -123.5                        -123.5 | -123.5
        -1 | -1.0                              -1 | -1.0
     -0.99 | -.9                               -1 | -1.0
         0 | .0                                 0 | .0
       0.1 | .1                               0.1 | .1
    0.9999 | .9                                 1 | 1.0
         1 | 1.0                                1 | 1.0
       1.1 | 1.1                              1.1 | 1.1
     1.234 | 1.2                              1.2 | 1.2
 123456789 | 123456789.0                123456789 | 123456789.0
mike24
Calcite | Level 5
Tom,



Thanks for your help.

The picture format you provided worked great!




Tom
Super User Tom
Super User

Are your inputs character strings or numbers?  Do you want as output character strings or numbers?

Note that SAS stores numbers as binary floating point. The string 000.5000 represents the exact same number as the string .5. Also note that floating point numbers cannot store every decimal representation exactly.  Values like .1 and .3 will be approximated.

 

Can you clarify the rules?

It looks like you want integers displayed with a period and zero in the tenths place.

It is not clear what you want when the number is not an exact multiple of 1/10.  Do you want to display more digits after the decimal place?  Round to nearest tenth?  Truncate/floor to the tenth?  

 

Do you really have to remove the leading zero for values with absolute values less than 1? If not the just using the normal numeric display formats will work.  For example 10.1 will display one decimal place.  And BEST10.1 will remove the period and decimal place from integer values.

 

418   data test;
419     input string $10. @1 number ;
420     put string $10. +1 number 10.1 +1 number best10.1;
421   cards;

1.500             1.5        1.5
-0.400           -0.4       -0.4
1.000             1.0          1
0.100             0.1        0.1
99.90            99.9       99.9
-99.90          -99.9      -99.9

 

 

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
  • 11 replies
  • 8348 views
  • 0 likes
  • 5 in conversation