Proc SQL and getting the length of a number

Reply
Contributor
Posts: 20

Proc SQL and getting the length of a number

Hi

Is it possible to get the lenght of a number in proc sql. Even better if the number of decmail places is included.

I tried converting the number to a character and then getting the length. But this automatically rounded the numbers removing the decimal places. Which is not what I want

Respected Advisor
Posts: 4,919

Re: Proc SQL and getting the length of a number

So, you would want length=infinity for irrational numbers such as Pi and e, as well as for rational numbers with infinite decimal expansions such as 1/3?

PG

PG
Super Contributor
Posts: 307

Re: Proc SQL and getting the length of a number

You could try something like this . . .adapt to make it work with your data . . . .

/* generate some random numbers for illustration */

data have ;
drop i;
do i = 1 to 100;
  id + 1;
  x = round(ranuni(i)*10000,.01);
  OUTPUT;
END;
RUN;

/* convert number to character and determine length */

/* note: result will be dependent on number of decimals and includes decimal point */

proc sql;
select t.*, strip(put(t.x, 12.2)) as xchar format=$15., length (strip(put(t.x, 12.2))) as lenxchar
from have t;
quit ;

Ask a Question
Discussion stats
  • 2 replies
  • 194 views
  • 0 likes
  • 3 in conversation