Rounding for decimals with +

Reply
N/A
Posts: 1

Rounding for decimals with +

I have a variable with data

X= +0.0125

X= +7.90

X= - 0.0076

X= - 0.5

Can anyone help me get all these numbers with same decimal place while keeping the + and - sign in the begining of each number.

Thank you- sign in the begining

Trusted Advisor
Posts: 1,228

Re: Rounding for decimals with +

Try this.

data have;

input x $;

datalines;

+0.0125

+7.90

-0.0076

-0.5

;

Super User
Posts: 11,104

Re: Rounding for decimals with +

How many decimal points do you want displayed? Do want trailing 0 to pad to that length?

Assuming you want 4 decimals then a custom format such as this should work. Make sure the data is read as numeric not character.

proc format library=work;
picture MyDec
low -<0 = '0009.9999'(prefix='-')
0 - high=  '0009.9999'(prefix='+');
run;

data have;
input x ;
datalines;
+0.0125
+7.90
-0.0076
-0.5
;
run;

proc print data=have;
format x MyDec.;
var x;
run;

Respected Advisor
Posts: 4,130

Re: Rounding for decimals with +

You've taken exactly the approach I was playing with as well when I run into some "picture" behaviour I didn't understand. https://communities.sas.com/thread/58118

Suggest you add "round" to your format like data _null_ suggested in the link posted.

...and: I believe '0' should just be '0' and not +0

Ask a Question
Discussion stats
  • 3 replies
  • 2443 views
  • 0 likes
  • 4 in conversation