Hi, The provided answer is pretty close to what you want but not identical. Note that in my solution, variable D converts to character type since in my opinion, you cannot show the significant digits in numeric type(the problem comes from the appending zero's). If you don't want to know about the function I wrote, you can just skip it. There are many ways to implement significant digits. Please note that the function sigFig() is a user defined function rather then a SAS data step function. * YOUR RAW DATA ;
data dat;
infile datalines;
input A: $10. B C D;
datalines;
N 36 36 36
Mean 9581.1728 9791.5262 1366.1106
SD 2965.8626 2982.0749 398.3543
Geo_Mean 9170.2867 2.0864674 1312.3213
CV 13.95511 13.455669 0.159741
;run;
* SPLIT DATASET ;
data dat_1 dat_2;
set dat;
i+1; /* Display order */
if A = "N" then output dat_1;
else output dat_2;
run;
*******************************************************************************
* Function we will use: sigFig ;
* Note: It's easy to convert functions in PROC FCMP to data step ;
libname my "/home/wangc187/SAStraining/FCMP"; /* Set your physical location */
proc fcmp outlib=my.func.format;
function rndgt(x, sf);
* Round to the specified digit according to significant figure ;
I=if x < 1 then 0 else 1;
rd=int(log10(x)) - sf + I;
return(round(x, 10**rd));
endsub;
function extsf(x) $30;
* Extract significant digits from a number ;
attrib sfc length=$30;
xc=compress(put(x, BEST30.)); /* Char. of x */
* Eliminate the decimal point and leading 0's;
sfc=prxchange("s/\.//", -1, xc); /* Eliminate decimal point */
sfc=prxchange("s/0*([1-9][0-9]*)/$1/", -1, sfc); /* Eliminate leading 0's */
return(sfc);
endsub;
function sigFig(x, sf) $30;
* Show significant figure result as a Char. ;
attrib sfc length=$30;
* 1. Round number ;
xr=rndgt(x, sf); /* Rounding result */
xrc=compress(put(xr, BEST30.)); /* Char. of xr */
* 2. Adding trailing 0's ;
n0=sf - length(extsf(xr)); /* Number of adding 0's */
if n0 > 0 then do; /* The case we need to add 0's */
zeros=repeat("0", n0 - 1); /* zeros we add*/
out=if index(xrc, ".") then cats(xrc, zeros)
else cats(xrc, ".", zeros); /* For integer, adding decimal point */
end;
else out=xrc;
return(out);
endsub;
options cmplib=my.func;
/* Set the searching route according to PROC FCMP outlib= */
******************************************************************************;
* APPLY FORMATS ;
data dat_2f(drop=D rename=(_D=D));
set dat_2;
format B C 10.3;
_D=sigFig(D, 5);
run;
* APPEND DATASETS ;
* Change data type for variable D in dat_1 for dataset appending ;
data dat_1d(drop=D rename=(_D=D));
set dat_1;
_D=put(D, 2.);
run;
* Append ;
proc sql;
create table result(drop=i) as
select *
from(
select *
from dat_1d
union
select *
from dat_2f)
order by i;
quit;
... View more