Maps Online >Resources SAS logo
Feedback   

Sample Data
Sample Programs
Tools
Useful Links


/*******************************************************************\
| Copyright (C) 2002 by SAS Institute Inc., Cary, NC, USA.          |
|                                                                   |
| SAS (R) is a registered trademark of SAS Institute Inc.           |
|                                                                   |
| SAS Institute does not assume responsibility for the accuracy of  |
| any material presented in this file.                              |
\*******************************************************************/


/* Re-projecting US, USCITY and USCENTER Datasets */

/*******************************************************************
|                                                                 |
| This program is a revised version to be used with V8 datasets.  |
| The   code was used to reproject US, USCITY and USCENTER data   |
| sets from the STATES data set and the LONG/LAT variables in     |
| the USCITY and USCENTERS data sets.                             |
|                                                                 |
| It was necessary to re-project these datasets with the purchase |
| of new city data.                                               |
| PLEASE BE AWARE THAT THIS PROGRAM CANNOT BE USED ASIS. ANY NEW  |
| INFORMATION SHOULD FIRST BE PROCESSED OR UNNECESSARY STEPS      |
| DELETED.                                                        |
|                                                                 |
| This program uses datasets from SAS MAPS library and new city   |
| information from USCITY  dataset.In the end, this job produces  |
| three work data sets: US,USCITY, USCENTER and graphics output.  |
| In the mean time the following naming convention is used for    |
| work ds:                                                        |
|                                                                 |
| Prefix - Indicates geographic area                              |
|     US   - All 50 states                                        |
|     CONT - 48 states                                            |
|     AK   - Alaska                                               |
|     HI   - Hawaii                                               |
| Middle - Projection / reduction                                 |
|     U    - Unprojected                                          |
|     P    - Projected                                            |
|     R    - Projected and reduced                                |
|   blank  - Projected low density                                |
| Suffix - Indicates data's type(s)                               |
|     MAP  - Only map points                                      |
|     CTY  - Cities                                               |
|     CTR  - Centers                                              |
|     ALL  - Map, cities, and centers                             |
|                                                                 |
*******************************************************************/

data USUMAP;
set MAPS.STATES(where=(state ne 72));
FLAG = 1;
if STATE = 02 and
SEGMENT = 1 then output;
else if ((STATE = 25 | STATE = 36 | STATE = 53) and (
SEGMENT = 1)) then output;
else output;
run;

/*********** Process new city information **************************/

data USUCTY;
set maps.uscity(drop=x y);
D2R = atan(1) / 45;
retain D2R;
X = LONG * D2R;
Y = LAT  * D2R;
SEGMENT = 99;
FLAG = 2;
drop D2R;
run;

data USUCTR;
set MAPS.USCENTER(drop=x y);
D2R = atan(1) / 45;
retain D2R;
X = LONG * D2R;
Y = LAT  * D2R;
SEGMENT = 98;
FLAG = 3;
drop D2R;
run;

data USUALL;
set USUMAP USUCTY USUCTR;
run;

/*------------------ Seperate Alaska and Hawaii ---------------------*/

data CONTUALL AKUALL HIUALL;
set USUALL;
if STATE = 15 then output HIUALL;
else if STATE = 2 then  output AKUALL;
else output CONTUALL;
run;

/*-------------------------- Project -------------------------------*/

proc gproject data=CONTUALL out=CONTPALL;
id STATE;
run;

proc gproject data=AKUALL out=AKPALL;
id STATE;
run;

proc gproject data=HIUALL out=HIPALL;
id STATE;
run;

/*------------------ Determine Scaling Factors ---------------------*/

title 'Range of Alaska';
proc means noprint MIN MAX data=AKPALL;/* find range of proj. Alaska */
var X Y;
output out=AKMEAN min=XMIN YMIN max=XMAX YMAX;
run;

title 'Range of Hawaii';
proc means noprint MIN MAX data=HIPALL; /*find range of proj. Hawaii */
var X Y;
output out=HIMEAN min=XMIN YMIN max=XMAX YMAX;
run;

title 'Range of Continental US';
proc means noprint MIN MAX data=CONTPALL;
var X Y;
output out=CONTMEAN min=CXMIN CYMIN max=CXMAX CYMAX;
run;

/*------------------ Move Alaska and Hawaii -------------------------*/

