BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
RAVI2000
Lapis Lazuli | Level 10

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! 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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.

View solution in original post

15 REPLIES 15
RAVI2000
Lapis Lazuli | Level 10
If I convert above code into a macro call, will that generate multiple png files?
ballardw
Super User

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.

RAVI2000
Lapis Lazuli | Level 10
I have different group data to be analyzed. I'm afraid to take it out.
Reeza
Super User
1. Does the code posted above do what you want? Before you automate anything it should work as you need. If not, solve that issue first.
2. Once you have 1, show how it would work for two or three iterations if you copied/pasted it. Then post back and we can help you convert it to a macro.
RAVI2000
Lapis Lazuli | Level 10

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.

 

S_RAVI_0-1635959461665.png

 

S_RAVI_1-1635959486601.png

 

S_RAVI_2-1635959550395.png

 

Thanks!

RAVI2000
Lapis Lazuli | Level 10

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.,

RAVI2000
Lapis Lazuli | Level 10
Hello @Reeza,

Can you help by taking a look into those codes and output and how can I convert them into macros?
Reeza
Super User

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
Lapis Lazuli | Level 10
This was a very helpful tutorial who are struggling with macros. I have learnt some new things from you @Reeza. Thank you so much!
Reeza
Super User
You should also check out this macro. It's old so haven't tested it lately but there's example testing code at the bottom.
https://gist.github.com/statgeek/d3bce2a9e2ef0523db9d
ballardw
Super User

@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.

SAS Innovate 2025: Register Now

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!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 15 replies
  • 2201 views
  • 3 likes
  • 4 in conversation