BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
mariko5797
Pyrite | Level 9

How can I round my data to 3 significant figures with a max of 4 decimal places? It doesn't seem SAS has any format for significant figures.

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
/*
You need customize a format by PICTURE.
*/
proc format;
picture fmt(round)
100-high='GE 100'(noedit)
10-<100 ='99.9'
1 -<10  =' 9.99'
0.1-<1='9.999'
low-<0.1='9.9999'
;
run;
data have;
input x;
format x fmt.;
cards;
110
90
12.3489
1.064
0.18912
0.018912
0.0018912
0.000189
;

proc print data=have noobs;run;

Ksharp_0-1704782094353.png

 

View solution in original post

8 REPLIES 8
Patrick
Opal | Level 21

To actually round a value: 

myvar= round(myvar, 0.0001)

 

Just for printing (display) without changing the stored value:

format myvar 16.4;

mariko5797
Pyrite | Level 9
The first condition is 3 significant figures though.
For example, 12.3489 would be printed as 12.3; 1.064 would be printed as
1.06; and 0.000189 would be printed as 0.0002.
Patrick
Opal | Level 21

And just for fun: If you really can't do it with a picture format then there is always the option to use a custom function in a format. That should give you almost "unlimited" options.

proc fcmp outlib=work.funcs.myfuncs;
  function sig_figure(in_num);

    s=put(round(in_num,.0001),e32.);

    c=round(input(scan(s,1,'E'),best32.),.01);
    p=input(scan(s,2,'E'),best32.);

    r=c*10**p;
    return(r);
  endsub;
run;
options cmplib=work.funcs;

proc format;
  value sig_num
    other=[sig_figure()]
  ;
quit;

data test;
  infile datalines truncover dsd;
  input have_num want_display :$16.;
  have_num2=have_num;
  datalines;
12.3489,12.3
1.064,1.06
0.000189,0.0002
;

proc print data=test;
  format have_num2 sig_num.;
run;

Patrick_0-1704784433832.png

 

ballardw
Super User

Can you describe how you round to 3 significant figures with 4 decimal places? If you are showing 4 decimals the implication is typically that all 4 decimal places are significant.

 

May some before/after examples?

mariko5797
Pyrite | Level 9

It’s a maximum of 4 decimal places, so in the event the number is too small,
e.g., 0.00193, we would display 4 decimal places, i.e., 0.0019. Otherwise, we only display 3 significant figures, e.g., 1.93056 would be displayed as 1.93.
Does that make sense?

Ksharp
Super User
/*
You need customize a format by PICTURE.
*/
proc format;
picture fmt(round)
100-high='GE 100'(noedit)
10-<100 ='99.9'
1 -<10  =' 9.99'
0.1-<1='9.999'
low-<0.1='9.9999'
;
run;
data have;
input x;
format x fmt.;
cards;
110
90
12.3489
1.064
0.18912
0.018912
0.0018912
0.000189
;

proc print data=have noobs;run;

Ksharp_0-1704782094353.png

 

mariko5797
Pyrite | Level 9
It's not a perfect method as it dropped the negative sign, but I could fix that with an if-then statement. Thank you!
Ksharp
Super User

If you want to extend to negative number , you could try this fotmat.

 

proc format;
picture fmt(round)
100-high='GE 100'(noedit)
10-<100 ='99.9'
1 -<10  =' 9.99'
0.1-<1='9.999'
0-<0.1='9.9999'

-0.1-<0='09.9999'(prefix='-')
-1-<-0.1='09.999'(prefix='-')
-10 -<-1  ='09.99'(prefix='-')
-100<-<-10 ='099.9'(prefix='-')
low- -100='LE -100'(noedit)

;
run;
data have;
input x;
format x fmt.;
cards;
110
90
12.3489
1.064
0.18912
0.018912
0.0018912
0.000189
-0.000189
-0.0018912
-0.018912
-0.18912
-1.064
-12.3489
-90
-110
;

proc print data=have noobs;run;

Ksharp_0-1704876173603.png

 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 2349 views
  • 1 like
  • 4 in conversation