## DECIMAL PRECISION ON THE RUN TIME

Occasional Contributor
Posts: 12

# DECIMAL PRECISION ON THE RUN TIME

Hi Guys,

I want to apply different precision to the same variable depending on other variable value without using round function.

TABLE

ValueDecimal place
11
2.33
04
3.56782
0.00025
4.4034
1.7030

DESIRABLE OUTPUT

ValueDecimal placeFinal value
111.0
2.332.300
040.0000
3.567823.56
0.000250.00020
4.40344.4034
1.70301
Super User
Posts: 9,599

Posts: 3,852

## Re: DECIMAL PRECISION ON THE RUN TIME

The suggestion by won't work using the W.D format because it rounds.

Is the variable (column) you have labeled as VALUE character or numeric?  That will be important to the solution.

Why don't you want to round?

Super User
Posts: 9,599

## Re: DECIMAL PRECISION ON THE RUN TIME

Sorry, I just assumed he didn't want to use that function, not that he didn't want rounding.  I would assume the only reason not to use rounding would be significant figures?  If some then its a fair bit more complicated, though there are quite a few formulae on the net to do that - or of course just use string functions.

Posts: 3,852

## Re: DECIMAL PRECISION ON THE RUN TIME

I don't disagree but looking at 's  final value column implies that D is truncated.  The problem is vague at best.

This might work. Assuming character value.

data have;
infile cards dsd dlm='09'x;

input value \$ d;
length new \$32;

if missing(d)

then new = value;
else do;
new = put(input(value,
F32.),F32.20-l);
new = substrn(new,1,d+find(new,'.')-(d=0));
end;

cards;
1  1
2.3   3
0  4
3.5678   2
0.0002   5
4.4034
1.703 0
;;;;
run;
proc print;

run;
Obs    value     d    new

1     1         1    1.0
2     2.3       3    2.300
3     0         4    0.0000
4     3.5678    2    3.56
5     0.0002    5    0.00020
6     4.4034    .    4.4034
7     1.703     0    1
Super User
Posts: 10,787

## Re: DECIMAL PRECISION ON THE RUN TIME

I would like to use PUTN as RW9 suggested, and proc format wouldn't round it .

```data have;
infile cards  dlm=' ' truncover;
input value  d;
format value 10.0 ;
cards;
1  1
2.3   3
0  4
3.5678   2
0.0002   5
4.4034
1.703 0
;;;;
run;
proc format;
picture dec0_
low-high='9';
picture dec1_
low-high='9.9';
picture dec2_
low-high='9.99';
picture dec3_
low-high='9.999';
picture dec4_
low-high='9.9999';
picture dec5_
low-high='9.99999';
run;

data have;
set have;
format=ifc(missing(d),'best32.',cats('dec',d,'_'));
new=putn(value,format);
run;

```

Xia Keshan

Occasional Contributor
Posts: 12

## Re: DECIMAL PRECISION ON THE RUN TIME

Thanks all for your suggestions. Variable (column) labeled as VALUE is numeric.

I didn't wanted to use round function because

SAY

A=0.696

B=0.7

if A=B then color=RED

if A <B then color=GREEN

I am exporting these values to a dashboard where variable A value should appear as one decimal precision.

now if I use ROUND(A,0.1) THEN A value would be 0.7, In the dashboard value of A AND B would appear as 0.7 and COLOR value as GREEN.

Thanks

Shiv

Discussion stats
• 6 replies
• 341 views
• 3 likes
• 4 in conversation