|
Storing Graphs Generated with BY-Group Processing in Separate Files 15MAR04 When exporting a single graph to a specific file, you can name the output file using a FILENAME statement. If you produce several graphs from a single procedure using a BY statement, they will each be written to that same file. Some formats, such as CGM and GIF, can store multiple images in the same file, so with BY-group processing you could append multiple graphs to the same output file. However, some programs can only import one image from such a file. Other formats, like PNG and JPEG, were designed to store only a single image. When using these formats, each graph that is created from the BY statement will replace the one before it. For these reasons, you may want store graphs generated from BY-group processing in separate files. In SAS 7 and above, you can write the graphs to separate files by specifying an aggregate file storage location on the FILENAME statement. The aggregate storage location is typically a directory or a partitioned data set. Each graph is automatically written to a separate file or member in this location. The names of the files are derived from the corresponding graphics catalog (GRSEG) entries. For more explicit control over the file names, you can create the output using a macro loop instead. The macro can use the value of the BY variable to subset the data and name the output file. This method is also useful in releases prior to SAS 7 where the aggregate file storage method is not available. Examples of both methods are given below. This document replaces TS-411, "How can I get graphs generated with BY-group processing stored in separate CGM files". I. Using Aggregate File Storage LocationEach time you create a graph with SAS/Graph, a GRSEG entry is created in a graphics catalog. With the aggregate file storage location method, file names are derived from these GRSEG entry names, while the file extension is determined by the device driver. The GRSEG entry name is defined automatically from the procedure name or manually by the NAME= option. The procedure name is the default method for naming the files. A. Default NamesThe following code produces ten graphs, one for each value of REGION. The GRSEG entries are named GCHART, GCHART1, GCHART2, and so on.
proc sort data=sashelp.shoes out=sorted;
The code also produces ten GIF files in the C:\ directory. The GIF files would be named according to their corresponding GRSEG entries such as GCHART.GIF, GCHART1.GIF, GCHART2.GIF, and so on. If a different procedure or different device driver were used, the file names would change accordingly. The FILENAME statement in this example works on hosts with directory-based file systems, such as Windows and UNIX. You may need to change the FILENAME statement if you are on another host. For example, on MVS you might change the FILENAME statement to:
filename grafout "userid.pdsename"
The members of this partitioned data set would be USERID.PDSENAME.GCHART, USERID.PDSENAME.GCHART1, USERID.PDSENAME.GCHART2, and so on. If the files are transferred to another system, the appropriate extension should be added at that time. B. The NAME= optionThe NAME= option can be used to set the GRSEG entry name, and likewise the file name of the exported file. For example, if the VBAR statement from the above example were modified as follows: vbar product / sumvar=inventory name='Region'; Now the output graphs are named REGION.GIF, REGION1.GIF, REGION2.GIF, and so on. The NAME= option is limited to eight characters or less. It cannot use the #BYVAL parameter to automatically name the graphs according to the value of the BY variable. C. Restart numbering to replace filesYou may have noticed that the catalog entry names are incremented for each graph generated. Because the GRSEG entry names change, the exported files are not replaced each time the procedure executes. For example, if you run the example in I.A above to produce GCHART.GIF through GCHART9.GIF, and then rerun the code in the same SAS session without deleting the first set of graphs, the next set of graphs will be GCHART10.GIF to GCHART19.GIF. To have the same names used each time the program is executed in a given SAS session, you can delete the previous catalog entries using the GREPLAY procedure. You can delete all the entries in the catalog, or delete specific entries by name or by number. Place the GREPLAY code at the beginning of your program to delete the previous entries before creating the new ones. By default, graphs are written to the WORK.GSEG catalog. 1. Deleting all entries in the catalog proc greplay igout=gseg nofs;
This code deletes all the graphs in the default catalog WORK.GSEG. 2. Deleting entries by name proc greplay igout=gseg nofs;
This code deletes the graphs named GCHART, GCHART1, and GCHART2 from the default the catalog WORK.GSEG. 3. Deleting entries by number proc greplay igout=gseg nofs;
This code deletes the first three entries in the default catalog WORK.GSEG. You can also specify a range of values: <>proc greplay igout=gseg nofs;delete 1 to 5; run;quit; This code deletes the first five entries, GCHART, GCHART1, GCHART2, GCHART3, and GCHART4. II. Using a MacroYou may want to use a macro to write multiple graphs generated by the same procedure to separate files if: you are running a release prior to SAS 7; you want to have file names longer than eight characters; or if you want the file name to contain the BY value. The basic steps are as follows:
Below are two examples of naming the output files using the BY-value. The first example uses one BY-value; the second example is modified to produce file names from two BY-values. A. Creating file names from one BY-valueThis example produces a separate graph for each REGION from the data set SASHELP.SHOES. The files are stored in the GIF format and named for the REGION they represent. Each numbered line is described below. proc sort data=sashelp.shoes out=sortreg;
The code does the following:
Notice with this method the extension is not appended automatically to the file name; if you change the DEVICE, you may need to change the extension to match. On the FILENAME, three dots are used between the name and extension: "&&value&i...gif". While its resolving, the macro variable absorbs the first two. After the macro variable is resolved, the file names still contain the ".gif" extension. The FILENAME statement in this example works on hosts with directory-based file systems, such as Windows and UNIX. You may need to tailor this statement for your host. For example, for MVS you might change the FILENAME statement to: filename grafout "userid.&&value&i...gif"
This example would create the following files: Africa.gif B. Creating file names from two BY-valuesThe logic for creating the FILENAME from two BY-values is very similar. The data is sorted by both BY-values, and different macro variables are created for each BY-value for the WHERE and FILENAME statements. For each unique combination of BY-values, a different set of macro variables is created. Some of these macro variables will resolve to the same value, but this method makes the macro processing easier: only one macro loop is needed. Each numbered line is described below. data subset;
The code does the following:
The graphs produced from this example are: Boot_Eastern Europe.gif Notice that the PRODUCT values may contain the single quote character " ' ". If your operating system cannot use this character as part of a file name, you can change the character using code similar to that given in Example II.A: if index(product,"'") GT 0 then substr(product,index(product,"'"),1)='_';
Or you can simply remove it: product=compress(product,"'");
With this data, each PRODUCT included each of the REGIONS; that is, all values of the second BY-variable were represented in each of the first BY-variables. However, this code would also work if the values of the second BY-variable were different for each of the first BY-variable values. For example, if the BY variables were REGION and SUBSIDIARY, there are twelve unique combinations of these variables in the SUBSET data set:
Although the values of SUBSIDIARY are unique within each REGION, the example can produce the correct graphs for this subset as well. III. Reference SAS OnlineDoc, SAS/GRAPH Software: Reference, "Exporting SAS/GRAPH Output", "Creating External Files with SAS/GRAPH Program Statements" |