Sample 37703: Dynamically create multiple PNG files where the PNG filenames are created using the values of the BY variable
The sample code in the
Full Code tab demonstrates how to use macro code to write multiple PNG files to disk, dynamically changing the PNG filename for each PNG file that is written to disk. The values of the BY variable are used when creating the filenames of the individual PNG files.
First, PROC GCHART code is placed inside of a macro. Instead of using a BY statement, the PROC GCHART code now uses a WHERE statement. Then, at the end of the code, a CALL EXECUTE statement is used within DATA step code to call the macro once for each unique value of the BY variable.
Note that this technique is very flexible and can be used in almost any situation where you want to create individual output files or graphs where the output filenames are based on the values of one or more BY variables.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
This sample code uses PROC GCHART code inside of a macro. Instead of using a BY statement with PROC GCHART, the code uses a WHERE statement. Then, at the end of the code, a CALL EXECUTE statement is used to call the macro one time (for each unique value of the BY variable). This allows the code to dynamically change the PNG filename for each PNG file written to disk, using the value of the BY variable as the PNG filename.
/* Macro debugging options */
options mprint mlogic symbolgen;
data widgets;
input @1 Region $10. Type $ Sales;
cards;
Southeast Gadgets 5250
Southeast Gizmos 4200
Southeast Widgets 4930
Northeast Gadgets 2700
Northeast Gizmos 4100
Northeast Widgets 3800
Southwest Gadgets 6300
Southwest Gizmos 5900
Southwest Widgets 5200
Northwest Gadgets 4600
Northwest Gizmos 2700
Northwest Widgets 5500
;
run;
proc sort data=widgets;
by region;
run;
%macro GRAPHIT(MREGION);
filename gsasfile "C:\temp\&MREGION..png";
goptions reset = all
device = PNG
gsfname = gsasfile
gsfmode = replace
htext = 10 pt
htitle = 12 pt;
title "Graph for &MREGION Region";
footnote1 j=r "Release is &Sysvlong";
proc gchart data=widgets;
vbar type / sumvar = sales
subgroup = type
width = 12
space = 6
raxis = axis1;
where region="&MREGION";
axis1 label=("Sales");
format sales dollar10.;
pattern1 V=S c=vibg;
pattern2 v=s c=dabg;
pattern3 v=s c=mob;
pattern4 v=s c=day;
run;
quit;
%mend;
data _null_;
set widgets;
by region;
if first.region
then call execute('%GRAPHIT(' || trim(region) || ')');
run;
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
Dynamically create multiple PNG files where the PNG filenames are created using the values of the BY variable.
Date Modified: | 2023-04-20 12:49:54 |
Date Created: | 2009-11-03 13:54:59 |
Operating System and Release Information
SAS System | SAS/GRAPH | 64-bit Enabled AIX | 9.2 TS2M0 | |
64-bit Enabled HP-UX | 9.2 TS2M0 | |
64-bit Enabled Solaris | 9.2 TS2M0 | |
Linux | 9.2 TS2M0 | |
OpenVMS Alpha | 9.2 TS2M0 | |
Tru64 UNIX | 9.2 TS2M0 | |
Microsoft Windows XP Professional | 9.2 TS2M0 | |
Microsoft Windows Server 2003 Standard Edition | 9.2 TS2M0 | |
Microsoft Windows Server 2003 Enterprise Edition | 9.2 TS2M0 | |
Microsoft Windows Server 2003 Datacenter Edition | 9.2 TS2M0 | |
z/OS | 9.2 TS2M0 | |