data AKPALL; /* rescale Alaska to fit under California */
/* --------------------------------------------------------
Scale X to (CXMIN, CXMIN + 0.200 * (CXMAX - CXMIN))
Scale Y to (CYMIN, CYMIN + 0.285 * (CYMAX - CYMIN))
------------------------------------------------------- */
set AKPALL;
if _n_ = 1 then do;
set CONTMEAN;
FACT1 =  CXMIN + 0.200 * (CXMAX - CXMIN);
FACT2 =  CYMIN + 0.285 * (CYMAX - CYMIN);
retain FACT1 FACT2;
set AKMEAN;
AX = (FACT1 - CXMIN) / (XMAX - XMIN);
AY = (FACT2 - CYMIN) / (YMAX - YMIN);
BX = CXMIN - AX * XMIN;
BY = CYMIN - AY * YMIN;
retain AX BX AY BY;
end;
X = X * AX + BX;
Y = Y * AY + BY;
drop FACT1-FACT2 BX BY AX AY XMAX XMIN YMAX YMIN;
drop CXMAX CXMIN CYMAX CYMIN;
run;

data HIPALL; /* rescale Hawaii to fit under New Mexico */
/* ---------------------------------------------
Scale X to (CXMIN + 0.200 * (CXMAX - CXMIN),
CXMIN + 0.300 * (CXMAX - CXMIN))
Scale Y to (CYMIN, CYMIN + 0.143 * (CYMAX - CYMIN))
--------------------------------------------- */
set HIPALL;
if _n_ = 1 then do;
set CONTMEAN;
FACT1 =  CXMIN + 0.200 * (CXMAX - CXMIN);
FACT2 =  CXMIN + 0.300 * (CXMAX - CXMIN);
FACT3 =  CYMIN + 0.143 * (CYMAX - CYMIN) ;
retain FACT1 FACT2 FACT3;
set HIMEAN;
AX = (FACT2 - FACT1) / (XMAX - XMIN);
AY = (FACT3 - CYMIN) / (YMAX - YMIN);
BX = FACT1 - AX * XMIN;
BY = CYMIN - AY * YMIN;
retain AX BX AY BY;
end;
X = X * AX + BX;
Y = Y * AY + BY;
drop FACT1-FACT3 BX BY AX AY XMAX XMIN YMAX YMIN;
drop CXMAX CXMIN CYMAX CYMIN;
run;

/*-------------------- Separate Data Sets --------------------------*/

data AKPCTY AKPCTR AKPMAP;
set AKPALL;
if FLAG = 1 then output AKPMAP;
else if FLAG = 2 then output AKPCTY;
else output AKPCTR;
run;

data HIPCTY HIPCTR HIPMAP;
set HIPALL;
if FLAG = 1 then output HIPMAP;
else if FLAG = 2 then output HIPCTY;
else output HIPCTR;
run;

data CONTPCTY CONTPCTR CONTPMAP;
set CONTPALL;
if FLAG = 1 then output CONTPMAP;
else if FLAG = 2 then output CONTPCTY;
else output CONTPCTR;
run;

/*-------------------- Assign Densities ----------------------------*/

title 'Reducing Maps';
proc greduce data=HIPMAP out=HIRMAP n1=114;
id STATE;
run;

proc greduce data=AKPMAP out=AKRMAP n1=109;
id STATE;
run;

proc greduce data=CONTPMAP out=CONTRMAP n1=84;
id STATE;
run;

data USRMAP;
set CONTRMAP AKRMAP HIRMAP;
run;

/*-------------------- Strip Maps to Low Density -------------------*/

data HIMAP;
set HIRMAP;
if DENSITY <= 1;
run;

data AKMAP;
set AKRMAP;
if DENSITY <= 1;
run;

data CONTMAP;
set CONTRMAP;
if DENSITY <= 1;
run;

/*-------------------- Recombine States ----------------------------*/

data USMAP; /* add repostioned Alaska and Hawaii to US */
set CONTMAP AKMAP HIMAP;
run;

data USCTY; /* add repostioned Alaska and Hawaii to US */
set CONTPCTY AKPCTY HIPCTY;
run;

data USCTR;/* add repostioned Alaska and Hawaii to US */
set CONTPCTR AKPCTR HIPCTR;
run;

/*-------------------- Sort Data Sets by STATE ---------------------*/

proc sort data=USMAP out=USMAP;
by STATE;
run;

proc sort data=USCTY out=USCTY;
by STATE;
run;

proc sort data=USCTR out=USCTR;
by STATE descending OCEAN;
run;

/*----------------------- Look It Over -----------------------------*/

