Hello,
My data set includes six months' of data for several different healthcare facilities. I don't need to do any calculations, they've already been performed. I'm using sgplot to create a bar line graph with my bars being individual facility data and the line being the total average data for all facilities. I'm creating a total of 4 graphs for each facility, one for each of the measures I'm looking at. How can I get the data to go through the sgplot so that each facility has all 4 graphs on a page together. I'm able to get 4 graphs on a page, but they're for 4 different facilities, the same data graph, i.e., one page with have 4 graphs for Length of Stay for 4 different facilities, rather than a graph of the 4 different measures for all the same facility. I imagine I need to use a macro, but I'm stuck, as macros are a weakness for me. I'm using SAS 9.4. I've attached a dummy data set and this is the code I'm using:
options nodate nonumber orientation=landscape;
goptions reset=all device=sasprtc htitle=13pt ftext="Helvetica/bold";
ods _all_ close;
ods pdf file='data.pdf' notoc dpi=300 startpage=no;
ods graphics / reset=all height=1.7in width=8in;
proc sgplot data=snf_complete;
title "Average Paid per Day";
vbar Date /response = Average_Paid_Per_Day;
vline date/response = Total_Avg_Paid_Per_Day y2axis;
by Facility_Name;
run;
ods graphics / reset=all height=1.7in width=8in;
proc sgplot data=snf_complete;
title "Average Paid per Discharge";
vbar Date / response = Average_Paid_Per_Discharge;
vline date / response = Total_Avg_Paid_Per_Discharge y2axis;
by Facility_Name;
run;
ods graphics / reset=all height=1.7in width=8in;
proc sgplot data=snf_complete;
title "Average Length of Stay";
vbar Date / response = Average_Length_of_Stay;
vline Date / response = Total_Avg_LOS y2axis;
by Facility_Name;
run;
ods graphics / reset=all height=1.7in width=8in;
proc sgplot data=snf_complete;
title "Readmission Rates";
vbar Date / response = Readmission_Rate;
vline Date / response = Total_Readmission_Rates y2axis;
by CCN_Facility_Name;
run;
ods _all_ close;
ods listing;
The attached code will create a page for each facility by looping over a list of the names extracted by proc sql. I would even go further and create a single sgplot inside a macro that would create all 4 graphs
%macro graphit;
ods graphics / reset=all height=1.7in width=8in;
proc sgplot data=snf_complete;
title "Average Paid per Day";
vbar Date /response = Average_Paid_Per_Day;
vline date/response = Total_Avg_Paid_Per_Day y2axis;
by Facility_Name;
run;
ods graphics / reset=all height=1.7in width=8in;
proc sgplot data=snf_complete;
title "Average Paid per Discharge";
vbar Date / response = Average_Paid_Per_Discharge;
vline date / response = Total_Avg_Paid_Per_Discharge y2axis;
by Facility_Name;
run;
ods graphics / reset=all height=1.7in width=8in;
proc sgplot data=snf_complete;
title "Average Length of Stay";
vbar Date / response = Length_of_Stay;
vline Date / response = Total_Avg_LOS y2axis;
by Facility_Name;
run;
ods graphics / reset=all height=1.7in width=8in;
proc sgplot data=snf_complete;
title "Readmission Rates";
vbar Date / response = Readmission_Rate;
vline Date / response = Total_Readmission_Rates y2axis;
by Facility_Name;
run;
%mend;
/* Make a list of facilities */
proc sql;
create table facility as
select distinct Facility_Name
from all;
quit;
options nodate nonumber orientation=landscape;
goptions reset=all device=sasprtc htitle=13pt ftext="Helvetica/bold";
ods _all_ close;
ods pdf file='~/dat/data.pdf' notoc dpi=300 startpage=no;
/* Loop over the list */
data _null_;
set facility;
call symput('fn',trim(Facility_Name));
call execute(
'data snf_complete;
set all;
where Facility_Name="&fn";
%graphit;
');
run;
ods _all_ close;
ods listing;
1. Remove the BY statement
2. Add a WHERE statement to filter for a single location.
3. Make sure the graph and file is generated as needed.
4. Convert to a macro using the following approach
@sasroper wrote:
Hello,
My data set includes six months' of data for several different healthcare facilities. I don't need to do any calculations, they've already been performed. I'm using sgplot to create a bar line graph with my bars being individual facility data and the line being the total average data for all facilities. I'm creating a total of 4 graphs for each facility, one for each of the measures I'm looking at. How can I get the data to go through the sgplot so that each facility has all 4 graphs on a page together. I'm able to get 4 graphs on a page, but they're for 4 different facilities, the same data graph, i.e., one page with have 4 graphs for Length of Stay for 4 different facilities, rather than a graph of the 4 different measures for all the same facility. I imagine I need to use a macro, but I'm stuck, as macros are a weakness for me. I'm using SAS 9.4. I've attached a dummy data set and this is the code I'm using:
options nodate nonumber orientation=landscape;goptions reset=all device=sasprtc htitle=13pt ftext="Helvetica/bold";
ods _all_ close;
ods pdf file='data.pdf' notoc dpi=300 startpage=no;
ods graphics / reset=all height=1.7in width=8in;
proc sgplot data=snf_complete;
title "Average Paid per Day";
vbar Date /response = Average_Paid_Per_Day;
vline date/response = Total_Avg_Paid_Per_Day y2axis;
by Facility_Name;
run;ods graphics / reset=all height=1.7in width=8in;
proc sgplot data=snf_complete;
title "Average Paid per Discharge";
vbar Date / response = Average_Paid_Per_Discharge;
vline date / response = Total_Avg_Paid_Per_Discharge y2axis;
by Facility_Name;
run;ods graphics / reset=all height=1.7in width=8in;
proc sgplot data=snf_complete;
title "Average Length of Stay";
vbar Date / response = Average_Length_of_Stay;
vline Date / response = Total_Avg_LOS y2axis;
by Facility_Name;
run;ods graphics / reset=all height=1.7in width=8in;
proc sgplot data=snf_complete;
title "Readmission Rates";
vbar Date / response = Readmission_Rate;
vline Date / response = Total_Readmission_Rates y2axis;
by CCN_Facility_Name;
run;
ods _all_ close;
ods listing;
The attached code will create a page for each facility by looping over a list of the names extracted by proc sql. I would even go further and create a single sgplot inside a macro that would create all 4 graphs
%macro graphit;
ods graphics / reset=all height=1.7in width=8in;
proc sgplot data=snf_complete;
title "Average Paid per Day";
vbar Date /response = Average_Paid_Per_Day;
vline date/response = Total_Avg_Paid_Per_Day y2axis;
by Facility_Name;
run;
ods graphics / reset=all height=1.7in width=8in;
proc sgplot data=snf_complete;
title "Average Paid per Discharge";
vbar Date / response = Average_Paid_Per_Discharge;
vline date / response = Total_Avg_Paid_Per_Discharge y2axis;
by Facility_Name;
run;
ods graphics / reset=all height=1.7in width=8in;
proc sgplot data=snf_complete;
title "Average Length of Stay";
vbar Date / response = Length_of_Stay;
vline Date / response = Total_Avg_LOS y2axis;
by Facility_Name;
run;
ods graphics / reset=all height=1.7in width=8in;
proc sgplot data=snf_complete;
title "Readmission Rates";
vbar Date / response = Readmission_Rate;
vline Date / response = Total_Readmission_Rates y2axis;
by Facility_Name;
run;
%mend;
/* Make a list of facilities */
proc sql;
create table facility as
select distinct Facility_Name
from all;
quit;
options nodate nonumber orientation=landscape;
goptions reset=all device=sasprtc htitle=13pt ftext="Helvetica/bold";
ods _all_ close;
ods pdf file='~/dat/data.pdf' notoc dpi=300 startpage=no;
/* Loop over the list */
data _null_;
set facility;
call symput('fn',trim(Facility_Name));
call execute(
'data snf_complete;
set all;
where Facility_Name="&fn";
%graphit;
');
run;
ods _all_ close;
ods listing;
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.