Hello Everyone,
I am trying to convert my below code into a macro where it generates multiple png images. I have a question on how to get the png formats?
options nodate nonumber;
ods escapechar='^';
ods listing close;
ods graphics on;
ods output survivalplot = sj.survplot;
proc lifetest data = sj.adsl3 method = km plots=s(test)
timelist = 0 1 2 5 10 15 20 25 30 conftype = linear;
time years * death(0);
strata ses;
run;
ods tagsets.rtf file = "/home/Projects/programs/exhibit_01.rtf";
ods graphics / reset height = 6in width = 8in noborder;
title1 j=c h=12pt font = arial "Overall Survival by SES";
proc sgplot data = sj.survplot;
step x=time y=survival /group=stratum name='s' lineattrs=(pattern=solid);
xaxis valueattrs=(size=11.2pt) labelattrs=(size=12.5pt) label="Years" values=(0 to 5 by 1);
yaxis valueattrs=(size=11.2pt) labelattrs=(size=12.5pt) label="Survival Probability" values=(0.5 to 1 by 0.1);
scatter x=time y=censored / markerattrs=(symbol=plus) name='c';
scatter x=time y=censored / markerattrs=(symbol=plus) group=stratum;
keylegend 'c'/location=inside position=topright;
keylegend 's'/location=inside position=bottomleft;
run;
ods tagsets.rtf close;
ods listing;
As you see it is currently generating just one rtf file now. I have to generate multiple png files.
Thanks!
Tutorial on converting a working program to a macro
This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it 🙂 https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md
%macro create_graphs(input_data = ,
output_data= ,
output_file=,
variable = );
options nodate nonumber;
ods escapechar='^';
ods listing close;
ods graphics on;
ods output survivalplot = sj.&outputfile;
proc lifetest data = sj.&input_data method = km plots=s(test)
timelist = 0 1 2 5 10 15 20 25 30 conftype = linear;
time years * death(0);
strata ses &variable;
run;
ods tagsets.rtf file = "/home/Projects/programs/&output_file..rtf";
ods graphics / reset height = 6in width = 8in noborder;
title1 j=c h=12pt font = arial "Overall Survival by SES";
proc sgplot data = sj.&output_data.;
step x=time y=survival /group=stratum name='s' lineattrs=(pattern=solid);
xaxis valueattrs=(size=11.2pt) labelattrs=(size=12.5pt) label="Years" values=(0 to 5 by 1);
yaxis valueattrs=(size=11.2pt) labelattrs=(size=12.5pt) label="Survival Probability" values=(0.5 to 1 by 0.1);
scatter x=time y=censored / markerattrs=(symbol=plus) name='c';
scatter x=time y=censored / markerattrs=(symbol=plus) group=stratum;
keylegend 'c'/location=inside position=topright;
keylegend 's'/location=inside position=bottomleft;
run;
ods tagsets.rtf close;
ods listing;
%mend;
%create_graphs(input_data = adsl3 , output_data= survplot1, output_file=exhibit_01, variable=ses);
%create_graphs(input_data = adsl4 , output_data= survplot2, output_file=exhibit_02, variable=race_ses);
Probably not everything but enough to get started and give you the idea. Please follow the tutorial linked above to see explanations and details.
This may not work because I have no data to test it on, if it doesn't and you cannot debug it yourself please post your code, log with macro debugging options on (MPRINT, SYMBOLGEN) and explain what is wrong.
Use the OUTPUTFMT= option of the ODS GRAPHICS statement.
RTF embeds the images; if you want separate image files, use ODS HTML or similar.
What is the rule for "multiple graphs"? How do you identify which graph goes where?
The only thing I can guess might be desirable for individual graphs would be the Stratum variable but you are using that in a group role.
If you use a BY STRATUM; (assumes the data set is sorted by Stratum) you will get one graph for each level of Stratum which would create separate PNG files if the proper ODS Graphics options are set. You would likely want to remove Stratum from the group role where used.
Hello @Reeza ,
Thanks for reaching out. Here are the iteration of graphs and their corresponding outputs. I have highlighted the code where ever I have changed it while producing the graphs.
options nodate nonumber; ods escapechar='^'; ods listing close; ods graphics on; ods output survivalplot =
sj.survplot1; proc lifetest data = sj.adsl3 method = km plots=s(test) timelist = 0 1 2 5 10 15 20 25 30 conftype = linear; time years * death(0); strata ses; run; ods tagsets.rtf file = "/home/Projects/programs/exhibit_01.rtf"; ods graphics / reset height = 6in width = 8in noborder; title1 j=c h=12pt font = arial "Overall Survival by SES"; proc sgplot data = sj.survplot1; step x=time y=survival /group=stratum name='s' lineattrs=(pattern=solid); xaxis valueattrs=(size=11.2pt) labelattrs=(size=12.5pt) label="Years" values=(0 to 5 by 1); yaxis valueattrs=(size=11.2pt) labelattrs=(size=12.5pt) label="Survival Probability" values=(0.5 to 1 by 0.1); scatter x=time y=censored / markerattrs=(symbol=plus) name='c'; scatter x=time y=censored / markerattrs=(symbol=plus) group=stratum; keylegend 'c'/location=inside position=topright; keylegend 's'/location=inside position=bottomleft; run; ods tagsets.rtf close; ods listing; ods listing close; ods graphics on; ods output survivalplot = sj.survplot2; proc lifetest data = sj.adsl3 method = km plots=s(test) timelist = 0 1 2 5 10 15 20 25 30 conftype = linear; time years * death(0); strata race_ses; run; ods tagsets.rtf file = "/home/Projects/programs/exhibit_02.rtf"; ods graphics / reset height = 7.5in width = 9in noborder; title1 j=c h=12pt font = arial "Survival by Race and SES"; proc sgplot data = sj.survplot2; step x=time y=survival /group=stratum name='s' lineattrs=(pattern=solid); xaxis valueattrs=(size=11.2pt) labelattrs=(size=12.5pt) label="Years" values=(0 to 5 by 1); yaxis valueattrs=(size=11.2pt) labelattrs=(size=12.5pt) label="Survival Probability" values=(0.3 to 1 by 0.1); scatter x=time y=censored / markerattrs=(symbol=plus) name='c'; scatter x=time y=censored / markerattrs=(symbol=plus) group=stratum; keylegend 'c'/location=inside position=topright; keylegend 's'/location=outside; run; ods tagsets.rtf close; ods listing; ods listing close; ods graphics on; ods output survivalplot = sj.survplot3; proc lifetest data = sj.adsl4 method = km plots=s(test) timelist = 0 1 2 5 10 15 20 25 30 conftype = linear; time years * death(0); strata race_ses_min; run; ods tagsets.rtf file = "/home/Projects/programs/exhibit_03.rtf"; ods graphics / reset height = 6in width = 8in noborder; title1 j=c h=12pt font = arial "Survival by Minority Race and SES"; proc sgplot data = sj.survplot3; step x=time y=survival /group=stratum name='s' lineattrs=(pattern=solid); xaxis valueattrs=(size=11.2pt) labelattrs=(size=12.5pt) label="Years" values=(0 to 5 by 1); yaxis valueattrs=(size=11.2pt) labelattrs=(size=12.5pt) label="Survival Probability" values=(0.3 to 1 by 0.1); scatter x=time y=censored / markerattrs=(symbol=plus) name='c'; scatter x=time y=censored / markerattrs=(symbol=plus) group=stratum; keylegend 'c'/location=inside position=topright; keylegend 's'/location=inside position=bottomleft; run; ods tagsets.rtf close; ods listing;
Also some of the other output formats are png images.
Thanks!
I have highlighted other elements that are changing across the code. Not sure why they are not appearing here. Some of them include in and out datasets like survplot1,2,3, height, title, strata variables, keylegend location, values for Y-AXIS etc.,
Tutorial on converting a working program to a macro
This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it 🙂 https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md
%macro create_graphs(input_data = ,
output_data= ,
output_file=,
variable = );
options nodate nonumber;
ods escapechar='^';
ods listing close;
ods graphics on;
ods output survivalplot = sj.&outputfile;
proc lifetest data = sj.&input_data method = km plots=s(test)
timelist = 0 1 2 5 10 15 20 25 30 conftype = linear;
time years * death(0);
strata ses &variable;
run;
ods tagsets.rtf file = "/home/Projects/programs/&output_file..rtf";
ods graphics / reset height = 6in width = 8in noborder;
title1 j=c h=12pt font = arial "Overall Survival by SES";
proc sgplot data = sj.&output_data.;
step x=time y=survival /group=stratum name='s' lineattrs=(pattern=solid);
xaxis valueattrs=(size=11.2pt) labelattrs=(size=12.5pt) label="Years" values=(0 to 5 by 1);
yaxis valueattrs=(size=11.2pt) labelattrs=(size=12.5pt) label="Survival Probability" values=(0.5 to 1 by 0.1);
scatter x=time y=censored / markerattrs=(symbol=plus) name='c';
scatter x=time y=censored / markerattrs=(symbol=plus) group=stratum;
keylegend 'c'/location=inside position=topright;
keylegend 's'/location=inside position=bottomleft;
run;
ods tagsets.rtf close;
ods listing;
%mend;
%create_graphs(input_data = adsl3 , output_data= survplot1, output_file=exhibit_01, variable=ses);
%create_graphs(input_data = adsl4 , output_data= survplot2, output_file=exhibit_02, variable=race_ses);
Probably not everything but enough to get started and give you the idea. Please follow the tutorial linked above to see explanations and details.
This may not work because I have no data to test it on, if it doesn't and you cannot debug it yourself please post your code, log with macro debugging options on (MPRINT, SYMBOLGEN) and explain what is wrong.
@RAVI2000 wrote:
I have different group data to be analyzed. I'm afraid to take it out.
So it appears, without any rule of how to create multiple graphs, that creating them actually isn't needed.
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.