DATA Step, Macro, Functions and more

issue with proc fcmp

Reply
Occasional Contributor Uli
Occasional Contributor
Posts: 8

issue with proc fcmp

Dear All,

Can you please help me out from the following issue. Proc fcmp is not giving the results as expected. Please find the following sample code.

data sample;
length SAMP $ 100;
input SAMP $20. ;
datalines;
0
12345
0.006789123
CATMNDR234
DMBR23>TCM
BLQ
93
;

proc fcmp outlib=work.funcs.dirs;
function sfunc( SAMP $) $ ;
length SAMP $ 30 ;
if compress(SAMP,"0123456789.") ne "" then do;
SAM=left(SAMP);
end;
else do;
if (0 < mod(input(SAMP,20.),1) < 1) then SAM=left(put(round(input(SAMP,8.),.00001),8.));
else if input(SAMP,8.) < 100 then SAM=SAMP;
else
SAM=left(put(round(input(SAMP,8.),100),8.));
end;
return(SAMP);
endsub;
run;

options cmplib=work.funcs;

data sss;
set sample;
length dc $ 30 ;
dc = sfunc(SAMP);
run;


In the above code numeric part is not giving as expected , can any one suggest me how i can resolve this.

Thanks in Advance.

Best Regards
Respected Advisor
Posts: 4,173

Re: issue with proc fcmp

Shouldn't it be "return(SAM);" instead of "return(SAMP);"?
Occasional Contributor Uli
Occasional Contributor
Posts: 8

Re: issue with proc fcmp

Dear Patrick,
Thanks for your Reply.

This was typo mistake. But still it is not giving the NUmeric part result.

could any one check and suggest me where i have to modify.

Thanks In Advance.

Best Regards
Super Contributor
Super Contributor
Posts: 365

Re: issue with proc fcmp

Hello Uli,

It is necessary to correct the following statement:

if (0 < mod(input(SAMP,20.),1) < 1) then SAM=left(put(round(input(SAMP,20.),.000000001),20.9));

HTH,
SPR
Regular Contributor
Posts: 241

Re: issue with proc fcmp

/* test data */
data one;
   infile cards truncover;
   input from :$20.;
cards;
   0
   0.006789123
   1
   53
   100
   12345
   a234
   234x
   dunkin
   -0.1
;
run;

/* special formatting */
proc fcmp outlib=work.star.bucks;
   function tall(n) $;
      return (if 0<=n & n<1
         then left(putn(round(n, .0001), "8.4"))
         else " ");
   endsub;
   function grande(n) $;
      return (if 1<=n & n<100
         then left(putn(n, "8."))
         else " ");
   endsub;
   function venti(n) $;
      return (if 100<=n & n<=constant('big')
         then left(putn(round(n,100), "best8."))
         else " ");
   endsub;
   function coffee(s $) $;
      length n 8 t $20 g $20 v $20;
      if verify(s, "0123456789. ") then return(left(s));
      n = inputn(s, "best20.");
      t = tall(n);
      g = grande(n);
      v = venti(n);
      return (coalescec(t,g,v));
   endsub;
quit;

%let cmplib = %sysfunc(getoption(cmplib));
options cmplib = (work.star &cmplib);
   data two;
      set one;
      length to $20;
      to = coffee(from);
   run;
options cmplib = (&cmplib);

/* check */
proc print data=two;
run;
/* on lst
      Obs  from           to
      1    0              0.0000
      2    0.006789123    0.0068
      3    1              1
      4    53             53
      5    100            100
      6    12345          12300
      7    a234           a234
      8    234x           234x
      9    dunkin         dunkin
     10    -0.1           -0.1
*/

Ask a Question
Discussion stats
  • 4 replies
  • 166 views
  • 0 likes
  • 4 in conversation