To demonstrate the process of mapping from SAS University Edition through Highcharts we will create a simple choropleth map displaying Gross Value Added (GVA) per head in each of the four constituent countries of the United Kingdom. GVA is a measurement of economic production linked to Gross Domestic Product (GDP). The map we will be using is the United Kingdom countries map from the maps collection page of the Highcharts web site.
Assuming we already have the data that we want to load into the map in a SAS Data Set we need to ensure that we have a variable that can link the geographical identifier in the Data Set with the geographical identifier in the map. We will achieve this by creating a custom format to apply to the Data Set. You can get the map identifiers for the supplied maps by following these steps:
Figure 1 - Maps Collection
Now that we have the identifiers we will create a custom format which we will apply to the geographical field in the data set. Here is the SAS code for the format we will use for the UK map (saved to the work library for convenience – in practice you may want to save this to a permanent formats library). This will achieve the desired link with the map.
proc format; value $gbcountries 'England'='gb-eng' 'Wales' = 'gb-wls' 'Scotland' = 'gb-sct' 'N Ireland' = 'gb-nir'; run;
Now that we have our format we can create the data for the map:
data countryGVA2015; length country $10; format country gbcountries.; infile datalines dlm=','; input country $ gva; datalines; England,26159 Wales,18002 Scotland,23685 N Ireland,18584 ; run;
filename gva "/folders/myshortcuts/Dropbox/SAS and Highmaps/countrygva2015.json"; proc json out=gva pretty; export countryGVA2015 / nosastags nokeys fmtcharacter; run;
In this call to Proc JSON the “pretty” option simply formats the output so it is easily readable, the “nosastags” and “nokeys” options suppress the output of SAS metadata and variable names in the output file. The “fmtcharacter” option ensures that the formatted value of any character variable is output to the file.
We are now ready to start drawing the map – the first thing we need to do is to declare some filerefs and then decide whether we want the colour scale to start at zero. In this case, all the values to be mapped are close in magnitude so if we start at zero it is likely that the colours will be close in value making it difficult to distinguish the differences. Therefore, we will get the smallest value in the file and round it down (in this case) to the nearest thousand and set that value to be the minimum of the scale.
filename myfile "/folders/myshortcuts/Dropbox/SAS and Highmaps/ukgva.html"; filename init "/folders/myshortcuts/Dropbox/SAS and Highmaps/Core/init.html"; filename term "/folders/myshortcuts/Dropbox/SAS and Highmaps/Core/term.html"; proc sql noprint; select min(gva) into :min from countryGVA2015; quit; %let min=%sysfunc(round(&min-500,1000));
We will now create a few macro variables for the values we want to substitute in the init.html and term.html files. These are self-explanatory except perhaps for mapfile which corresponds to the map file name in the Highmaps collection.
%let mapfile=countries/gb/custom/gb-countries; %let titletag=UK GVA; %let title=UK GVA Per Head 2015; %let subtitle=United Kingdom;
Finally, we will run Proc Stream which, as its name implies, processes an input stream of text performing macro substitution and executing any SAS macros it finds in the text.
proc stream outfile=myfile prescol resetdelim='label' resetdelim="goto"; begin goto; %include init; goto newline; goto; %include gva; goto newline; goto; %include term; </html> ;;;; run;
Essentially the three files we have created are run through the procedure one at a time performing macro substitution and stitching them together into one single file. All the files required are attached to this article (init.html and term.html have been renamed with .txt extensions for upload purposes).
The Proc Stream code we have run will generate an HTML file which you can open in a browser. A static image of it is in Figure 3 below. Here is a list of the features you get with this map:
Figure 3 - Final Map
If you have any features you would like to see demonstrated, any questions or any examples of maps you would like replicated please leave the details in the comments section below. I would love to hear from you!