Data visualization with SAS programming

How to create a simple USA Map with dots

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 18
Accepted Solution

How to create a simple USA Map with dots

I've got a dataset named geocoded with latitude, longitutde, and color.    I'd like to just place colored dots at each X,Y coordinate on a US map using the color Color.

I know I have to use annotate and maybe gproject.  Any suggestions (code is good too : ) would be appreciated.


Accepted Solutions
Solution
‎03-20-2015 03:00 PM
SAS Employee
Posts: 170

Re: How to create a simple USA Map with dots


Here is a whole example so you can see MAPSGFK and GPROJECT:

data schools (label='Wake County, NC public schools'); 
  infile datalines dlm=',';
  length school $64 address $32 city $24          state $2 zip 5 type $12 color $10;
  input school address city state zip type;
  type = upcase(type);
  /* Set point color based on school type. */
  if      type = 'HIGH'       then color = 'CXff0000';
  else if type = 'MIDDLE'     then color = 'CX00ff00';
  else if type = 'ELEMENTARY' then color = 'CX0000ff';
  else delete; /* Drop multi-type schools */
datalines;
Adams Elementary, 805 Cary Towne Blvd., Cary, NC, 27511, Elementary
Apex High, 1501 Laura Duncan Road, Apex, NC, 27502, High
Apex Middle, 400 E. Moore Street, Apex, NC, 27502, Middle
Athens Drive High, 1420 Athens Drive, Raleigh, NC, 27606, High
;
run;
proc geocode method=street data=work.schools out=work.geocoded lookupstreet=sashelp.geoexm; run;
  %make_dots(anno,work.geocoded,color,1);
data anno; set anno; flag=1; run;
data us_states(drop=state); set mapsgfk.us_states;  run;
data us_states; set us_states; state=statecode; x=long; y=lat;
  if state="AK" or state="HI" then; else output; run;
data all; set us_states anno; run;
proc sort data=all out=allsorted; by state; run;
proc gproject data=allsorted out=annop project=albers degrees eastlong; id state; run;
data annodata usmap; set annop; if flag=1 then output annodata; else output usmap; run;
proc gmap data=usmap map=usmap anno=annodata;
id state;
choro state/  nolegend;
run; quit;

View solution in original post


All Replies
SAS Employee
Posts: 963

Re: How to create a simple USA Map with dots

I think this example will provide a good starting place for your code:

http://robslink.com/SAS/democd28/dotmap_info.htm

dotmap.png

Occasional Contributor
Posts: 18

Re: How to create a simple USA Map with dots

Hi. Thanks - I probably didn't express myself well.  What I'm trying to do is start with a US map.

Then I have a dataset of x,y,color.

I just want to plot each x,y (latitude, longitude) in the color on top of the US map.

The dataset are locations in the map and the colors represent different kinds of locations.

I reviewed the suggested applicaation, but it seemed a lot more complex than what I'm trying to do.  Here's what I did - taken mostly from examples.  geocode2 is the dataset containing x,y, and color.

data anno; set sasuser.geocoded2; length function $ 8 ; xsys='2'; ysys='2'; hsys='3'; when='A'; function = 'POINT'; segment=1; run;

proc gproject data=anno out=annop degree eastlong; id state; run;

proc gmap data=maps.us annotate=annop; id state; run; quit;

It didn't produce any map.  I wasn't sure what ID to use.

Grand Advisor
Posts: 16,411

Re: How to create a simple USA Map with dots

@BallardW example is what you're looking for, replace the ZIP table with your table of x/y locations.

Occasional Contributor
Posts: 18

Re: How to create a simple USA Map with dots

Here's what I have.   The code below is what I get when I replace things.  What that gives me is a US graph at an angle with State FIPS Code 1-10 10-20...   There don't seem to be any of the colored dots.

data anno;

set sasuser.geocoded2;

length function $ 8 ; xsys='2'; ysys='2'; hsys='3'; when='A'; function = 'POINT'; flag=1; run;

data all;

set maps.us anno;

run;

proc sort data=all out=allsorted;

by state;

run;

proc gproject data=allsorted out=annop degrees;

id state;

run;

data annodata usmap;

set annop;

if flag=1 then output annodata; else output usmap;

run;

proc gmap data=usmap map=usmap;

id state;

choro state/annotate=annodata;

run; quit;

Is this the right idea?  At least I'm getting a graphic map now.

Message was edited by: alan weiner Trying to make things more readable.

SAS Employee
Posts: 170

Re: How to create a simple USA Map with dots

