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

%let marker_color=red;
%let pattern_color=gray88;
%let wheel_color=cxe55d6b;

/* 
http://mashable.com/2013/05/07/youtube-map-trends/
*/

data my_data;
infile datalines dlm=',';
length city $50 state $2 video_id $20 video_title $100;
input city state video_id video_title;
datalines;
Raleigh, NC, dTnpkmPwHeM, SAS Analytics U - Emily Baranello
Atlanta, GA, dTnpkmPwHeM, SAS Analytics U - Emily Baranello
Jackson, MS, dTnpkmPwHeM, SAS Analytics U - Emily Baranello
Houston, TX, dTnpkmPwHeM, SAS Analytics U - Emily Baranello
Rensselaer, NY, dTnpkmPwHeM, SAS Analytics U - Emily Baranello
Bangor, ME, dTnpkmPwHeM, SAS Analytics U - Emily Baranello
Salt Lake City, UT, dTnpkmPwHeM, SAS Analytics U - Emily Baranello
Nashville, TN, dTnpkmPwHeM, SAS Analytics U - Emily Baranello
Cincinnati, OH, dTnpkmPwHeM, SAS Analytics U - Emily Baranello
Columbia, SC, fE_OT8fmfT0, SAS Tech Talk: SAS Programming Language Advances
San Francisco, CA, 6wlPAK-Mb_M, SAS Global Forum 2013 - Jim Goodnight Opening Address
Washington, DC, 3tFNkT_C6Nc, SAS Visual Analytics - An Overview
Tampa, FL, 3tFNkT_C6Nc, SAS Visual Analytics - An Overview
New York, NY, 3tFNkT_C6Nc, SAS Visual Analytics - An Overview
Irvine, CA, 3tFNkT_C6Nc, SAS Visual Analytics - An Overview
Chicago, IL, HJFWrwXGUz4, SAS Visual Analytics in the Cloud
Detroit, MI, HJFWrwXGUz4, SAS Visual Analytics in the Cloud
Helena, MT, SoEzywDrB4Y, Paul Kent: SAS' Approach to Big Data
Dallas, TX, c4Z8twzywN4, Armistead Sapp / SAS R&D Update
San Antonio, TX, nmRQ3MtkG6A, SAS/IML and R: Using Them Together
Albuquerque, NM, R0VElQ-vLuw, SAS Fraud & Security Solution 
;
run;

proc geocode CITY lookup=sashelp.zipcode data=my_data out=my_data;
run;

data my_data; set my_data (rename=(state=statecode));
state=stfips(statecode);
long=x; 
lat=y;
run;

data anno_markers; set my_data (where=(x^=. and y^=.));
anno_flag=1;
run;

/* Get the U.S. map */
data states;
 set mapsgfk.us_states (where=(density<=3 and statecode not in ('AK' 'HI')));
run;

/* combine, project, and separate */
data combined; set states anno_markers; run;
proc gproject data=combined out=combined latlong eastlong degrees dupok;
id state;
run;
data states anno_markers; set combined;
if anno_flag=1 then output anno_markers;
else output states;
run;

/*
Create annotate marker with chart-tip and video drilldown for each city
*/
data anno_markers; set anno_markers;
length function style color $8 text $20 html $1024;
xsys='2'; ysys='2'; hsys='3'; when='a';
function='move'; output;
/* 
note that html title= and href= are 'broken' in SAS 9.4m1 
(there's a hotfix for 9.4m1, and it's fixed in 9.4m2 - S1063735) 
*/
html= 
 'title='||quote(trim(left(video_title))||'0d'x||
   'is trending in '||trim(left(city))||', '||trim(left(statecode)))|| 
 ' href='||quote('https://www.youtube.com/watch?v='||trim(left(video_id)));
xsys='9'; ysys='9'; /* switch to relative coordinates */
function='move';  x=-2.5;  y=-2.5; output;
function='image'; x=2.5*2; y=2.5*2; 
 imgpath='thumbnails/'||trim(left(video_id))||'.png'; style='fit';
 output;
run;

/* Create an annotate dataset, where each of the states is just a gray
   filled polygon -- when='b' draws this behind/before the real map */
data shadow_anno; set states; 
by state segment notsorted;
length function color $8; 
xsys='2'; ysys='2'; when='B';
color="gray44";
style='msolid';
if first.state or first.segment then function='poly';
else function='polycont';
run;

/* Give a little x & y offset, so it will look like a shadow */
data shadow_anno; set shadow_anno;
x=x+.002; y=y-.002;
run;


goptions device=png;
goptions xpixels=800 ypixels=550;
goptions border;
 
ODS LISTING CLOSE;
ODS HTML path=odsout body="&name..htm"
 (title="Trending SAS Videos")
 style=htmlblue;

goptions gunit=pct htitle=7 htext=3.0 ftitle="albany amt/bold" ftext="albany amt";

title1 ls=1.5 "Trending SAS Videos";
title2 a=90 h=2 ' ';
title3 a=-90 h=2 ' ';

pattern v=s c=grayb4;

proc gmap data=states map=states anno=anno_markers;
note move=(10,5) c=gray55 "proof-of-concept";
id state; choro state / levels=1 nolegend
 coutline=graycc anno=shadow_anno
 des='' name="&name";
run;

quit;
ODS HTML CLOSE;
ODS LISTING;
