%macro BarChart( _dset, _Var, _Val_labels, _chtType_, _ChtSize_, _ChartTitle_); /******************************************************************************/ /* Macro: BarChart.SAS Date: 12/16/2007 */ /* Auth: Phil Rack, MineQuest, llc Revd: */ /* */ /* Parms: */ /* %BarChart(Dataset, Variable, VarLabels, ChartType, ChartSize, ChartTitle) */ /* */ /* Where: */ /* Dataset - then name of the dataset containing the values to be plotted */ /* Variable - the variable that contains the numeric counts */ /* VarLabels - The variable that contains the labels for the pie slices */ /* ChartType - V for a Vertical Bar Chart and H for a Horizontal Bar Chart */ /* ChartSize - SM=Small, MED=Medium, LG = Large */ /* ChartTitle - The title for the chart. Spaces should have a '+' sign */ /* */ /* Desc: Takes two variables from a data set and creates a Bar Chart calling */ /* the google chart api. */ /* */ /******************************************************************************/ Proc Contents data= &_dset noprint out=_plotP3temp_; Run; *--> get the number of obs which will be the number of vars after the transpose; Data _null_; Set _plotP3temp_; if _n_=1; Call symput('Nobs',compress(nobs)); Run; *--> flip the data so that we can get it in a form for further processing; Proc Transpose data= &_dset out=_transVal_ prefix=val; VAR &_Val_labels ; Run; Proc Transpose data= &_dset out= _TransCnt_ prefix=cnt; var &_var; Run; data _trans_; merge _transVal_ _TransCnt_; Run; *--> build the strings needed so we can pass them to google; Data _PlotData_(keep=value_label Count_label MaxLabLen maxLabLens lablen1-lablen5 ); Set _trans_; Length Value_label count_label $ 2048 MaxLabLens $ 8 lablen1-lablen&nobs 8; %If %upcase("&_chtType_") = "H" %then %Do; value_label = '1:'; %End; %If %upcase("&_chtType_") = "V" %then %Do; value_label = '0:'; %End; Count_label = ''; Array Cntcol{&nobs} Cnt1--Cnt&&nobs; Array valcol{&nobs} Val1--Val&&nobs; Array LabLen{&nobs} Lablen1--Lablen&&nobs; *--> the horizontal bar chart labels; Do ii=1 to &nobs; if ii = 1 then Count_label = compress(CntCol{ii}||','); if (ii gt 1) and (ii lt &nobs) then Count_label = compress(Count_label||CntCol{ii}||','); if (ii=&nobs) then Count_label = compress(count_label||cntCol{ii}); End; *--> Create the string that has the data; %If %upcase("&_ChtType_" ) = "V" %then %Do; Do jj= 1 to &nobs; value_label = compress(Value_label||'|'||ValCol{jj}); End; %End; %If %upcase("&_ChtType_") = "H" %then %Do; Do jj= &nobs to 1 by -1; value_label = compress(Value_label||'|'||ValCol{jj}); End; %End; *--> get max label length; Do ii=1 to &nobs; labLen{ii}=(length(ValCol{ii})*5)+5; End; MaxLabLen = max(of lablen1-lablen&&nobs); MaxLabLens = Left(trim(MaxLabLen)); Run; *--> create the macro variables that will contain the values to be passed to google; Data initmacrovars; Set _plotdata_; Call symput('Value_label',compress(value_label)); Call symput('Count_Vals', compress(Count_label)); Call symput('MaxLabLens', MaxLabLens); Run; *--> delete the temporary data sets - no longer needed; Proc datasets ddname=work; delete initmacrovars _labels_ _plotdata_ _counts_; quit; Run; *--> pass the url to internet explorer and invoke the google graphics service; Data _null_; length gURL $ 2048; %let chartSize=; %let _ChartType_=; %let _ChartType_ = bhg; *--> default chart type - horizontal bar chart; *--> get the size of the charts in pixels; %if (%upcase("&_chtType_") = "H") %then %do; %if %upcase("&&_chtSize_") = "SM" %then %let ChartSize = 400x250; %else %if %upcase("&&_chtSize_") = "MED" %then %let ChartSize = 500x300; %else %if %Upcase("&&_chtSize_") = "LG" %then %let ChartSize = 750x400; %else %let ChartSize = 400x200; *--> use this as the default if sm, med or lg not chosen; %let MaxLabLength=; %End; %if %upcase("&_chtType_") = "V" %then %do; %if %upcase("&&_chtSize_") = "SM" %then %let ChartSize = 200x300; %else %if %upcase("&&_chtSize_") = "MED" %then %let ChartSize = 300x400; %else %if %Upcase("&&_chtSize_") = "LG" %then %let ChartSize = 500x550; %else %let ChartSize = 300x400; *--> use this as the default if sm, med or lg not chosen; %let MaxLabLength = &chbh=&MaxLabLens; %End; %if %upcase("&_chtType_") = "H" %then %let _ChartType_ = bhg; %else %if %upcase("&_chtType_") = "V" %then %let _ChartType_ = bvg; *--> Build the command string to call the google web service; %if (%upcase("&_chtType_") = "H") or (%upcase("&_chtType_") = "V") %then %do; gURL = ("&&webbrowser http://chart.apis.google.com/chart?chf=bg,s&cht=&&_ChartType_&chd=t:&&count_vals"|| "&MaxLabLength&chtt=&&_ChartTitle_&chs=&&chartsize&chxt=x,y&chxl=&&value_label"); Call System(gURL); %end; %else %do; put 'ERROR: Invalid Chart Type Specified. Chart Type must have a value of H or V.'; put "ERROR: A value of &&_chttype_ was found instead."; %end; Run; %Mend BarChart;