Brad
Brad

Reputation: 85

SAS-Dynamically Writing Bar Charts to Excel workbook in SAS Macro

I am trying to dynamically generate and export bar charts to and an excel workbook. My Macro pulls certain distinct Identifier codes and creates two summary tables (prov_&x table and the prov_revcd_&x) and populates to single excel sheet, for each respective ID. I have been unable however to successfully generate bar charts for the data and export to excel. Below is a condensed version of code. I have removed the steps for creating the prov_&x table and the prov_revcd_&x table to help keep the post as concise as possible. I have tried using the GOUT function and NAME function and then explicitly calling those but that does not seem to work. Any suggestions are welcomed and I understand my macro code is a little sloppy but it generates the tables so I will clean up once I can get the bar charts to generate.

Also, I can see in my results viewer that the graphs are generating, so I'm assuming the problem is in how I am trying to reference them to the workbook. THANKS!

%macro runtab(x);

/*Create summary chart for generating graph of codes billed per month*/
proc sql;
CREATE TABLE summary_&x AS
select DISTINCT month, COUNT (CH_ICN) AS ICN_Count, CLI_Revenue_Cd_Category_Cd
FROM corf_data1_sorted
WHERE BP_Billing_Prov_Num_OSCAR=&x
group by month ,CLI_Revenue_Cd_Category_Cd;
run;

/*Create a graph of Services Per Month and group by the Revenue Code*/
proc sgplot data=summary_&x NAME= 'graph_&x';
  title 'Provider Revenue Analysis';
  vbar month / response=ICN_count group=CLI_Revenue_Cd_Category_Cd stat=sum
       datalabel datalabelattrs=(weight=bold);
  yaxis grid  label='Month';
  run;
%mend runtab;


/*Create a macro variable of all the codes */
proc sql noprint;
  select BP_Billing_Prov_Num_OSCAR
  into :varlist separated by ' ' /*Each code in the list is sep. by a single space*/
from provider;
quit;


%let cntlist = &sqlobs; /*Store a count of the number of oscar codes*/
%put &varlist; /*Print the codes to the log to be sure our list is accurate*/



/*write a macro to generate the output tables*/
%macro output(x);


ods tagsets.excelxp options(sheet_interval='none' sheet_name="&x");

proc print data=prov_&x;
run;

proc print data=prov_revcd_&x;
run;

proc print data=graph_&x;
run;  

%mend;

/*Run a loop for each oscar code. Each code will enter the document generation loop*/
%macro loopit(mylist);
    %let else=;
   %let n = %sysfunc(countw(&mylist)); /*let n=number of codes in the list*/
    data
   %do I=0 %to &n;
      %let val = %scan(&mylist,&I); /*Let val= the ith code in the list*/
    %end;


   %do j=0 %to &n;
      %let val = %scan(&mylist,&j); /*Let val= the jth code in the list*/
/*Run the macro loop to generate the required tables*/
%runtab(&val);


%output(&val);


   %end;
   run;
%mend;


/*Run the macro loop over the list of significant procedure code values*/


ods tagsets.excelxp file="W:\user\test_wkbk.xml";


%loopit(&varlist)


ods tagsets.excelxp close;

Upvotes: 2

Views: 736

Answers (1)

Joe
Joe

Reputation: 63424

You can't export charts with ODS TAGSETS.EXCELXP, unfortunately.

You have a few options if you need to export charts.

  1. Use ODS Excel, available in the more recent maintenance releases of SAS 9.4. See Chris H's blog post for more information on that. It is fairly similar to Tagsets.ExcelXP, but not identical. It does generate a "Real" excel file (.xlsx).
  2. Create an HTML file that Excel can read, using TAGSETS.MSOFFICE2K or regular HTML. Chevell Parker, a SAS tech support analyst, has a few papers like this one on the different options.
  3. Use DDE to write your image to the excel file. Not a preferred option but included for completeness.

There is also a new proc - proc mschart - that is enabled in SAS 9.4 TS1M3 due out in a month or two, that will generate Excel charts in ODS EXCEL (ie, not an image, but telling Excel to make a chart here please).

Upvotes: 1

Related Questions