DATA Step, Macro, Functions and more

proc gplot in a macro

Reply
Occasional Contributor
Posts: 15

proc gplot in a macro

[ Edited ]

I don't understand why the code below:

 

- cannot find the axis1 statement, regardless of where it is put

- outputs the resulting .png file 3 times

[otherwise it generates the plot]

 

the code:

 

* macro to graph three models ;

%macro plotit;
    symbol1 color=&down2_color interpol=join value=dot height=2 width=5;
    symbol2 color=&down1_color interpol=join value=dot height=2 width=5;
    symbol3 color=&base_color interpol=join value=dot height=2 width=5;
    symbol4 color=&up1_color interpol=join value=dot height=2 width=5;
    symbol5 color=&up2_color interpol=join value=dot height=2 width=5;
    *axis2 offset=(0 0) value=(angle = 0 ) width=1 label=(h=3) label=(a=90 h=3 c=&actuals_color 'Actual' c=black ' and ' c=&forecast_color 'Expected') minor=none ;
    axis1 /* offset=(0 0) */
    width=3 label=none value=(angle=55) order=(&start_date to &end_date by month);
    * axis3 value=(c=red) label=(angle=90 c=&pererr_color '% Error between Actual and Forecast') ;
    legend1 frame /*shape=symbol(4,2)*/
    position=(bottom right inside) mode=reserve label=('');
    * loop through the shocked variables ;

    %do i=1 %to 1;
        *loop through the products ;

        %do j=1 %to 1;
            *loop through the graph variables ;

            %do k=1 %to 1;
                axis1 /* offset=(0 0) */
                width=3 label=none value=(angle=55) order=(&start_date to &end_date by 
                    month);
                options orientation=landscape;
                filename graphout 
                    "C:\grezek\proj\sensitivity\graphout\&&invar&i.._&&product&j.._&&gvar&k...png";
                goptions reset=all border gunit=pct cback=white device=png300 
                    gsfname=graphout gsfmode=replace target=png300;

                proc gplot data=sasdb.impact_&&invar&i.._&&product&j.. (where=(&start_date <=end_effective_date <=&end_date));
                    format end_effective_date monyy7.;
                    plot &&&gvar&k.._&&&invar&i..&&&&&&invar&i.._perc1. * end_effective_date
&&gvar&i.._&&&invar&i..&&&&&&invar&i.._perc2. * end_effective_date
&&gvar&i.. * end_effective_date
&&gvar&i.._&&&invar&i..&&&&&&invar&i.._perc3. * end_effective_date
&&gvar&i.._&&&invar&i..&&&&&&invar&i.._perc4. * end_effective_date / vzero 
                        overlay haxis=axis1

                        /* vaxis = axis2 */
                        autovref autohref /*legend = legend1 */;

                    /*
                     * plot2 &model_var._diff*&month / vaxis = axis3 ;
                    
                    title "&portfolio &sproduct" ;
                    title2 "Mape: %sysfunc(putn(&mape.,percent10.2))
                    RMSE: %sysfunc(putn(&rmse.,percent10.2))
                    Cumulative % Error: %sysfunc(putn(&mape_cum.,percent10.2))" ;
                    footnote "Training Period Jan 2007 - Sep 2014" ;*/
%end;
            %end;
        %end;
        run;
    %mend plotit;

    run;
    %plotit;
    run;

 

 

the log:

 

2723

2724 * macro to graph three models ;

2725 %macro plotit ;

2726 symbol1 color=&down2_color interpol=join value=dot height=2 width=5 ;

2727 symbol2 color=&down1_color interpol=join value=dot height=2 width=5 ;

2728 symbol3 color=&base_color interpol=join value=dot height=2 width=5 ;

2729 symbol4 color=&up1_color interpol=join value=dot height=2 width=5 ;

2730 symbol5 color=&up2_color interpol=join value=dot height=2 width=5 ;

2731

2732 *axis2 offset=(0 0) value=(angle = 0 ) width=1 label=(h=3) label=(a=90 h=3 c=&actuals_color 'Actual' c=black ' and '

2732! c=&forecast_color 'Expected') minor=none ;

2733

2734 axis1 /* offset=(0 0) */ width=3 label=none value=(angle=55) order=(&start_date to &end_date by month) ;

2735 * axis3 value=(c=red) label=(angle=90 c=&pererr_color '% Error between Actual and Forecast') ;

2736 legend1 frame /*shape=symbol(4,2)*/ position=(bottom right inside) mode=reserve label=('') ;

2737

2738 * loop through the shocked variables ;

2739 %do i = 1 %to 1 ;

2740 *loop through the products ;

2741 %do j = 1 %to 1 ;

2742 *loop through the graph variables ;

2743 %do k = 1 %to 1 ;

