BookmarkSubscribeRSS Feed
knveraraju91
Barite | Level 11

Dear,

 

I need find maximal decimal digits in value for each var.  Please suggest. Thank you

In my data below the output needed;

var    maxdecimal

a          3

b          1

c          6

d          0

data have;
input var $ value;
datalines;
a 100.01
a 110.003
a 100
b 1.1
b 2
c 5.000002
c 6.01
c 4
d 2
d 5
;
2 REPLIES 2
novinosrin
Tourmaline | Level 20

data have;
input var $ value;
datalines;
a 100.01
a 110.003
a 100
b 1.1
b 2
c 5.000002
c 6.01
c 4
d 2
d 5
;

data want;
 do until(last.var);
  set have;
  by var;
  max_dec=max_dec <> lengthn(scan(cats(value),2,'.'));
 end;
 drop value;
run;
PGStats
Opal | Level 21

You must trust SAS number to string conversion algorithms (which are pretty good) for this to work:

 

proc sql;
select
    var,
    max(lengthn(scan(put(value, best32.), 2, "."))) as maxDecimal
from have
group by var;
quit;
PG

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 2 replies
  • 1376 views
  • 2 likes
  • 3 in conversation