Hi, I'm trying to figure out the coding for how I can format this data set into 3 significant figures with the exact presentation listed below.
Thanks
data have;
input x;
datalines;
31.45
0.02355
0.45
34
4723
;
run;
Want:
Hi @Hello_there,
You could create a format based on a user-defined function:
data have;
input x;
datalines;
31.45
0.02355
0.45
34
4723
-9994.999
-9995
-1.235e-15
0
9.876e15
9.999e15
.
.A
;
proc fcmp outlib=work.funcs.test;
function sig3dig(x) $32;
length f $32;
if x then do;
m=floor(log10(abs(x)));
y=round(x,10**(m-2));
m=floor(log10(abs(y)));
f=if m>1 then left(put(y,32.))
else left(putn(y,cat('32.',2-m)));
end;
else f=put(x,1.);
return(f);
endsub;
run;
options cmplib=work.funcs;
proc format;
value sig3dig (default=32)
other=[sig3dig()];
run;
proc print data=have;
format x sig3dig.;
run;
This should work for numbers with absolute values in a range from about 1E-15 through 1E16. (I assume that the "31.6" in your sample output is a typo and should read 31.5.)
I am not entirely sure what format you want to add, as the data has changed in various different way.
If what you are asking, is to have 3 decimals then a format like this should do the job:
data want;
set have;
format x comma10.3;
run;
This will give you this result:
@Hello_there wrote:
Hey Mike, thanks for the reply. The final result I'm looking for is how I have it screenshot in the original post. There is some code doing a quick online search on how to get 3 significant figures, but the difficulty I'm having is attaching 1 zero at the end of the values in row 3 and row 4. I'm sure there has to be some kind of conditional logic I have to use to be able to do it, but I was looking for a more efficient way.
How do you know the zeros belong at the end of the third and fourth values?
That is :
How do you know that 0.45 was measured to the thousandths place and not the hundreds place?
And how do you know that 34 was measured to the tenths place instead of the ones place?
Hi @Hello_there,
You could create a format based on a user-defined function:
data have;
input x;
datalines;
31.45
0.02355
0.45
34
4723
-9994.999
-9995
-1.235e-15
0
9.876e15
9.999e15
.
.A
;
proc fcmp outlib=work.funcs.test;
function sig3dig(x) $32;
length f $32;
if x then do;
m=floor(log10(abs(x)));
y=round(x,10**(m-2));
m=floor(log10(abs(y)));
f=if m>1 then left(put(y,32.))
else left(putn(y,cat('32.',2-m)));
end;
else f=put(x,1.);
return(f);
endsub;
run;
options cmplib=work.funcs;
proc format;
value sig3dig (default=32)
other=[sig3dig()];
run;
proc print data=have;
format x sig3dig.;
run;
This should work for numbers with absolute values in a range from about 1E-15 through 1E16. (I assume that the "31.6" in your sample output is a typo and should read 31.5.)
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.