%let name=mobile_os_wars;
filename odsout '.';

data foo;
year2=.; year2=put(today(),year4.);
month2=.; month2=put(today(),month.);
monname2=put(today(),monname3.);
run;

proc sql noprint;
select unique year2 into :year2 separated by ' ' from foo;
select unique month2 format=z2. into :month2 separated by ' ' from foo;
select unique monname2 into :monname2 separated by ' ' from foo;
quit; run;


/*
This bit of tricky code downloads the csv file on-the-fly,
so you don't have to do it manually :)

How did I figure out the URL? ...
Viewed the report in Google Chrome: 
http://gs.statcounter.com/#mobile_os-ww-monthly-200911-201207
Right-clicked on the "Download data" link, and ran 'Inspect element'.
Right-clicked on the href line, and did a 'Copy link address'
Then pasted it into the SAS code below...
http://gs.statcounter.com/chart.php?statType_hidden=mobile_os&region_hidden=ww&granularity=monthly&statType=Mobile%20OS&region=Worldwide&fromMonthYear=2009-11&toMonthYear=2012-07&csv=1
*/

%let csvname=&name..csv;

%include '../data_vault/proxy.sas';
/*
I %include a file that sets the my_proxy macro variable.
You won't be able to use my proxy, therefore set your own
macro variable, which will be something like the following:
%let my_proxy=http://yourproxy.com:80;
*/

filename xlsfile url
"http://gs.statcounter.com/chart.php?statType_hidden=mobile_os&region_hidden=ww&granularity=monthly&statType=Mobile%20OS&region=Worldwide&fromMonthYear=2011-01&toMonthYear=&year2-&month2&csv=1"
 proxy="&my_proxy";

data _null_;
 n=-1;
 infile xlsfile recfm=s nbyte=n length=len _infile_=tmp;
 input;
 file "&csvname" recfm=n;
 put tmp $varying32767. len;
run;

PROC IMPORT OUT=my_data DATAFILE="&csvname" DBMS=CSV REPLACE;
GETNAMES=YES;
DATAROW=2;
RUN;

data my_data; set my_data;
format date date9.;
date=datepart(date);
month=.; month=put(date,month.);
year=.; year=put(date,year4.);
run;

data tran_data; set my_data (drop=date);
run;

data my_data; set my_data;
length my_html $500;
my_html=
 'title='||quote(
  put(date,date9.)||'0d'x||
  '----------------'||'0d'x||
  trim(left(SymbianOS))||'% SymbianOS '||'0d'x||
  trim(left(iOS))||'% iOS '||'0d'x||
  trim(left(Android))||'% Android '||'0d'x||
  trim(left(BlackBerry_OS))||'% BlackBerry_OS '||'0d'x||
  trim(left(Unknown))||'% Unknown '||'0d'x||
  trim(left(Samsung))||'% Samsung '||'0d'x||
  trim(left(Sony_Ericsson))||'% Sony_Ericsson '||'0d'x||
  trim(left(Series_40))||'% Series_40 '||'0d'x||
  trim(left(other))||'% Other ')||
 ' href="mobile_os_wars_info.htm"';
run;

data anno_year; set my_data (where=(month=1));
xsys='2'; ysys='1'; hsys='3'; when='a';
function='label';
text=put(date,year4.);
x=date;
y=-5; position='8';
output;
function='move'; x=date; y=0; output;
function='draw'; y=100; size=.0001; color='graydd'; when='b'; output;
run;

proc sort data=tran_data out=tran_data;
by year month;
run;
proc transpose data=tran_data out=tran_data;
by year month;
run;
data tran_data; set tran_data;
_name_=trim(left(translate(_name_,' ','_')));
if col1<1 then _name_='Other';
length my_html $300;
my_html=
 'title='||quote(
  trim(left(_name_))||'0d'x||
  'Year: '||trim(left(year))||'0d'x||
  'Month: '||trim(left(month))||'0d'x||
  trim(left(col1))||'%')||
 ' href="mobile_os_wars_info.htm"';
run;


goptions device=png;
goptions xpixels=900 ypixels=500;
goptions noborder;
 
ODS LISTING CLOSE;
ODS HTML path=odsout body="&name..htm" 
 (title="Worldwide Mobile Operating System Popularity") 
 style=sasweb;

goptions gunit=pct htitle=4.0 ftitle="albany amt/bold" htext=2.8 ftext="albany amt/bold";
goptions ctext=gray33;

title1 
 link="http://gs.statcounter.com/#mobile_os-ww-monthly-200911-&year2&month2"
 ls=1.5 "Worldwide Mobile Operating System Popularity";

title2 
 link="http://gs.statcounter.com/#mobile_os-ww-monthly-200911-&year2&month2"
 ls=1.0 color=gray "Data Source: statcounter.com";

pattern1 v=solid repeat=50;

axis1 label=(j=c 'Percent' j=c 'of Market') minor=none offset=(0,0)
 order=(0 to 100 by 25) major=(color=black);

axis2 label=(color=gray) offset=(0,.8);

axis3 label=(color=gray) value=(font='albany amt' height=2 color=gray);

legend1 label=none across=1 position=(right middle) offset=(9,0) 
 shape=bar(.15in,.15in) order=descending mode=share;
title3 a=-90 h=15pct " ";

proc gchart data=tran_data;
vbar month / discrete nozero type=sum sumvar=col1
 subgroup=_name_ group=year
 space=0 gspace=.8
 raxis=axis1 gaxis=axis2 maxis=axis3
 legend=legend1 noframe
 autoref frontref cref=gray77 lref=1 
 html=my_html
 des='' name="&name";
run;


title3;

axis4 label=(j=c 'Percent' j=c 'of Market') major=none minor=none style=0 offset=(0,0);

legend2 label=none across=1 position=(right middle) offset=(0,22) 
 repeat=2;

symbol1 value=circle height=1.7 interpol=join line=1 repeat=50;

data tran_data; set tran_data;
date=year+((month-1)/12);
run;

proc sql noprint;
select int(min(date)) into :minyr from tran_data;
select int(max(date))+1 into :maxyr from tran_data;
quit; run;

data pltanno; 
xsys='2'; ysys='3'; hsys='3'; when='a';
do year = &minyr to &maxyr by 1;
 x=year+.5; y=5;
 function='label'; position='5'; text=trim(left(year));
 output;
 end;
run;

axis5 label=(h=3 ' ') order=(&minyr to &maxyr by 1) major=none minor=none
 value=none offset=(0,0);

/* move the plot markers over about 1/2 a month, so they don't plot right on the refline */
data tran_data; set tran_data;
date=date+.04;
run;

proc gplot data=tran_data anno=pltanno;
plot col1*date=_name_ / 
 vaxis=axis4 haxis=axis5 vzero
 autovref cvref=graydd
 autohref chref=graydd
 legend=legend2 noframe
 html=my_html
 des='' name="&name";
run;

quit;
ODS HTML CLOSE;
ODS LISTING;
