%let name=crabtree_ods;

libname here '.';

/*
Run crabtree_ods.sas first, which writes .htm and .gif files to crabtree_maps/ folder,
and *then* run this crabtree_stores.sas, which writes .gif (gifanim) files to the
same folder, overwriting the non-animated gifs.

sas crabtree_build.sas
sas crabtree_categories.sas
sas crabtree_ods.sas
sas crabtree_stores.sas
*/


/*
This is a bit tricky ...
Since dev=gifanim does not work with ODS, I cannot create my html pages 
(with html= charttips and drilldowns) when I create my gifanim files.
But, if I create the same maps, with dev=gif, then I can use the html
files to point to the gifanim gif's (rather than the regular gifs)
and hopefully have the best of both worlds - the html files *and* 
the animated gifs!

Run crabtree_ods.sas, which writes .htm and .gif files to crabtree_maps/ folder,
and *then* run crabtree_stores.sas, which writes .gif (gifanim) files to the
same folder, overwriting the non-animated gifs.
*/


%macro do_map(spacenum);

/*
You need a variable you can do "by" processing with, in the 
map= data set, the data= data set, and the anno= data set
therefore I'm duplicating the data, and adding a "by_var" variable,
and then combining into 1 data set again
*/

data mydata; set here.crabtree_map;
run;

/* And this one's for the anno= annotated stars */
proc sql;
create table interest as select * 
 from here.crabtree_stores where trim(left(upcase(spacenum))) eq trim(left(upcase("&spacenum")));
/* can't use trim() in this situation, but the 'separated by' will trim the leading/trailing blanks */
select unique store into :store separated by ""
 from here.crabtree_stores where trim(left(upcase(spacenum))) eq trim(left(upcase("&spacenum")));
quit; run;


 filename odsout './crabtree_maps/';
 GOPTIONS DEVICE=gif;
 ODS LISTING CLOSE;
 ODS HTML path=odsout body="&spacenum..htm" 
   (title="Virtual Crabtree Valley Mall map (SAS Prototype)") style=minimal;

goptions xpixels=900 ypixels=860;
goptions ftitle="arial/bold" ftext="arial/bold" htitle=4pct htext=2.5pct;
goptions cback=white;

options nobyline;
pattern1 v=s c=white;

title1 link="http://www.crabtree-valley-mall.com" "Crabtree Valley Mall";
title2 font='wingdings 2' color=red 'ea'x font="arial/bold" color=black " = &store";

proc gmap data=mydata map=here.crabtree_map anno=here.crabtree_pic; 
 id idnum; 
 choro idnum / nolegend anno=interest coutline=white 
  des='' name="map_&spacenum"; 
run;
/*
I stuck the 'map_' in front of the gif name, because ods doesn't create 
gifs that start with a number, and prefixes them with an underscore
(such as _104.gif) whereas the old-school way I'm creating the gifanims
creates it as 104.gif, and then my html wouldn't point to the right name.
By making all the gifs start with a character ('map_') now I'm guaranteed
that both techniques will use the same name, with no auto-switching to
prepend an '_' on some names.
*/


 quit;
 ODS _all_ CLOSE;

%mend;



/*
Some stores are in multiple categories, so get rid of duplicates.
Otherwise if there's a duplicate, and the name is exactly 8 characters,
it will truncate off the last character, and and then add a numeric digit,
which will quite likely make it exactly-match another name and create
confusing mis-matches between the html files created by this job,
and the gif files created by the gifanim job.
Just take my word for it - that is *bad*! :)
*/
proc sql noprint;
create table loopdata as
select unique 
 trim(left(lowcase(spacenum))) as spacenum, 
 translate(store,"b4"x,"'") as store
from here.crabtree_stores
where spacenum^=''
order by category, store;
quit; run;


/*
Loop through all the stores, and create the gif file and html overlay (.htm page) 
for each one (note that this gif file created here will be overwritten by the 
gifanim .gif file created by crabtree_stores.sas,
and you'll want to keep the code used to generate the maps *exactly* in sync
between this job (crabtree_ods.sas) and crabtree_stores.sas, so that the
html hotspots will be in the right location.
*/
data _null_;
 set loopdata;
   call execute('%do_map('|| spacenum ||');');
run;
quit;




/*--------------------------------------------------------------------*/


/*
Now, do a table with drilldowns.
You don't really utilize this crabtree_ods.htm table,
but it's useful for checking things as you go...
*/

proc sql noprint;
create table loopdata as
select unique 
 cat, category,
 trim(left(lowcase(spacenum))) as spacenum, 
 translate(store,"b4"x,"'") as store
from here.crabtree_stores
where spacenum^=''
order by category, store;
quit; run;


filename odsout '.';
ODS LISTING CLOSE;
ODS HTML path=odsout body="&name..htm" 
 (title="Virtual Crabtree Valley Mall map (SAS Prototype)") style=minimal;

/* Print a table at the top of the page, with drilldowns to the anchor for each category,
   or the individual map for each store */
data tabldata; set loopdata;
length link $ 300;
link='crabtree_maps/'||trim(left(cat))||'.htm';
length link2 $ 300;
link2='crabtree_maps/'||trim(left(spacenum))||'.htm';
run;
title;
data tabldata; set tabldata;
 length link $300 href $300;
 label link='Select a Category to Map...';
 href = 'href="' || trim(left(link)) || '"';
 link = '<a ' || trim(href) || '>' || htmlencode(trim(category)) || '</a>';

 length link2 $300 href $300;
 label link2='or    Select a Store to Map...';
 href = 'href="' || trim(left(link2)) || '"';
 link2 = '<a ' || trim(href) || '>' || htmlencode(trim(store)) || '</a>';
run;
options nocenter;
proc report data=tabldata;
column link link2;
define link / group;
run;

quit;
ODS _ALL_ CLOSE;

