Hi,
I am trying to create a function in FCMP to convert a numeric to character and print to s significant figures. The function has three inputs, x - the numeric value, s - number of significant figures to be reported, l - number of figures to the right of the decimal point when printing (to allow decimal alignment when using different s.f. across different observations.
The function works but prints a w.d. format was too small to be printed note, I pulled all the code out into a dataset (test2) instead of the function and the note disappears. Any idea where it comes from or how I could debug?
While testing I found the issue comes only from the derivation of c and d and only for the first record in test. Test2 is set to produce same output as variable c in Test1.
data test;
a=2981248.76403; OUTPUT; a=1248.76403; OUTPUT; a=248.76403; OUTPUT;a=48.76403; output; a=8.76403;;OUTPUT;a=0.76403;;OUTPUT;a=0.076403;;OUTPUT;a=0.0076403;;OUTPUT;a=0.00076403;;OUTPUT;a=0.000076403;;OUTPUT;a=0.00007640387106801;;OUTPUT;
RUN;
proc fcmp outlib=work.funcs.test;
function sigdig(x,s,l) $200;
length g e f $200;
if x then do;
a=floor(log10(abs(x)));
b=a-(s-1);
c=round(x,10**(b));
cc=compress(put(x, best32.));
ccc=length(compress(scan(cc, 2, ".")));
d=floor(log10(abs(c)));
e=cat(compress(put(l, best.)),".");
ee=2-d+(s-3);
eee=if ccc>0 and ee>0 then min(ccc, ee)
else ee;
ff=if eee<=0 THEN l
ELSE l+1+eee;
f=cat(compress(put(ff, best.)),".",compress(put(eee, best.)));
g=if eee<=0 then putn(c,e)
else putn(c,f);
end;
else do;
e=cat(compress(put(l, best.)),".");
g=putn(c,e);
end;
return(g);
endsub;
run;
options cmplib=work.funcs;
data test1;
SET test;
c=sigdig(a, 3, 8);
d=sigdig(a, 4, 8);
e=sigdig(a, 5, 8);
f=sigdig(a, 6, 8);
g=sigdig(a, 7, 8);
h=sigdig(a, 8, 8);
RUN;
data test2;
set test (rename=(a=x));
l=8;
s=3;
length g e f $200;
if x then do;
a=floor(log10(abs(x)));
b=a-(s-1);
c=round(x,10**(b));
cc=compress(put(x, best32.));
ccc=length(compress(scan(cc, 2, ".")));
d=floor(log10(abs(c)));
e=cat(compress(put(l, best.)),".");
ee=2-d+(s-3);
if ccc>0 and ee>0 then eee=min(ccc, ee);
else eee=ee;
if eee<=0 THEN ff=l;
ELSE ff=l+1+eee;
f=cat(compress(put(ff, best.)),".",compress(put(eee, best.)));
if eee<=0 then g=putn(c,e);
else g=putn(c,f);
end;
else do;
e=cat(compress(put(l, best.)),".");
g=putn(c,e);
end;
run;
Apologies for the messiness of the code, I was going round in circles while testing,
Thanks
... View more