%let name=path_on_map_sgplot; filename odsout '.'; /* An example requested by Darrin Brown, in his comment/question in one of my blogs: http://blogs.sas.com/content/sastraining/2017/03/29/plotting-markers-on-a-map-at-zip-code-locations-using-gmap-or-sgplot/ */ data locations; input lat long; data_flag=1; datalines; 28.4309148 -81.307914 28.8040591 -82.6837236 30.271513 -86.2855226 34.7985729 -80.6412966 35.8785112 -78.7877989 ; run; data my_map; set mapsgfk.us_states (where=(statecode not in ('AK' 'HI') and density<=3) drop=resolution); run; /* Sgplot polygons need every segment of every polygon to have a unique id. */ data my_map; set my_map; length statecode_plus_segment $10; statecode_plus_segment=trim(left(statecode))||'_'||trim(left(segment)); run; /* We combine the data & map to project them together, to guarantee that the projected x/y will line up right. (There is a way to project them separately, if you really want to do that...) */ data combined; set my_map locations; run; proc gproject data=combined out=combined latlong eastlong degrees; id statecode; run; data my_map locations; set combined; if data_flag=1 then output locations; else output my_map; run; /* I like splitting the map & data apart after projecting it. This gives a lot of flexibility, for example, if you want to annotate the data. But in this case, using an overlaid sgplot scatter, we will end up combining them again. I guess you 'could' save a little time by leaving them together, but I prefer working with the datasets split apart. */ /* sganno doesn't support hover-text tips yet, so let's use an overlaid scatter plot instead... */ data scatter_locations (rename=(x=dataX y=dataY)); set locations (keep = x y lat long); tip_text='= = = = ='||'0d'x||trim(left(lat))||', '||trim(left(long)); run; /* To overlay a scatter on a map, you must combine the two datasets. Make sure you don't have any variables named the same! */ data my_map; set my_map scatter_locations; run; goptions device=png; goptions border; ODS LISTING CLOSE; ODS HTML path=odsout body="&name..htm" (title="Lat/Long path overlaid on a sgplot") style=htmlblue; ods graphics on / imagename="&name" height=6.25in width=8.33in imagefmt=staticmap imagemap=on tipmax=2500; /* Calculate the aspect ratio of the points in the map */ proc sql noprint; select (max(y)-min(y))/(max(x)-min(x)) into :aspect from combined; quit; run; title1 ls=0.2 height=22pt c=gray33 'Lat/Long Path on a US Map'; title2 height=12pt c=gray33 "Using Proc SGplot, and overlaid series lines & scatter markers"; proc sgplot data=my_map noborder noautolegend aspect=&aspect; polygon x=x y=y id=statecode_plus_segment / fill outline tip=none lineattrs=(color=gray99) fillattrs=(color=cxe8edd5); series x=dataX y=dataY / lineattrs=(thickness=3 color=red); scatter x=dataX y=dataY / tip=(tip_text) filledoutlinemarkers markerattrs=(symbol=circlefilled size=13) markerfillattrs=(color=yellow) markeroutlineattrs=(color=purple); xaxis display=none; yaxis display=none; run; quit; ODS HTML CLOSE; ODS LISTING;