2

I'm pretty new to coding and SAS in general. I tried to create a bunch of KPI charts that correspond to the number of rows, but the loop code below keeps creating two of the same GPKI charts for the last row. Why is this the case? Any help will be greatly appreciated.

Thanks

%Macro scanloop (scanfile,field1,field2,field3);
data _null_;
if 0 then set &scanfile nobs=X;
call symput ('Count',X);
stop;
run;

%DO I=1 %To &count;
Data _null_;
set &scanfile (firstobs=&I);
call symput('Client', &field1);
call symput('LossRatio', &field2 );
call symput('Target', &field3 );    
stop;
run;

proc gkpi mode=raised;
speedometer actual=&LossRatio bounds=(0 .2 .4 .6 .8 1) /
target=&Target label="&field1 KPI" nolowbound format="percent8.0"
afont=(f="Albany AMT" height=.5cm)
bfont=(f="Albany AMT" height=.4cm) ;
Run;
%end;
%MEND SCANLOOP;

%scanloop (work.Test, Client,LossRatio,Target);run;
  • Is this really the way to make these charts?? They couldn't be made by using a BY statement instead, perhaps? This sort of macro looping is horribly inefficient and prone to errors. – Joe Jul 27 '13 at 04:19

1 Answers1

0

It's the extra run;

I don't know why sas does this, but I have seen it in my own code. Sometimes charts will print a second copy when there is an extra run at the end of the program.

You don't need to run; after a macro as it is not a macro language function and the macro will go ahead and work without it. The end result is that the last trip through the loop will look like this (notice the extra run at the end)

proc gkpi mode=raised;
speedometer actual=&LossRatio bounds=(0 .2 .4 .6 .8 1) /
target=&Target label="&field1 KPI" nolowbound format="percent8.0"
afont=(f="Albany AMT" height=.5cm)
bfont=(f="Albany AMT" height=.4cm) ;
Run;
run;

Due to the aforementioned bug (feature?), this is probably what is causing your duplicate chart. I don't think this happens in all output modes, but if you are using ods to output to a pdf, I am pretty sure that this is your problem.

Joe is right though that you should try to avoid doing this with a macro if at all possible.

o.h
  • 1,202
  • 1
  • 14
  • 24