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.
/*
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;
To actually round a value:
myvar= round(myvar, 0.0001)
Just for printing (display) without changing the stored value:
format myvar 16.4;
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;
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?
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?
/*
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;
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;
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.
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.
Ready to level-up your skills? Choose your own adventure.