Before and After Line Plots with Proc SGPLOT
When graphically displaying "before" and "after" values in a line plot it is often desirable to display the results for each individual but also to group then by some other characteristic by using the same line style and colour for each group. An example of this might be the representation of blood pressure measurements before and after some intervention in a clinical trial for a set of individuals who are to be further classified by their gender.
The main issue that we need to overcome when graphing this data is that there are now two classification factors to take into account (individual and gender in our example), but we can only easily classify by one. There are a number of ways of tackling this problem, but one of the easiest to implement is to introduce missing values in the response variable at strategic points in the data to cause the line to "break".
The code below generates some fictional blood pressure data, introduces some missing data for each individual and then uses Proc SGPLOT to generate the line plot. Note that the data must be appropriately sorted before the missing data is introduced and the use of the BREAK option in the SERIES statement of Proc SGPLOT. The DATALABEL option is also used to identify the ID number of each individual at the beginning and end of each line.
input id gender $ when $ diabp @@;
1 M BEFORE 89 1 M AFTER 81 2 M BEFORE 88 2 M AFTER 82 3 F BEFORE 84 3 F AFTER 82 4 F BEFORE 85 4 F AFTER 80
data bp1; set bp; by id when notsorted; output; if last.id then do; diabp=.; output; end; run;
proc sgplot data=bp1; series x=when y=diabp / group=gender break datalabel=id; run;