BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SciFiGuy0
Obsidian | Level 7

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Darrell_sas
SAS Employee


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

10 REPLIES 10
GraphGuy
Meteorite | Level 14

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

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

dotmap.png

SciFiGuy0
Obsidian | Level 7

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.

Reeza
Super User

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

SciFiGuy0
Obsidian | Level 7

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.

Darrell_sas
SAS Employee

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;

ballardw
Super User

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.

SciFiGuy0
Obsidian | Level 7

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

ballardw
Super User

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.

Darrell_sas
SAS Employee


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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 10 replies
  • 3295 views
  • 6 likes
  • 5 in conversation