data US(label='Copyright(c) 1999 SAS Institute Inc.,USA');
set USMAP;
label X       = 'Projected Longitude in Radians';
label Y       = 'Projected Latitude in Radians';
label SEGMENT = 'State Segement Number';
label STATE   = 'State FIPS Code';
keep X Y SEGMENT STATE;
run;


data USCTY;                /* add temporary variables */
length COLOR $ 8;
set USCTY;
XSYS = '2';
YSYS = '2';
TEXT = '*';
COLOR = 'RED';
run;

data USCITY(label='Copyright(c) 1999 SAS Institute Inc.,USA');
set USCTY;
keep X Y LONG LAT CITY CNTYFIPS CITYFIPS STATE CAPITAL
ALT POP PLACE FEATYPE;
run;

data USCENTER(label='Copyright(c) 1999 SAS Institute Inc.,USA');
set USCTR;
label X       = 'Projected Longitude from Radians';
label Y       = 'Projected Latitude from Radians';
label LONG    = 'Unprojected Longitude in Degrees';
label LAT     = 'Unprojected Latitude in Degrees';
label CITY    = 'City Name';
label OCEAN   = 'Y/N';
label STATE   = 'State FIPS Code';
keep X Y LONG LAT CITY OCEAN STATE;
run;

title 'Contents of US Map';
proc contents data=US;
run;

title 'Contents of USCITY';
proc contents data=USCITY;
run;

title 'Contents of USCENTER';
proc contents data=USCENTER;
run;

/*---------------------- Test Data Sets --------------*/
DATA CENTERS(KEEP=state  city flag
XSYS YSYS WHEN FUNCTION TEXT X Y COLOR SIZE);
LENGTH FUNCTION COLOR $8 TEXT $25;
RETAIN
WHEN 'A' XSYS YSYS '2' ;
set uscity;
by state;
FLAG='C';
FUNCTION='LABEL';
SIZE=.65;
TEXT=city;
color='RED';
*   TEXT='*';
RUN;

DATA SYMBCTR(KEEP=state  city flag
XSYS YSYS WHEN FUNCTION TEXT X Y COLOR SIZE);
LENGTH FUNCTION COLOR $8 TEXT $25;
RETAIN
WHEN 'A' XSYS YSYS '2' ;
set uscity;
by state;
FLAG='C';
FUNCTION='LABEL';
SIZE=.65;
TEXT='*';
color='RED';
RUN;

%macro show2(dsn,note,st,anno);
goptions colors=(black graycc);

PROC GMAP
%if &st ne %str( ) %then %do;
DATA=&dsn(where=(state in(&st) ) )
map=&dsn(where=(state in(&st ) ))
ANNO=&anno;
%end;
%else %do;
DATA=&dsn
map=&dsn
ANNO=&anno;
%end;
ID state ;
CHORO state / DISCRETE;
PATTERN1 V=e r=100;
title; footnote "¬e";
run;
%mend;
%show2(%str(maps.us),%str(maps.us using text),17,
%str(centers(where=(state in(17) and city='Chicago'))));

%show2(%str(maps.us),%str(maps.us using symbols),17,
%str(symbctr(where=(state in(17) and city='Chicago'))));

%show2(%str(work.us),%str(work.us reduced using text),17,
%str(centers(where=(state in(17) and city='Chicago'))));

%show2(%str(work.us),%str(work.us reduced using symbols),17,
%str(symbctr(where=(state in(17) and city='Chicago'))));

%show2(%str(work.usrmap),%str(work.us unreduced using text),17,
%str(centers(where=(state in(17)))));

%show2(%str(work.usrmap),%str(work.us unreduced using symbols),17,
%str(symbctr(where=(state in(17)))));

%show2(%str(work.us),%str(work.us using text),%str(29),
%str(centers(where=(state in(29)))));

%show2(%str(work.us),%str(work.us using symbols),%str(29),
%str(symbctr(where=(state in(29)))));

%show2(%str(work.us),%str(work.us using text),%str(29),
%str(centers(where=(state in(29)
and city in:('Maryvil' 'St.Lou' 'Kansas') ))));

%show2(%str(work.us),%str(work.us using symbols),%str(29),
%str(symbctr(where=(state in(29)
and city in:('Maryvil' 'St.Lou' 'Kansas') ))));

%show2(%str(work.us),%str(work.us using text),,centers);

%show2(%str(work.us),%str(work.us using symbols),,symbctr);