The sample code on the Full Code tab uses the Annotate facility to place a point on a map at the location of specific ZIP codes. In order to make the map easier to read, Alaska, Hawaii, and Puerto Rico have been moved from their geographic locations to be closer to the continental U.S. map.
Note that since the U.S. Postal Service changes ZIP codes frequently, your ZIP code might not be found in the SASHELP.ZIPCODE data set. You can download updated versions of the SASHELP.ZIPCODE data set from the Maps Online web site.
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
/* Set the graphics environment */
goptions reset=all border;
/* Create a data set containing ZIP codes */
data myzip;
input zip;
datalines;
00602
35004
85003
71601
80002
6001
19701
20001
32007
83201
60001
46001
50001
66002
40003
70001
3901
20601
1001
27513
96701
99501
;
run;
/* Sort the data set by ZIP codes */
proc sort data=myzip;
by zip;
run;
/* Create a data set containing the */
/* X and Y values for my ZIP codes */
data longlat;
merge work.myzip(in=mine) sashelp.zipcode(rename=(x=long y=lat));
by zip;
/* Keep if the ZIP code was in my data set */
if mine;
/* Convert longitude and latitude in degrees to radians */
/* to match the values in the map data set */
x=atan(1)/45*long;
y=atan(1)/45*lat;
/* Adjust the hemisphere */
x=-x;
/* Keep only the ZIP, X, Y, and STATE variables */
keep zip x y state;
length state 5.;
run;
/* Create an annotate data set to place a symbol */
/* at ZIP code locations. */
data anno;
/* Use the X and Y values from the LONGLAT data set */
set longlat;
/* Set the data value coordinate system */
/* Set the function to LABEL */
/* Set the size of the symbol to .75 */
/* Set a FLAG variable to signal annotate observations */
retain xsys ysys '2' function 'label' size .75 flag 1 when 'a';
/* Set the font to the Special font */
style='special';
/* The symbol is a star */
text='M';
/* Color for the symbol */
color='depk';
/* Output the observation to place the symbol */
output;
run;
/* Since we want to move Alaska, Hawaii, and Puerto Rico under */
/* the continental U.S., we need to separate their coordinates */
/* so they can be projected separately. */
data states alaska hawaii pr;
set maps.states;
select;
when (state=2) output alaska;
when (state=15) output hawaii;
when (state=72) output pr;
otherwise output states;
end;
run;
/* Separate the annotate data for the continental U.S., */
/* Alaska, Hawaii, and Puerto Rico to be projected with */
/* their state coordinates. */
data stateszip alzip hizip przip;
set anno;
select;
when (state=2) output alzip;
when (state=15) output hizip;
when (state=72) output przip;
otherwise output stateszip;
end;
run;
/* Combine the map and annotate data sets */
/* for the contiguous states. */
data states;
set states stateszip;
run;
/* Project the data */
proc gproject data=states out=stproj;
id state;
run;
/* Combine the Alaska map and annotate data */
data alaska;
set alaska alzip;
run;
/* Project Alaska so it will appear full size under the U.S. */
proc gproject data=alaska out=akproj;
id state;
run;
/* Combine the Hawaii map and annotate data */
data hawaii;
set hawaii hizip;
run;
/* Project Hawaii */
proc gproject data=hawaii out=hiproj;
id state;
run;
/* Combine the Puerto Rico map and annotate data */
data pr;
set pr przip;
run;
/* Project Puerto Rico */
proc gproject data=pr out=prproj;
id state;
run;
/* Adjust the coordinates for Alaska to move */
/* it under the continental U.S. */
data akproj(drop=x y);
set akproj;
if density < 2;
newx=(x-.55)*.50;
newy=(y-.35)*.50;
run;
/* Adjust the coordinates for Hawaii to move */
/* under the continental U.S. */
data hiproj(drop=x y);
set hiproj;
newx=x-.12;
newy=y-.20;
run;
/* Adjust the coordinates for Puerto Rico */
data prproj(drop=x y);
set prproj;
newx=(x+.315);
newy=(y-.24);
run;
/* Combine all of the projected data sets */
data all;
set stproj akproj(rename=(newx=x newy=y))
hiproj(rename=(newx=x newy=y))
prproj(rename=(newx=x newy=y));
run;
/* Separate the projected data set into a map and an annotate data set */
data map dot;
set all;
/* If the FLAG variable has a value of 1, it is an annotate data */
/* set observation; otherwise, it is a map data set observation. */
if flag=1 then output dot;
else output map;
run;
/* Define the pattern for the map */
pattern1 v=me c=grp r=50;
/* Define the title for the map. */
title1 'ZIP Code locations on a US Map';
title2 'moving Alaska, Hawaii and Puerto Rico';
/* Generate the map and place the symbols at ZIP code locations */
proc gmap data=longlat map=map all;
id state;
choro state / anno=dot nolegend;
run;
quit;
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
Type: | Sample |
Topic: | SAS Reference ==> Procedures ==> GMAP Query and Reporting ==> Creating Reports ==> Graphical ==> Graph Elements ==> Annotation |
Date Modified: | 2011-02-28 13:40:43 |
Date Created: | 2011-02-17 09:19:35 |
Product Family | Product | Host | SAS Release | |
Starting | Ending | |||
SAS System | SAS/GRAPH | Microsoft® Windows® for 64-Bit Itanium-based Systems | 9.1 TS1M3 | |
z/OS | 9.1 TS1M3 | |||
Microsoft Windows Server 2003 Datacenter 64-bit Edition | 9.1 TS1M3 | |||
Microsoft Windows Server 2003 Enterprise 64-bit Edition | 9.1 TS1M3 | |||
Microsoft Windows XP 64-bit Edition | 9.1 TS1M3 | |||
Microsoft Windows 2000 Advanced Server | 9.1 TS1M3 | |||
Microsoft Windows 2000 Datacenter Server | 9.1 TS1M3 | |||
Microsoft Windows 2000 Server | 9.1 TS1M3 | |||
Microsoft Windows 2000 Professional | 9.1 TS1M3 | |||
Microsoft Windows NT Workstation | 9.1 TS1M3 | |||
Microsoft Windows Server 2003 Datacenter Edition | 9.1 TS1M3 | |||
Microsoft Windows Server 2003 Enterprise Edition | 9.1 TS1M3 | |||
Microsoft Windows Server 2003 Standard Edition | 9.1 TS1M3 | |||
Microsoft Windows XP Professional | 9.1 TS1M3 | |||
Windows Vista | 9.1 TS1M3 | |||
Windows Vista for x64 | 9.1 TS1M3 | |||
64-bit Enabled AIX | 9.1 TS1M3 | |||
64-bit Enabled HP-UX | 9.1 TS1M3 | |||
64-bit Enabled Solaris | 9.1 TS1M3 | |||
HP-UX IPF | 9.1 TS1M3 | |||
Linux | 9.1 TS1M3 | |||
OpenVMS Alpha | 9.1 TS1M3 | |||
Tru64 UNIX | 9.1 TS1M3 |