2744 axis1 /* offset=(0 0) */ width=3 label=none value=(angle=55) order=(&start_date to &end_date by month) ;

2745

2746 options orientation=landscape;

2747 filename graphout "C:\grezek\proj\sensitivity\graphout\&&invar&i.._&&product&j.._&&gvar&k...png";

2748 goptions reset = all border gunit = pct cback = white device = png300 gsfname=graphout gsfmode=replace target = png300 ;

2749

2750 proc gplot data = sasdb.impact_&&invar&i.._&&product&j.. (where=(&start_date <= end_effective_date <= &end_date)) ;

2751 format end_effective_date monyy7.;

2752 plot &&&gvar&k.._&&&invar&i..&&&&&&invar&i.._perc1. * end_effective_date

2753 &&gvar&i.._&&&invar&i..&&&&&&invar&i.._perc2. * end_effective_date

2754 &&gvar&i.. * end_effective_date

2755 &&gvar&i.._&&&invar&i..&&&&&&invar&i.._perc3. * end_effective_date

2756 &&gvar&i.._&&&invar&i..&&&&&&invar&i.._perc4. * end_effective_date / vzero overlay

2757 haxis = axis1

2758 /* vaxis = axis2 */

2759 autovref autohref /*legend = legend1 */;

2760 /*

2761 * plot2 &model_var._diff*&month / vaxis = axis3 ;

2762

2763 title "&portfolio &sproduct" ;

2764 title2 "Mape: %sysfunc(putn(&mape.,percent10.2))

2765 RMSE: %sysfunc(putn(&rmse.,percent10.2))

2766 Cumulative % Error: %sysfunc(putn(&mape_cum.,percent10.2))" ;

2767 footnote "Training Period Jan 2007 - Sep 2014" ;*/

2768 %end ;%end ; %end ; run ;

2769

2770 %mend plotit ;

2771 run ;

WARNING: AXIS statement 1 not found. The default axis description will be used.

NOTE: 71709 bytes written to C:\grezek\proj\sensitivity\graphout\unemp_1st_mtg_exp_m_EL.png.

2772 %plotit;run;

SYMBOLGEN: Macro variable DOWN2_COLOR resolves to blue

MPRINT(PLOTIT): symbol1 color=blue interpol=join value=dot height=2 width=5 ;

SYMBOLGEN: Macro variable DOWN1_COLOR resolves to brown

MPRINT(PLOTIT): symbol2 color=brown interpol=join value=dot height=2 width=5 ;

SYMBOLGEN: Macro variable BASE_COLOR resolves to black

MPRINT(PLOTIT): symbol3 color=black interpol=join value=dot height=2 width=5 ;

SYMBOLGEN: Macro variable UP1_COLOR resolves to green

MPRINT(PLOTIT): symbol4 color=green interpol=join value=dot height=2 width=5 ;

SYMBOLGEN: Macro variable UP2_COLOR resolves to red

MPRINT(PLOTIT): symbol5 color=red interpol=join value=dot height=2 width=5 ;

MPRINT(PLOTIT): *axis2 offset=(0 0) value=(angle = 0 ) width=1 label=(h=3) label=(a=90 h=3 c=&actuals_color 'Actual' c=black

' and ' c=&forecast_color 'Expected') minor=none ;

SYMBOLGEN: Macro variable START_DATE resolves to 20635

SYMBOLGEN: Macro variable END_DATE resolves to 21427

MPRINT(PLOTIT): axis1 width=3 label=none value=(angle=55) order=( 20635 to 21427 by month) ;

MPRINT(PLOTIT): * axis3 value=(c=red) label=(angle=90 c=&pererr_color '% Error between Actual and Forecast') ;

MPRINT(PLOTIT): legend1 frame position=(bottom right inside) mode=reserve label=('') ;

WARNING: Can not use MODE=RESERVE and POSITION=(INSIDE). Changed to MODE=PROTECT.

MPRINT(PLOTIT): * loop through the shocked variables ;

MPRINT(PLOTIT): *loop through the products ;

MPRINT(PLOTIT): *loop through the graph variables ;

SYMBOLGEN: Macro variable START_DATE resolves to 20635

SYMBOLGEN: Macro variable END_DATE resolves to 21427

MPRINT(PLOTIT): axis1 width=3 label=none value=(angle=55) order=( 20635 to 21427 by month) ;

MPRINT(PLOTIT): options orientation=landscape;

SYMBOLGEN: && resolves to &.

SYMBOLGEN: Macro variable I resolves to 1

SYMBOLGEN: && resolves to &.

SYMBOLGEN: Macro variable J resolves to 1

SYMBOLGEN: && resolves to &.

SYMBOLGEN: Macro variable K resolves to 1

SYMBOLGEN: Macro variable INVAR1 resolves to unemp

