Working with SAS macro variables

Believe it or not, I recently had to use SAS for a simulation study because it was the only system available that could maximize the exact partial likelihood of a Cox proportional hazards model for tied data (ties=exact) in a reasonable amount of time for a data set of 2,000 observations. This was the first time I ran a simulation in SAS. I made use of the MACRO capabilities of SAS to write generic functions that could be used to repeat steps; I might blog about my overall experience some day. Today, I’ll just write about one source of frustration that I had.

In writing my simulation study, I made use of Macro Variables (%let command) for the different scenarios I was investigating. After getting some unexpected results, I started debugging and then realized that whatever comes after the equal sign and before the semicolon of a %let statement is what the macro variable stores. For example, if I specify %let x = 2 + log(2) / λ ;, what gets stored in x is 2 + log(2) / λ. When I utilize &x, 2 + log(2) / λ gets substituted in the code (I incorrectly assumed this expression was evaluated), which can lead to unexpected results if it were used in mathematical expressions. For instance, if in a data step I do y = z / &x, then I might get something I wasn’t expecting: z / 2 + log(2) / λ instead of z / (2 + log(2) / λ). I then thought I needed to use %EVAL or %SYSEVALF, but these functions only work with additions and not other mathematical functions (e.g., log). I also considered using CALL SYMPUTN but it could only be used in a DATA step. My solution for getting the right math is to always surround expressions with parentheses: %let x = (log(2) / λ). This way, it’s safe to use &x as an entity.

To help with debugging and to see exactly how your code is running, I suggest the symbolgen option be turned on when macros are used:

 <pre class="example">options symbolgen ; /*show macro variable resolution*/

flags for compiling source code: pkg-config command

so i was trying to compile some C code on my server at school that involves the math and the GNU Scientific Library (gsl) libraries. that is, the source has the following lines:

 <pre class="src src-C"><span style="color: #b0c4de;">#include</span> <span style="color: #ffa07a;">&lt;stdio.h&gt;</span>

#include <math.h> #include <gsl/gsl_integration.h>

those link the header files. however, when compiling, gcc would complain that they can’t see functions such as sqrt, abs, etc, from the math library. it also complains about my call to a function from the gsl library. i googled the math library stuff and realized i need to pass the ‘-lm’ argument in gcc for the math library. similarly, i saw somewhere that refers to the ‘-gsl’ argument. however, this did not work for me. i tried many things. some more googling, i found this thread that shows the ‘pkg-config’ command that shows exactly what arguments u need for which library.

So, i get:

 <pre class="src src-sh">$ pkg-config --libs gsl

-lgsl -lgslcblas -lm

so i just need to execute:

 <pre class="src src-sh">gcc -lgsl -lgslcblas -lm myprogram.c

i really don’t understand how the GNU compilers work. on my mac, i don’t have to pass those arguments. on my debian server, i need to. at first i thought that including the header was enough. will need to learn more about this stuff.

PS I also remember using this command when i had a hard time compiling shell-fm on my mac os x.

UPDATE: so on my mac os x, after installing gsl, i need to use ‘gcc -lgsl myprog.c’ to use the gsl library. things are fine now. I also want to note that i could not compile GSL form source on my debian server…had to rely on apt-get.

Sage (again): everything math

I blogged about Sage in the past and stated that I won’t be using it much since R is my language/environment of choice. This is still true, but I wanted to write a few more comments about Sage after toying with it a bit more.

Sage is based on Python (good!) and its mission statement is

Mission: Creating a viable free open source alternative to Magma, Maple, Mathematica and Matlab.

I like it. If I need to solve an equation, factor, do partial fraction decomposition, do Taylor expansions, find derivatives, integrate, and all else math, Sage is there for me. It’s both free (open-source) and easy to use. The learning curve is pretty low if you want to do basic things like create examples for teaching Calculus. Plotting is also great but R is superior in my opinion. Sage graphics outperforms R graphics in one respect: it can include and display LaTeX equations natively (uses matlibplot, which is based on GNU-plot, I think). Sage also displays the vertical and horizontal axes in the center of a plot, similar to the graphs in textbooks I grew up with. Sage graphics seems more geared towards teaching whereas R is geared towards professional publishing.

Personally, I’ll use Sage when I teach stuff like Calculus where I need plots with axes and all other math features that R isn’t built for.

There is a sage mode for emacs, however, is in alpha mode as of now, so the features aren’t comparable to ESS is for R.

Another great thing about Sage is it has a notebook GUI that allows it to be run inside a web browser. Therefore, you can run a Sage server that allows users run Sage interactively! See this for example. You can run notebook() on your own computer or create an account on the previous site to test it out