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.
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
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)
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);
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?
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
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
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.