BookmarkSubscribeRSS Feed
rwe_stat
Calcite | Level 5

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).

Screenshot 2024-08-28 163300.png

 Thank you!

2 REPLIES 2
ballardw
Super User

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 .

 

 

Ksharp
Super User
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;

Ksharp_0-1725503035607.png

 

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

Register now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 735 views
  • 0 likes
  • 3 in conversation