SAS SGPLOT Meaningful names for charts with by statement

207 Views Asked by At

I refer here to the the issue addressed 6 years ago in "SAS GRAPH and multiple charts, more meaningful names?" To me this issue needs a solution. I need to generate one chart by indicator (2) and country (27). The index is 1,3,5,7,9,...,107. Please try to find the file/worksheet (png/xls) for country x and indicator y. Good luck! Can somebody help me? Maybe the person who addressed the issue at that time found a solution (I really hope it)? Many thanks!

Below you have an example using sashelp library. It creates Picture1.png, Picture3.png and Picture5.png. Instead of this, I would like to have PictureIBM.png, PictureIntel.png and PictureMicrosoft.png.

proc sort data=SASHELP.STOCKS out=testfile; by stock date; run;
data testfile; set testfile;
by stock  ;
retain n;
if first.stock then n=0; else n=n+1;
if n le 5;
run;


ods listing gpath='write the path' ;
ods listing /*style = styles.new_font   */
    gpath='/ec/prod/1eusilc/flashestimates/FLIPI/3_Output/FE/PERFORMANCE/overall 2022/TIME SERIES' ;


ods graphics on / 
      width=14in height=10in
      outputfmt=gif
      imagemap=on
      imagename="Picture"
      border=off  reset=index ;


proc sgplot data=testfile noborder noopaque nowall  dattrmap=DATTRMAP  
title "write the title";
by stock ;
highlow x=date low=low high=high / type=bar   ;
scatter x=date y=close   ;
xaxis type=discrete;
run;
1

There are 1 best solutions below

1
Reeza On

Here are two ways to do this, one uses the #byval as indicated in the previous solution and which you used in title but not the file name for some reason.

First make fake data:

data prdsummary;
    set sashelp.prdsale;
    where year=1993 and (country="GERMANY" or country="CANADA") and region="EAST" 
        and division="CONSUMER" and
    (product="SOFA" or product="TABLE" or product="BED");
run;

proc sort data=prdsummary;
    by country;
run;

Then use #BYVAL. However, if you run this code multiple times, SAS does not overwrite the previous files, it adds a 1 to the filename instead. So you need to make sure that the files don't already exist. There may be an option to control this feature, I'm unsure at the moment.


ods listing gpath='/home/fkhurshed/Demo1/';
title 'Graph for #byval1';
ods graphics / imagename="Country #byval1";

proc sgplot data=prdsummary;
    by country;
    vbar prodtype / group=product response=actual groupdisplay=stack;
run;

A second workaround is to use a macro instead and control the name explicitly.

*explicitly control the names via macro looping;
ods listing gpath='/home/fkhurshed/Demo1/';


%macro generate_country_graph(country=);
    ods graphics / imagename="Graphic for &Country.";

    proc sgplot data=prdsummary;
        where country="&country";
        vbar prodtype / group=product response=actual groupdisplay=stack;
    run;

%mend;

data _null_;
    set prdsummary;
    by country;

    if first.country then
        do;
            str=catt('%generate_country_graph(country=', country, ');');
            call execute(str);
        end;
run;