BookmarkSubscribeRSS Feed
Uli
Calcite | Level 5 Uli
Calcite | Level 5
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
4 REPLIES 4
Patrick
Opal | Level 21
Shouldn't it be "return(SAM);" instead of "return(SAMP);"?
Uli
Calcite | Level 5 Uli
Calcite | Level 5
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
SPR
Quartz | Level 8 SPR
Quartz | Level 8
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
chang_y_chung_hotmail_com
Obsidian | Level 7

/* 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
*/

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1636 views
  • 0 likes
  • 4 in conversation