BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
GabbyJ
Fluorite | Level 6

Hi,

Could you kindly help me?

I would like to add confidence intervals to a graph following proc GLM with data from multiple timepoints.

I calculated the means in each group, but not CIs. Could you teach me how to calculate and graph the CIs?

Or would it be easier to use a mixed model?

 

Data, code, and sample graph are below. 

Thank you so much 🙂

 

proc glm data=exercise;
  class exertype;
  model time1 time2 time3 = exertype;
  repeated time 3 ;
  lsmeans exertype / out=means;
run;
quit;
proc print data=means;
run;

proc sgplot data=means;
	series x=_name_ y=lsmean / group=exertype;
run;
quit;
GabbyJ_0-1595019319897.png

 

 

 

sample data:

data exercise;
  input id exertype diet time1 time2 time3;
cards;
1     1         1       85       85       88
2     1         1       90       92       93
3     1         1       97       97       94
4     1         1       80       82       83
5     1         1       91       92       91
6     1         2       83       83       84
7     1         2       87       88       90
8     1         2       92       94       95
9     1         2       97       99       96
10    1         2      100       97      100
11    2         1       86       86       84
12    2         1       93      103      104
13    2         1       90       92       93
14    2         1       95       96      100
15    2         1       89       96       95
16    2         2       84       86       89
17    2         2      103      109       90
18    2         2       92       96      101
19    2         2       97       98      100
20    2         2      102      104      103
21    3         1       93       98      110
22    3         1       98      104      112
23    3         1       98      105       99
24    3         1       87      132      120
25    3         1       94      110      116
26    3         2       95      126      143
27    3         2      100      126      140
28    3         2      103      124      140
29    3         2       94      135      130
30    3         2       99      111      150
;
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
GabbyJ
Fluorite | Level 6

I just figured it out, thank you Reeza for your kind help 🙂 I added the group function in the band command.

 

 

proc sgplot data=means;
	series x=_name_ y=lsmean / group=exertype ;
    band x = _name_ upper=upperci lower=lowerci  / fillattrs=(color=graydd) group=exertype transparency=.80;
run;
quit;

 

 

View solution in original post

4 REPLIES 4
Reeza
Super User

Start by adding the CL option to the LSMEANS statement and that will give you the confidence limits to plot. 

https://documentation.sas.com/?docsetId=statug&docsetTarget=statug_glm_syntax10.htm&docsetVersion=15...

proc glm data=exercise;
  class exertype;
  model time1 time2 time3 = exertype;
  repeated time 3 ;
  lsmeans exertype / out=means CL;
run;

 

Then you can add them with a BAND statement. I don't know what the exact variables are that come out of the LSMEANS statement so you'll need to modify the code to take the correct names.

proc sgplot data=means;
	series x=_name_ y=lsmean / group=exertype;
        band x = _name_ upper=upper_limit lower=lower_limit  / fillattrs=(color=graydd);
run;
quit;

 

GabbyJ
Fluorite | Level 6

Dear Reeza,

Thank you so much for your reply 🙂

The standard error comes out from the CL option in lsmeans, and I converted it to upper and lower CI's. However, when I graph the upper and lower CI's using band, something doesn't quite work. Could you kindly help? Thank you again! I very much appreciate your help 🙂 Code, data, and graph are below.

 

 

proc glm data=exercise;
  class exertype;
  model time1 time2 time3 = exertype;
  repeated time 3 ;
  lsmeans exertype / out=means CL pdiff;
run;
quit;

data means;
set means; 
upperci = lsmean + (1.96*STDERR);
lowerci = lsmean - (1.96*STDERR);
run;

proc print data=means;
run;

proc sgplot data=means;
	series x=_name_ y=lsmean / group=exertype;
    band x = _name_ upper=upperci lower=lowerci  / fillattrs=(color=graydd);
run;
quit;

Sample of "mean" data:

 Obs _NAME_ exertype LSMEAN STDERR upperci lowerci123456789

time1190.21.8493293.82586.575
time1293.11.8493296.72589.475
time1396.11.8493299.72592.475
time2190.92.9528496.68885.112
time2296.62.95284102.38890.812
time23117.12.95284122.888111.312
time3191.43.4722798.20684.594
time3295.93.47227102.70689.094
time33126.03.47227132.806119.194

 

GabbyJ_0-1595112377048.png

 

 
 

 

 

 

GabbyJ
Fluorite | Level 6

Dear Reeza,

Thank you so much for your help and reply! The CI option output the standard error (which I converted to CI's) and tried to add them to the graph with the band function. The graph is not looking quite right. Could you kindly advise? Thank you again! 🙂

proc glm data=exercise;
  class exertype;
  model time1 time2 time3 = exertype;
  repeated time 3 ;
  lsmeans exertype / out=means CL;
run;
quit;

data means;
set means; 
upperci = lsmean + (1.96*STDERR);
lowerci = lsmean - (1.96*STDERR);
run;

proc print data=means;
run;

proc sgplot data=means;
	series x=_name_ y=lsmean / group=exertype;
    band x = _name_ upper=upperci lower=lowerci  / fillattrs=(color=graydd);
run;
quit;

 Obs _NAME_ exertype LSMEAN STDERR upperci lowerci123456789

time1190.21.8493293.82586.575
time1293.11.8493296.72589.475
time1396.11.8493299.72592.475
time2190.92.9528496.68885.112
time2296.62.95284102.38890.812
time23117.12.95284122.888111.312
time3191.43.4722798.20684.594
time3295.93.47227102.70689.094
time33126.03.47227132.806119.194

 
 

for_sas_test.png



 

 

 

 
 

 

 

GabbyJ
Fluorite | Level 6

I just figured it out, thank you Reeza for your kind help 🙂 I added the group function in the band command.

 

 

proc sgplot data=means;
	series x=_name_ y=lsmean / group=exertype ;
    band x = _name_ upper=upperci lower=lowerci  / fillattrs=(color=graydd) group=exertype transparency=.80;
run;
quit;

 

 

sas-innovate-white.png

🚨 Early Bird Rate Extended!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Lock in the best rate now before the price increases on April 1.

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 2349 views
  • 3 likes
  • 2 in conversation