To Find the length of numeric variable

Regular Contributor
Posts: 229

To Find the length of numeric variable

I have anumeric variable no i want the length of that numeric variable

data l;
input sal;
cards;
5449844894949849
17188
5454548978754545415
211151
run;

Dont keep \$ after sal.if i keep \$ after sal i am getting but i want it in numeric only
Not applicable
Posts: 0

Re: To Find the length of numeric variable

Hi sas_user,

int(log10(sal))+1;

will give you the length of the number, assuming no decimals. You then just need to find the maximum, either using proc sql, a data step or another proc.
Posts: 2,124

Re: To Find the length of numeric variable

Be careful with really long numbers. SAS stores all numbers in floating point and can only represent integers to certain level of precision. See the companion for your OS to determine the maximum length of an integer that can be represented for a particular number of bytes of storage.
Not applicable
Posts: 0

Re: To Find the length of numeric variable

If there were decimals, then, not withstanding Doc's comments, something like this might help:

data have;
input sal \$ 48.;
cards;
54498448949.49849
171.88
5454548978.754545415
2111.51
run;

data _null_;
set have end = eof;
retain int 0;
retain dec 0;
array digits(2);
digits(1) = scan(sal,1,'.');
digits(2) = scan(sal,2,'.');
if input(digits(1), 32.) gt int then int = digits(1);
if input(digits(2), 32.) gt dec then dec = digits(2);
if eof then do;
i = compress(int(log10(int))+1);
d = compress(int(log10(dec))+1);
t = compress(int(log10(int))+1 + int(log10(dec))+1 + 1) || '.' || compress(int(log10(dec))+1);
put 'NOTE: Integers: ' i ' Decimals: ' d ' suggested format: ' t;
end;
run;

Which gave me a message like this:

NOTE: Integers: 11 Decimals: 9 suggested format: 21.9
Discussion stats
• 3 replies
• 2612 views
• 0 likes
• 3 in conversation