Hello,
I am interested in using SGPLOT to produce a line graph that depicts an interaction between happiness and sex on sleep regularity (with one line per sex). I used proc reg to perform the analysis. Below is code for a sample of my data (total N=600, sample n = 49) and the proc reg analysis:
/*input data*/
data FFS_Act.sample_for_mood2;
infile datalines;
input sleepreg sex_happy_inter happiness sex school_prop;
datalines;
0.99379 0.00000 -0.22054 0 0
1.31911 0.52946 0.52946 1 0.75
0.43157 0.77946 0.77946 1 0
1.85374 -1.42054 -1.42054 1 0.2
0.79650 0.00000 0.35089 0 0.4285714286
0.71688 1.11279 1.11279 1 0
0.53411 0.49375 0.49375 1 0.7142857143
0.75846 -1.22054 -1.22054 1 0.5714285714
1.47524 0.00000 0.77946 0 0.8
0.95821 0.77946 0.77946 1 0.4285714286
0.84614 0.11279 0.11279 1 0.6666666667
0.06736 0.00000 1.44613 0 0
0.68801 0.00000 0.77946 0 0
1.77189 0.00000 0.02946 0 0.5
1.32406 0.77946 0.77946 1 0.7142857143
0.21170 0.77946 0.77946 1 1
3.71327 1.20803 1.20803 1 0.4285714286
1.79483 0.00000 0.17946 0 0.5
0.77272 0.00000 -0.34554 0 0.625
1.48909 0.00000 -1.22054 0 0.6
2.17360 0.00000 -1.38721 0 0
1.31587 -0.50625 -0.50625 1 0
0.18523 0.00000 -1.22054 0 0.6666666667
1.02759 -0.22054 -0.22054 1 1
1.03145 0.00000 -1.79197 0 0.5714285714
0.01925 -1.55387 -1.55387 1 1
1.22628 0.11279 0.11279 1 0.6666666667
1.39426 0.00000 -0.22054 0 0.5
0.36690 0.00000 -0.05387 0 0
2.10064 -1.36340 -1.36340 1 0.7142857143
0.63730 0.00000 0.61279 0 0
1.06912 0.00000 0.06517 0 0.7142857143
0.21987 0.11279 0.11279 1 0.8333333333
1.46393 0.00000 -0.88721 0 0.6666666667
2.14191 -0.22054 -0.22054 1 0.6666666667
1.79833 -0.02054 -0.02054 1 0.8
0.28731 0.57946 0.57946 1 0
0.80436 1.35089 1.35089 1 0.7142857143
0.45050 -0.22054 -0.22054 1 0.8333333333
0.57373 0.00000 -0.97054 0 0
0.84570 0.00000 0.11279 0 0
0.85744 0.00000 -0.50625 0 0
1.59275 0.37946 0.37946 1 0.8
1.73977 -0.72054 -0.72054 1 0
0.13712 -0.22054 -0.22054 1 0
1.11154 0.00000 0.02946 0 0.625
2.52446 0.00000 1.17946 0 0.6
0.71264 0.00000 -0.79197 0 0.4285714286
0.84820 0.00000 -0.42054 0 0.2
;
/*analysis*/
Proc reg data=FFS_Act.sample_for_mood2 plots=none;
model sleepreg= happiness Sex sex_happy_inter school_prop / VIF clb stb;run;quit;
Below is an example of what I want the chart to look like (this was created on the full and not the sample dataset, but the idea is the same):
The only difference is that instead of the dotted lines, I would prefer 95% shaded confidence bands for each of the lines. Can anyone help?
Thanks.
Something similar to this perhaps?
proc sgplot data=have; reg x=happiness y=sleepreg/ group=sex nomarkers clm clmtransparency=.3; run;
Please don't provide libraries in data set names. I am not going to make a library to run your code. Have is a generic name for data, use your data set name.
Can't tell from your description if you wanted an interval for the Mean (the CLM) or individual observations, which would be CLI. If you want a band representing the CLI values then you would use the Reg procedure to capture the estimates for the upper and lower bounds and use a BAND plot to overlay the Reg line only plot.
ballardw, thanks for the recommendation about the library. I will remember that for future posts. No one has ever mentioned that to me before; I suppose they simply deleted the library name.
Thank you, the code worked well, with the proc format from GraphGuy. However I would like to make only the male line (sex=1) dotted, would you be able to help out? I only know how to change both lines. Thank you.
I'm not really sure on the proc reg part, but I've kludged something together to kinda show what you're wanting...
data sample_for_mood2;
infile datalines dlm='09'x;
input sleepreg sex_happy_inter happiness sex school_prop;
datalines;
0.99379 0.00000 -0.22054 0 0
1.31911 0.52946 0.52946 1 0.75
0.43157 0.77946 0.77946 1 0
1.85374 -1.42054 -1.42054 1 0.2
0.79650 0.00000 0.35089 0 0.4285714286
0.71688 1.11279 1.11279 1 0
0.53411 0.49375 0.49375 1 0.7142857143
0.75846 -1.22054 -1.22054 1 0.5714285714
1.47524 0.00000 0.77946 0 0.8
0.95821 0.77946 0.77946 1 0.4285714286
0.84614 0.11279 0.11279 1 0.6666666667
0.06736 0.00000 1.44613 0 0
0.68801 0.00000 0.77946 0 0
1.77189 0.00000 0.02946 0 0.5
1.32406 0.77946 0.77946 1 0.7142857143
0.21170 0.77946 0.77946 1 1
3.71327 1.20803 1.20803 1 0.4285714286
1.79483 0.00000 0.17946 0 0.5
0.77272 0.00000 -0.34554 0 0.625
1.48909 0.00000 -1.22054 0 0.6
2.17360 0.00000 -1.38721 0 0
1.31587 -0.50625 -0.50625 1 0
0.18523 0.00000 -1.22054 0 0.6666666667
1.02759 -0.22054 -0.22054 1 1
1.03145 0.00000 -1.79197 0 0.5714285714
0.01925 -1.55387 -1.55387 1 1
1.22628 0.11279 0.11279 1 0.6666666667
1.39426 0.00000 -0.22054 0 0.5
0.36690 0.00000 -0.05387 0 0
2.10064 -1.36340 -1.36340 1 0.7142857143
0.63730 0.00000 0.61279 0 0
1.06912 0.00000 0.06517 0 0.7142857143
0.21987 0.11279 0.11279 1 0.8333333333
1.46393 0.00000 -0.88721 0 0.6666666667
2.14191 -0.22054 -0.22054 1 0.6666666667
1.79833 -0.02054 -0.02054 1 0.8
0.28731 0.57946 0.57946 1 0
0.80436 1.35089 1.35089 1 0.7142857143
0.45050 -0.22054 -0.22054 1 0.8333333333
0.57373 0.00000 -0.97054 0 0
0.84570 0.00000 0.11279 0 0
0.85744 0.00000 -0.50625 0 0
1.59275 0.37946 0.37946 1 0.8
1.73977 -0.72054 -0.72054 1 0
0.13712 -0.22054 -0.22054 1 0
1.11154 0.00000 0.02946 0 0.625
2.52446 0.00000 1.17946 0 0.6
0.71264 0.00000 -0.79197 0 0.4285714286
0.84820 0.00000 -0.42054 0 0.2
;
proc sort data=sample_for_mood2;
by sex happiness;
run;
/*analysis*/
Proc reg data=sample_for_mood2 plots=none noprint;
model sleepreg=happiness Sex sex_happy_inter school_prop / VIF clb stb;
output out=reg_out_data p=pred ucl=upper lcl=lower;;
run;quit;
proc format;
value sexes
0=Female
1=Male
;
run;
proc sgplot data=reg_out_data;
format sex sexes.;
label happiness='Happiness (centered)';
label pred='Sleep regularity';
styleattrs datacontrastcolors=(hotpink blue);
series x=happiness y=pred / group=sex name="solid";
series x=happiness y=upper / group=sex lineattrs=(pattern=shortdash);
series x=happiness y=lower / group=sex lineattrs=(pattern=shortdash);
yaxis values=(-1 to 3 by 1);
xaxis values=(-2 to 2 by 1);
keylegend "solid" / position=top;
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!
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.