BookmarkSubscribeRSS Feed
shivkr
Fluorite | Level 6

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
6 REPLIES 6
data_null__
Jade | Level 19

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?

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

data_null__
Jade | Level 19

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     
Ksharp
Super User

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

shivkr
Fluorite | Level 6

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 863 views
  • 3 likes
  • 4 in conversation