SYMBOLGEN: Macro variable PRODUCT1 resolves to 1st_mtg

SYMBOLGEN: Macro variable GVAR1 resolves to exp_m_EL

MPRINT(PLOTIT): filename graphout "C:\grezek\proj\sensitivity\graphout\unemp_1st_mtg_exp_m_EL.png";

MPRINT(PLOTIT): goptions reset = all border gunit = pct cback = white device = png300 gsfname=graphout gsfmode=replace target

= png300 ;

NOTE: There were 27 observations read from the data set SASDB.IMPACT_UNEMP_1ST_MTG.

WHERE (end_effective_date>=20635 and end_effective_date<=21427);

NOTE: PROCEDURE GPLOT used (Total process time):

real time 7:41.89

cpu time 3.16 seconds

 

SYMBOLGEN: && resolves to &.

SYMBOLGEN: Macro variable I resolves to 1

SYMBOLGEN: && resolves to &.

SYMBOLGEN: Macro variable J resolves to 1

SYMBOLGEN: Macro variable INVAR1 resolves to unemp

SYMBOLGEN: Macro variable PRODUCT1 resolves to 1st_mtg

SYMBOLGEN: Macro variable START_DATE resolves to 20635

SYMBOLGEN: Macro variable END_DATE resolves to 21427

MPRINT(PLOTIT): proc gplot data = sasdb.impact_unemp_1st_mtg (where=( 20635 <= end_effective_date <= 21427)) ;

MPRINT(PLOTIT): format end_effective_date monyy7.;

SYMBOLGEN: && resolves to &.

SYMBOLGEN: Unable to resolve the macro variable reference &gvar

SYMBOLGEN: && resolves to &.

SYMBOLGEN: Macro variable K resolves to 1

SYMBOLGEN: && resolves to &.

SYMBOLGEN: Unable to resolve the macro variable reference &invar

SYMBOLGEN: Macro variable GVAR1 resolves to exp_m_EL

SYMBOLGEN: && resolves to &.

SYMBOLGEN: Macro variable I resolves to 1

SYMBOLGEN: Macro variable INVAR1 resolves to unemp

SYMBOLGEN: && resolves to &.

SYMBOLGEN: && resolves to &.

SYMBOLGEN: && resolves to &.

SYMBOLGEN: Macro variable I resolves to 1

SYMBOLGEN: && resolves to &.

SYMBOLGEN: Macro variable INVAR1 resolves to unemp

SYMBOLGEN: Macro variable UNEMP_PERC1 resolves to _down5

SYMBOLGEN: && resolves to &.

SYMBOLGEN: Macro variable I resolves to 1

SYMBOLGEN: && resolves to &.

SYMBOLGEN: Unable to resolve the macro variable reference &invar

SYMBOLGEN: Macro variable GVAR1 resolves to exp_m_EL

SYMBOLGEN: && resolves to &.

SYMBOLGEN: Macro variable I resolves to 1

SYMBOLGEN: Macro variable INVAR1 resolves to unemp

SYMBOLGEN: && resolves to &.

SYMBOLGEN: && resolves to &.

SYMBOLGEN: && resolves to &.

SYMBOLGEN: Macro variable I resolves to 1

SYMBOLGEN: && resolves to &.

SYMBOLGEN: Macro variable INVAR1 resolves to unemp

SYMBOLGEN: Macro variable UNEMP_PERC2 resolves to _down2

SYMBOLGEN: && resolves to &.

SYMBOLGEN: Macro variable I resolves to 1

SYMBOLGEN: Macro variable GVAR1 resolves to exp_m_EL

SYMBOLGEN: && resolves to &.

SYMBOLGEN: Macro variable I resolves to 1

SYMBOLGEN: && resolves to &.

SYMBOLGEN: Unable to resolve the macro variable reference &invar

SYMBOLGEN: Macro variable GVAR1 resolves to exp_m_EL

SYMBOLGEN: && resolves to &.

SYMBOLGEN: Macro variable I resolves to 1

SYMBOLGEN: Macro variable INVAR1 resolves to unemp

SYMBOLGEN: && resolves to &.

SYMBOLGEN: && resolves to &.

SYMBOLGEN: && resolves to &.

SYMBOLGEN: Macro variable I resolves to 1

SYMBOLGEN: && resolves to &.

SYMBOLGEN: Macro variable INVAR1 resolves to unemp

SYMBOLGEN: Macro variable UNEMP_PERC3 resolves to _up2

SYMBOLGEN: && resolves to &.

SYMBOLGEN: Macro variable I resolves to 1

SYMBOLGEN: && resolves to &.

SYMBOLGEN: Unable to resolve the macro variable reference &invar

SYMBOLGEN: Macro variable GVAR1 resolves to exp_m_EL

