Hello,
I use SAS EG 8.3 and I have a small dataset "have" with variables subject, score at baseline, and score at month 1.
data have;
input subject Baseline month1;
cards;
23 4 4
26 3 0
36 3 0
39 4 1
41 4 3
42 0 3
46 0 1
49 1 0
51 0 0
55 2 4
56 2 1
57 4 1
73 2 0
83 2 0
84 1 1
85 3 2
89 0 0
96 2 0
101 2 3
112 2 0
117 4 0
120 2 1
124 2 0
;
run;
How can I produce a spaghetti plot that looks like this one attached (below)? I would also like to add a line for the median (that is the black dashed line in the attachment).
Thank you!
Plots will typically require an Xaxis value and Yaxis value for most types of plots. So your data needs to be shaped to match that requirement.
proc transpose data= have out=canplot prefix=y; by subject; run; proc sgplot data=canplot; series x=_name_ y=y1 / group=subject; label _name_='Time';
label y1='Score'; run;
With the basic plot out of the way, what do you mean by "median" in this case?
You will need to calculate it and either add to the data or annotate .
data have;
input subject Baseline month1;
cards;
23 4 4
26 3 0
36 3 0
39 4 1
41 4 3
42 0 3
46 0 1
49 1 0
51 0 0
55 2 4
56 2 1
57 4 1
73 2 0
83 2 0
84 1 1
85 3 2
89 0 0
96 2 0
101 2 3
112 2 0
117 4 0
120 2 1
124 2 0
;
run;
data scatter;
do x3=0.5 ,2.5;
do y3=0.5 to 4.5 by 0.5;
output;
end;
end;
run;
proc transpose data= have out=canplot prefix=y;
by subject;
run;
proc sql;
create table want as
select *,ifn(_NAME_='Baseline',1,2) as g from canplot
outer union
select 1 as x2,median(Baseline) as y2 from have /*Baseline*/
outer union corr
select 2 as x2,median(month1) as y2 from have /*month1*/
outer union
select * from scatter
;quit;
title 'Change in Score';
proc sgplot data=want noautolegend noborder aspect=0.5;
styleattrs datacontrastcolors=(orange black) datalinepatterns=(solid dash);
series x=g y=y1 / group=subject lineattrs=(color=orange thickness=8 pattern=solid) transparency=0.5;
series x=x2 y=y2/lineattrs=(color=black thickness=8 pattern=dash);
scatter x=x3 y=y3;
xaxis type=linear offsetmin=0.01 values=(0.5 1 2 2.5) valuesdisplay=(' ' 'Baseline' '1 month' ' ') display=(noline noticks);
yaxis values=(0 to 4.5 by 0.5) grid display=(noline noticks) valuesformat=best.;
label x3='Time-Point';
label y3='Score';
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.