I am working on spaghetti plot using proc sgplot, just wonder if there is a way to add the data value on each data point.
You can add a label at each data point on a spaghetti plot, with the DATALABEL option after the / on the SERIES statement. Below is sample code that illustrates the syntax:
data one;
input trt_group time subject results;
datalines;
1 2 100 20
1 4 100 30
1 6 100 35
1 8 100 50
1 2 200 40
1 4 200 25
1 6 200 40
1 8 200 30
1 2 300 25
1 4 300 40
1 6 300 45
1 8 300 55
2 2 400 15
2 4 400 35
2 6 400 50
2 8 400 45
2 2 500 35
2 4 500 35
2 6 500 20
2 8 500 35
;
run;
ODS html;
proc sgplot data=one;
title 'Study Results by Treatment Group';
series x=time y=results / group=subject grouplc=trt_group name='grouping' datalabel=results;
keylegend 'grouping' / type=linecolor;
run;
Can you please show a small example of your data, and your code to produce the spaghetti plot without the labels? And then describe what data value labels you want to add. Normally a spaghetti plot will be a lot of overlapping lines (like spaghetti), so labeling values wouldn't make sense. Do you mean you want to label each strand of the spaghetti?
This blog post by Rick Wicklin might be helpful to you. It shows a spaghetti plot, and discusses how spaghetti plots differ from line plots. https://blogs.sas.com/content/iml/2016/06/02/create-spaghetti-plots-in-sas.html
You can add a label at each data point on a spaghetti plot, with the DATALABEL option after the / on the SERIES statement. Below is sample code that illustrates the syntax:
data one;
input trt_group time subject results;
datalines;
1 2 100 20
1 4 100 30
1 6 100 35
1 8 100 50
1 2 200 40
1 4 200 25
1 6 200 40
1 8 200 30
1 2 300 25
1 4 300 40
1 6 300 45
1 8 300 55
2 2 400 15
2 4 400 35
2 6 400 50
2 8 400 45
2 2 500 35
2 4 500 35
2 6 500 20
2 8 500 35
;
run;
ODS html;
proc sgplot data=one;
title 'Study Results by Treatment Group';
series x=time y=results / group=subject grouplc=trt_group name='grouping' datalabel=results;
keylegend 'grouping' / type=linecolor;
run;
Thanks very much!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.