Hi,
I am trying to get ODS PDF to loop through three SGPLOT procedures for each BY group, which comes from a list in a separate table containing the subset of BY groups I want to use.
The below example uses SASHELP.CARS, but the applications could be employee scorecards, business unit reports, etc. The first data step creates a table of BY groups (selectedmakes) to be used in the following SGPLOTs. The goal would be to have six total pages with three plots each (one for BMW, one for Chevrolet, etc.). If the answer is to "just make a macro do loop," please explain the method or provide a solution using the example code. I have tried to study how to do this but have had trouble understanding to this point.
data selectedmakes;
format selectedmake $20.;
input index selectedmake $;
cards;
1 BMW
2 Chevrolet
3 Ford
4 Jaguar
5 Mercury
6 Pontiac
;
run;
options printerpath=PDF;
goptions device=SASPRTC;
ods graphics on / reset=all;
ods pdf gtitle startpage=no notoc dpi=300;
ods graphics / height=3in width=6in;
options nobyline;
proc sgplot data=sashelp.cars;
title "Highway MPG by Type";
title2 "Make: #byval1";
vbox mpg_highway / category = type;
yaxis values = (0 to 40 by 5) label = "Highway MPG";
by make;
run;
proc sgplot data=sashelp.cars;
title "Invoice vs. MSRP";
title2 "Make: #byval1";
scatter x = invoice y = msrp;
xaxis values = (0 to 100000 by 20000) label = "Invoice";
yaxis values = (0 to 100000 by 20000) label = "MSRP";
by make;
run;
proc sgplot data=sashelp.cars;
title "Horsepower by Engine Cylinders by Type";
title2 "Make: #byval1";
vbox horsepower / category = cylinders group = type;
yaxis values = (0 to 500 by 100) label = "Horsepower";
by make;
run;
ods pdf close;
Thanks for your help!
data selectedmakes;
format selectedmake $20.;
input index selectedmake $;
cards;
1 BMW
2 Chevrolet
3 Ford
4 Jaguar
5 Mercury
6 Pontiac
;
run;
%macro create_graphs(make=);
ods pdf startpage=now;
proc sgplot data=sashelp.cars;
title "Highway MPG by Type";
title2 "Make: &make.";
where make = "&make";
vbox mpg_highway / category = type;
yaxis values = (0 to 40 by 5) label = "Highway MPG";
run;
proc sgplot data=sashelp.cars;
title "Invoice vs. MSRP";
title2 "Make: &make.";
where make = "&make";
scatter x = invoice y = msrp;
xaxis values = (0 to 100000 by 20000) label = "Invoice";
yaxis values = (0 to 100000 by 20000) label = "MSRP";
run;
proc sgplot data=sashelp.cars;
title "Horsepower by Engine Cylinders by Type";
title2 "Make: &make.";
where make = "&make";
vbox horsepower / category = cylinders group = type;
yaxis values = (0 to 500 by 100) label = "Horsepower";
run;
%mend;
ods pdf file = '/home/fkhurshed/Demo1/singlePage.pdf' gtitle notoc dpi=300 startpage=never;
options printerpath=PDF;
goptions device=SASPRTC;
ods graphics on / reset=all;
ods graphics / height=3in width=6in;
options nobyline;
data _null_;
set selectedmakes;
str = catt('%create_graphs(make=',selectedmake, ');') ;
call execute(str);
run;
ods pdf close;
https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md
@Thummim wrote:
Hi,
I am trying to get ODS PDF to loop through three SGPLOT procedures for each BY group, which comes from a list in a separate table containing the subset of BY groups I want to use.
The below example uses SASHELP.CARS, but the applications could be employee scorecards, business unit reports, etc. The first data step creates a table of BY groups (selectedmakes) to be used in the following SGPLOTs. The goal would be to have six total pages with three plots each (one for BMW, one for Chevrolet, etc.). If the answer is to "just make a macro do loop," please explain the method or provide a solution using the example code. I have tried to study how to do this but have had trouble understanding to this point.
data selectedmakes; format selectedmake $20.; input index selectedmake $; cards; 1 BMW 2 Chevrolet 3 Ford 4 Jaguar 5 Mercury 6 Pontiac ; run; options printerpath=PDF; goptions device=SASPRTC; ods graphics on / reset=all; ods pdf gtitle startpage=no notoc dpi=300; ods graphics / height=3in width=6in; options nobyline; proc sgplot data=sashelp.cars; title "Highway MPG by Type"; title2 "Make: #byval1"; vbox mpg_highway / category = type; yaxis values = (0 to 40 by 5) label = "Highway MPG"; by make; run; proc sgplot data=sashelp.cars; title "Invoice vs. MSRP"; title2 "Make: #byval1"; scatter x = invoice y = msrp; xaxis values = (0 to 100000 by 20000) label = "Invoice"; yaxis values = (0 to 100000 by 20000) label = "MSRP"; by make; run; proc sgplot data=sashelp.cars; title "Horsepower by Engine Cylinders by Type"; title2 "Make: #byval1"; vbox horsepower / category = cylinders group = type; yaxis values = (0 to 500 by 100) label = "Horsepower"; by make; run; ods pdf close;
Thanks for your help!
data selectedmakes;
format selectedmake $20.;
input index selectedmake $;
cards;
1 BMW
2 Chevrolet
3 Ford
4 Jaguar
5 Mercury
6 Pontiac
;
run;
%macro create_graphs(make=);
ods pdf startpage=now;
proc sgplot data=sashelp.cars;
title "Highway MPG by Type";
title2 "Make: &make.";
where make = "&make";
vbox mpg_highway / category = type;
yaxis values = (0 to 40 by 5) label = "Highway MPG";
run;
proc sgplot data=sashelp.cars;
title "Invoice vs. MSRP";
title2 "Make: &make.";
where make = "&make";
scatter x = invoice y = msrp;
xaxis values = (0 to 100000 by 20000) label = "Invoice";
yaxis values = (0 to 100000 by 20000) label = "MSRP";
run;
proc sgplot data=sashelp.cars;
title "Horsepower by Engine Cylinders by Type";
title2 "Make: &make.";
where make = "&make";
vbox horsepower / category = cylinders group = type;
yaxis values = (0 to 500 by 100) label = "Horsepower";
run;
%mend;
ods pdf file = '/home/fkhurshed/Demo1/singlePage.pdf' gtitle notoc dpi=300 startpage=never;
options printerpath=PDF;
goptions device=SASPRTC;
ods graphics on / reset=all;
ods graphics / height=3in width=6in;
options nobyline;
data _null_;
set selectedmakes;
str = catt('%create_graphs(make=',selectedmake, ');') ;
call execute(str);
run;
ods pdf close;
https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md
@Thummim wrote:
Hi,
I am trying to get ODS PDF to loop through three SGPLOT procedures for each BY group, which comes from a list in a separate table containing the subset of BY groups I want to use.
The below example uses SASHELP.CARS, but the applications could be employee scorecards, business unit reports, etc. The first data step creates a table of BY groups (selectedmakes) to be used in the following SGPLOTs. The goal would be to have six total pages with three plots each (one for BMW, one for Chevrolet, etc.). If the answer is to "just make a macro do loop," please explain the method or provide a solution using the example code. I have tried to study how to do this but have had trouble understanding to this point.
data selectedmakes; format selectedmake $20.; input index selectedmake $; cards; 1 BMW 2 Chevrolet 3 Ford 4 Jaguar 5 Mercury 6 Pontiac ; run; options printerpath=PDF; goptions device=SASPRTC; ods graphics on / reset=all; ods pdf gtitle startpage=no notoc dpi=300; ods graphics / height=3in width=6in; options nobyline; proc sgplot data=sashelp.cars; title "Highway MPG by Type"; title2 "Make: #byval1"; vbox mpg_highway / category = type; yaxis values = (0 to 40 by 5) label = "Highway MPG"; by make; run; proc sgplot data=sashelp.cars; title "Invoice vs. MSRP"; title2 "Make: #byval1"; scatter x = invoice y = msrp; xaxis values = (0 to 100000 by 20000) label = "Invoice"; yaxis values = (0 to 100000 by 20000) label = "MSRP"; by make; run; proc sgplot data=sashelp.cars; title "Horsepower by Engine Cylinders by Type"; title2 "Make: #byval1"; vbox horsepower / category = cylinders group = type; yaxis values = (0 to 500 by 100) label = "Horsepower"; by make; run; ods pdf close;
Thanks for your help!
Thank you for the solution and helpful article!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.