SYMBOLGEN: && resolves to &.

SYMBOLGEN: Macro variable I resolves to 1

SYMBOLGEN: Macro variable INVAR1 resolves to unemp

SYMBOLGEN: && resolves to &.

SYMBOLGEN: && resolves to &.

SYMBOLGEN: && resolves to &.

SYMBOLGEN: Macro variable I resolves to 1

SYMBOLGEN: && resolves to &.

SYMBOLGEN: Macro variable INVAR1 resolves to unemp

SYMBOLGEN: Macro variable UNEMP_PERC4 resolves to _up5

MPRINT(PLOTIT): plot exp_m_EL_unemp_down5 * end_effective_date exp_m_EL_unemp_down2 * end_effective_date exp_m_EL *

end_effective_date exp_m_EL_unemp_up2 * end_effective_date exp_m_EL_unemp_up5 * end_effective_date / vzero overlay haxis =

axis1 autovref autohref ;

MPRINT(PLOTIT): run ;

WARNING: AXIS statement 1 not found. The default axis description will be used.

NOTE: 71709 bytes written to C:\grezek\proj\sensitivity\graphout\unemp_1st_mtg_exp_m_EL.png.

WARNING: AXIS statement 1 not found. The default axis description will be used.

NOTE: 71709 bytes written to C:\grezek\proj\sensitivity\graphout\unemp_1st_mtg_exp_m_EL.png.

2773 run ;

WARNING: AXIS statement 1 not found. The default axis description will be used.

NOTE: 71709 bytes written to C:\grezek\proj\sensitivity\graphout\unemp_1st_mtg_exp_m_EL.png.

run ;

Super User
Super User
Posts: 6,497

Re: proc gplot in a macro

What happens if you add a RUN; and QUIT; statement before the macro call?

From your SAS log it looks like PROC GPLOT is already running before you submitted your macro call.

MPRINT(PLOTIT): goptions reset = all border gunit = pct cback = white device = png300 gsfname=graphout gsfmode=replace target                                   
= png300 ;                                                                                                                                                      
NOTE: There were 27 observations read from the data set SASDB.IMPACT_UNEMP_1ST_MTG.                                                                             
WHERE (end_effective_date>=20635 and end_effective_date<=21427);                                                                                                
NOTE: PROCEDURE GPLOT used (Total process time):                                                                                                                
real time 7:41.89                                                                                                                                               
cpu time 3.16 seconds                                                                                                                                           
                                                                                                                                                                
MPRINT(PLOTIT): proc gplot data = sasdb.impact_unemp_1st_mtg (where=( 20635 <= end_effective_date <= 21427)) ;     
Occasional Contributor
Posts: 15

Re: proc gplot in a macro

Tom,

 

Thanks for responding so quickly.  I added the run; and quit; statements before invoking the macro.  I get the same results.

 

When you said that the macro looked as though is was running, I ran the code in batch (I've been running in DMS, which I usually don't do).  I still have the same problem.  It is as though the code thinks that it should run 3 times, although I would think that the gplot in the center of three do loops would run the procedute once.  Note that it doesn't read the symbol statements either.

 

I appreciate your comments.  -- George

Super User
Posts: 17,750

Re: proc gplot in a macro

So before ever creating a macro you should have working code for the graphs you want. Do you have that code handy?

If so, a comparison between this and that may help. Given the amount of code it's hard to process especially when you can't see the data. Can you add some fake data so we could run to help debug? 

 

 

Occasional Contributor
Posts: 15

Re: proc gplot in a macro

Good idea. Let me rewrite without the macro; I apologize for the amount of code - it's hard to read I know. -- George
Super User
Posts: 3,101

Re: proc gplot in a macro

[ Edited ]

One thing to be careful of in macros are normal comments (* Comment; /* Comment */). In my experience they can cause the macro parser to get confused. So I'd suggest removing the comments or converting them to macro comments ( %* Macro Comment; ).

Occasional Contributor
Posts: 15

Re: proc gplot in a macro

Thank you very much!  I've always been a bit careless with comments in macros.  -- George

Super User
Posts: 6,928

Re: proc gplot in a macro

From what I see, your code has an unfinished proc gplot started before the macro is defined and called; that gplot complains about the missing axis statement. (It is started by the run; immediately before the %plotit call)

Either make that proc gplot complete BEFORE defining and running the macro, or remove it, or move it into the macro.

 

You should also move the run; inside the macro to the end of the innermost %do loop, as it really belongs there.

All other run; statements should then become obsolete.

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 15

Re: proc gplot in a macro

Thank you very very -- especially the absent run statement was causing problems. -- George 

Ask a Question
Discussion stats
  • 8 replies
  • 234 views
  • 1 like
  • 5 in conversation