I think this is the right idea.  MAPS.US is already projected.  Your points are probably not.  I would use MAPSGFK.US_STATES or something in MAPSGFK.

Lat/Long are not projected in MAPSGFK so you can set X=long; y=lat;

It is also in degrees rather than Radians.

I would also use this Macro for the annotate.  You can size the "points":

%macro make_dots(annodata, inputdata, color, size, tipvar);
data &annodata;
length function color $ 8 style $20 position $ 1 text $ 60 html $1024;
retain xsys ysys '2' hsys '3' when 'a' size .7;
set &inputdata;
/*optional arguments*/
%if (&size ^= ) %then %do; size=&size; %end;
/* All annotate points are 360-degree pies. */
function='pie';
rotate=360;
style='psolid';
color=&color;
/* optional tool tips */
%if (&tipvar ^= ) %then %do;
html= 'title=' || quote("&tipvar" ||'=' || trim(left(&tipvar)) );
%end;
output;
/* Draw an outline around each 'dot' */
color="gray55";
style='pempty';
output;
run;
%mend;

Grand Advisor
Posts: 9,748

Re: How to create a simple USA Map with dots

If you looked at the example I posted you may have seen some code involved that transforms Lat and Long from degrees to radians. There's also a bit involved in getting east/west values as positive negative to match the map dataset.

I suspect your log has a message about trying to display points outside of the map area.

Grand Advisor
Posts: 9,748

Re: How to create a simple USA Map with dots

Occasional Contributor
Posts: 18

Re: How to create a simple USA Map with dots

Thanks for all your help!    I now have a map with tiny dots (I'll use Daryll's macro to make them larger)  The original data file had city/state data.  I used geocode to get the lat/long from there.

I turned the geocoded dataset into an annotated set and then projected that with the degrees option, then merged it with the mapsgfk.us map.

Actually, I do have notes on the log that X is not on the graph as Ballard suggested.

Ballard - does one have to go through all those transformations in order to get the right coordinates?  or is that what the project is supposed to do for you?

There's light at the end of the tunnel Smiley Happy

Message was edited by: alan weiner

Grand Advisor
Posts: 9,748

Re: How to create a simple USA Map with dots

From the help:

By default, the GPROJECT procedure assumes that the units for the input

coordinate values are radians and that values for the horizontal coordinate

increase from east to west across the map. If your map coordinates are stored as

degrees of arc, use the DEGREE option in the PROC GPROJECT statement. If the

horizontal coordinate values in the map increase west-to-east rather than

east-to-west, use the EASTLONG option in the PROC GPROJECT statement.

Solution
‎03-20-2015 03:00 PM
SAS Employee
Posts: 170

Re: How to create a simple USA Map with dots


Here is a whole example so you can see MAPSGFK and GPROJECT:

data schools (label='Wake County, NC public schools'); 
  infile datalines dlm=',';
  length school $64 address $32 city $24          state $2 zip 5 type $12 color $10;
  input school address city state zip type;
  type = upcase(type);
  /* Set point color based on school type. */
  if      type = 'HIGH'       then color = 'CXff0000';
  else if type = 'MIDDLE'     then color = 'CX00ff00';
  else if type = 'ELEMENTARY' then color = 'CX0000ff';
  else delete; /* Drop multi-type schools */
datalines;
Adams Elementary, 805 Cary Towne Blvd., Cary, NC, 27511, Elementary
Apex High, 1501 Laura Duncan Road, Apex, NC, 27502, High
Apex Middle, 400 E. Moore Street, Apex, NC, 27502, Middle
Athens Drive High, 1420 Athens Drive, Raleigh, NC, 27606, High
;
run;
proc geocode method=street data=work.schools out=work.geocoded lookupstreet=sashelp.geoexm; run;
  %make_dots(anno,work.geocoded,color,1);
data anno; set anno; flag=1; run;
data us_states(drop=state); set mapsgfk.us_states;  run;
data us_states; set us_states; state=statecode; x=long; y=lat;
  if state="AK" or state="HI" then; else output; run;
data all; set us_states anno; run;
proc sort data=all out=allsorted; by state; run;
proc gproject data=allsorted out=annop project=albers degrees eastlong; id state; run;
data annodata usmap; set annop; if flag=1 then output annodata; else output usmap; run;
proc gmap data=usmap map=usmap anno=annodata;
id state;
choro state/  nolegend;
run; quit;

Post a Question
Discussion Stats
  • 10 replies
  • 801 views
  • 6 likes
  • 5 in conversation