Since most of my statistical analysis and data management is done in SAS, I constantly develop new pieces of code and macros to be able to make my work more efficient. Below, I’ve included some nifty codes samples/macros for download. They are all made available freely. (Naturally, I take absolutely no responsibility for any errors…)

Incidence density sampling macro

The IDS macro is designed to perform incidence density sampling using the risk set sampling method. The macro is capable of handling time-dependent variables, matching    and real-time specification of time axis of analysis. It is based on the SAS SQL Procedure. It may be used, circulated and modified freely. The macro can be downloaded here:

Directions for use:

The macro will read a specified dataset which, at minimum, should contain four variables: 1. a variable indicating the identity of the subject; 2. A variable indicating the date of start of follow-up; 3. A variable indicating end of follow-up and 4. A variable indicating failure or censoring at date of exit. In addition, the macro handles matching and the specification of different time-axis using optional specifications. Finally, if necessary, the macro can also perform matching on a generated random variable to improve efficiency.

SIR macro

The SIR macro calculates a standardized incidence ratio (SIR) with confidence intervals based on the observed and expected number of cases. The macro is pretty straightforward, but contrary to most other pieces of code that is circulating it uses a chi2-distribution for the confidence intervals which seems to come closer to an exact Poisson distribution (the ideal), which may be of some importance especially for smaller samples. The macro can be downloaded here:

There are seven options for the macro:

o=observed – indicating the observed number of cases

e=expected – indicating the expected number of cases

sir=sir – the variable which should hold the calculated SIR

l=lower – the variable for the lower bound of the confidence interval

u=upper – the upper bound of the confidence interval

lim=95 – the percentile width of the confidence interval.

alpha= – an alternative expression for the width of the confidence intervals.

On a side note, my colleague Klaus Rostgaard at Statens Serum Institut in Copenhagen has produced an incredibly useful set of SAS macros for stratifying (and aggregating) data according to time-varying covariates called pyrsstep. His excellent tool, which is especially useful for Poisson regression and calculating standardized incidence ratios in situations where you might want to consider several time axes simultaneously, can be downloaded here. He has also written a very informative paper describing the underlying principles that can be accessed from PubMed Central.

IR macro

The IR macro calculates an incidence rate with confidence intervals. The CI:s are again calculated using a Chi-2 Poisson estimation. The macro can be downloaded here:

The macro options are essentially the same as for the SIR macro, but an additional option ”factor” is available. This transforms the calculated incidence rate (and its CI:s) to the usual x number of cases per y person years. The default is 100,000.

Personnummer (NRN) macro

The CheckNRN macro  checks the date and check digit portions of a Swedish national registration number (NRN) and returns an  error code. It can also add a missing check digit depending on the corr option. The macro can be downloaded here:

The return variable (default ”err”) will take one of three values:
0 – No error
1 – Invalid NRN
2 – Valid NRN, but check digit added

Interval macro

The interval macro can be used to generate a pleasing output from a program that calculates SIR:s, IRR:s or Hazard Rations with matching confidence intervals. The macro is especially useful when you want to create or tabulate large numbers of point estimates with CI:s. It can be downloaded here: It is by no means an example of nice programming, but it does its job…