I need to add a second x-axis on a plot using PROC SGPLOT in Sas. It is a time series with vline (because it is mean values with error bars). My dataset has the following columns:
ID (subjects)
treatment
time_elapsed (num)
clocktime (num)
Response.
Here is my code:
proc sgplot data=mydata ;
vline time_elapsed / response=Response stat=mean limitstat=stderr numstd=2
/ group=condition ;
xaxis label="Time elapsed [hours]" values=(0 to 9 by 1);
This gives my elapsed time on the x-axis. I would like to have another x-axis, maybe on top of the plot, with "clock time" - that is on the same scale (hours). I can not do so:
proc sgplot data=mydata ;
vline time_elapsed / response=Response stat=mean limitstat=stderr numstd=2
/ group=condition ;
vline clocktime / response=Response stat=mean limitstat=stderr numstd=2
/ group=condition x2axis;
Because the category variable should be the same. And I must use "vline" to be able to represent mean values and error bars. Any other idea?
Thank you!
Your best option would be to use PROC MEANS or PROC SUMMARY to calculate the numbers for each overlay, merge the results into one data set, and use SERIES and SCATTER plots (with error bars) to display the result. This following code is uses this technique with some sample data. Just substitute your data and variables. Let me know if you have any questions about the details.
Thanks!
Dan
proc summary data=sashelp.heart nway;
class weight_status;
var cholesterol;
output out=weight mean=chol_wgt stderr=se_wgt;
run;
proc summary data=sashelp.heart nway;
class chol_status;
var cholesterol;
output out=chol mean=chol_chol stderr=se_chol;
run;
data merged;
set weight chol;
wgt_upper = chol_wgt + se_wgt;
wgt_lower = chol_wgt - se_wgt;
chol_upper = chol_chol + se_chol;
chol_lower = chol_chol - se_chol;
run;
proc sgplot data=merged cycleattrs;
series x=weight_status y=chol_wgt / name="weight" lineattrs=graphdata1;
scatter x=weight_status y=chol_wgt / markerattrs=graphdata1(size=0)
yerrorupper=wgt_upper yerrorlower=wgt_lower;
series x=chol_status y=chol_chol / x2axis name="chol" lineattrs=graphdata2;
scatter x=chol_status y=chol_chol/ markerattrs=graphdata2(size=0) x2axis
yerrorupper=chol_upper yerrorlower=chol_lower;
keylegend "weight" "chol";
run;
Your best option would be to use PROC MEANS or PROC SUMMARY to calculate the numbers for each overlay, merge the results into one data set, and use SERIES and SCATTER plots (with error bars) to display the result. This following code is uses this technique with some sample data. Just substitute your data and variables. Let me know if you have any questions about the details.
Thanks!
Dan
proc summary data=sashelp.heart nway;
class weight_status;
var cholesterol;
output out=weight mean=chol_wgt stderr=se_wgt;
run;
proc summary data=sashelp.heart nway;
class chol_status;
var cholesterol;
output out=chol mean=chol_chol stderr=se_chol;
run;
data merged;
set weight chol;
wgt_upper = chol_wgt + se_wgt;
wgt_lower = chol_wgt - se_wgt;
chol_upper = chol_chol + se_chol;
chol_lower = chol_chol - se_chol;
run;
proc sgplot data=merged cycleattrs;
series x=weight_status y=chol_wgt / name="weight" lineattrs=graphdata1;
scatter x=weight_status y=chol_wgt / markerattrs=graphdata1(size=0)
yerrorupper=wgt_upper yerrorlower=wgt_lower;
series x=chol_status y=chol_chol / x2axis name="chol" lineattrs=graphdata2;
scatter x=chol_status y=chol_chol/ markerattrs=graphdata2(size=0) x2axis
yerrorupper=chol_upper yerrorlower=chol_lower;
keylegend "weight" "chol";
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.