How to use the SAS pi constant

7

With Pi Day coming up on 3/14, I wanted to make sure all you SAS programmers know how to use the pi constant in your SAS code...

All you have to do is use constant("pi") in a data step, and you've got the value of pi out to a good many decimal places (probably enough for most any practical scenario). In this example, I let the user specify the value for a circle's radius as a macro variable, and then get the value of the pi constant in a data step, and use that value to calculate the circumference and area of a circle with that radius. I convert those calculated dataset values into macro variables, and use the calculated macro variables in various ways in a GPlot and annotated circle.

Here's the code for doing the calculations and creating the macro variables - hopefully a light bulb is going off in your head right now, and you are thinking of all kinds of ways you could reuse this code!

%let radius=7.0;
 
data foo;
pi=constant("pi");
circumference=2*pi*&radius;
area=pi*(&radius*&radius);
run;
 
proc sql;
select unique pi format=comma20.18 into :pi separated by ' ' from foo;
select unique circumference format=comma8.3 into :circum separated by ' ' from foo;
select unique area format=comma8.3 into :area separated by ' ' from foo;
quit; run;

And here's what my graph looks like (here's the code if you'd like to see all the details).

pi_constant

 

Have a happy pi day!

Share

About Author

Robert Allison

The Graph Guy!

Robert has worked at SAS for over a quarter century, and his specialty is customizing graphs and maps - adding those little extra touches that help them answer your questions at a glance. His educational background is in Computer Science, and he holds a BS, MS, and PhD from NC State University.

7 Comments

  1. Bennett Rutledge on

    Reminds me of the bad old days when I had to hand-key "Tiny Basic" into my Sphere 310 in 8080 machine code. I had to hand build the trig functions, too, and my pi function was x*355/113. That only gives half as many significant digits as SAS does, and that rational approximation of pi has been around since only the fifth century(see Milu in Wikipedia) ... just a taste of how much Dr. Goodnight does for the modern statistician!

  2. The hyperlink under the word 'code' doesn't link to the code. It links to the same image that is already shown.

  3. gordon Keener on

    You can also:

    proc sql noprint;
    select unique
    pi format=comma20.14,
    circumference format=comma8.3,
    area format=comma8.3
    into
    :pi separated by ' ',
    :circum separated by ' ',
    :area separated by ' '
    from foo;
    quit;

    Since "foo" only has one row, you can also do without the "unique" and the "separated by"s.

    I also reduced the format precision to eat the trailing zeroes.

    • Robert Allison
      Robert Allison on

      Thanks Gordon! That's probably more efficient ... but I'd be afraid I would mis-match my "select"s and my "into"s. :)

Back to Top