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*&amp;radius; area=pi*(&amp;radius*&amp;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).

Have a happy pi day!

Share

The Graph Guy!

Robert has worked at SAS for over 25 years, and is perhaps the foremost expert in creating custom graphs using SAS/GRAPH. His educational background is in Computer Science, and he holds a BS, MS, and PhD from NC State University. He is the author of several conference papers, has won a few graphic competitions, and has written a book (SAS/GRAPH: Beyond the Basics).

1. 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 on

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