Published on :
Mapping SASHELP_AND_INTERNAL_CREATION

Creating a Gradient Map of the United States

This code is also available in: Deutsch Français
Awaiting validation
The section begins by converting the X (longitude) and Y (latitude) variables from radians to degrees, to align them with the projection types expected by the CHOROMAP procedure. Next, plot data is created by filtering to include only the 48 continental states and collecting the FIPS (Federal Information Processing Standards) codes of the states. A global title and a footnote are assigned to contextualize the graph. Finally, the SGMAP procedure is executed, using a states map dataset, U.S. Census Bureau 2010 population response data (here simulated by scoreperstate), and the plot dataset. The ESRIMAP statement is used to create a base map, CHOROMAP to plot state populations with a color gradient, and TEXT to assign state names. A gradient legend is added with GRADLEGEND for better interpretation of average credit scores.
Data Analysis

Type : SASHELP_AND_INTERNAL_CREATION


The examples use the SASHELP.MAPS libraries (MAPS.STATES, MAPS.USCENTER) and an internal 'scoreperstate' dataset created via a DATA step for example autonomy.

1 Code Block
DATA STEP Data
Explanation :
Creation of a dummy dataset 'work.scoreperstate' to simulate average credit scores by state, necessary to make the example autonomous. State FIPS codes are used as identifiers.
Copied!
1DATA work.scoreperstate;
2 LENGTH state_fips $2.;
3 INPUT state_fips $ _Score_ numeric;
4 DATALINES;
501 700
602 650
704 720
805 680
906 750
1008 690
1109 710
1210 730
1311 670
1412 700
1513 740
1616 680
1717 720
1818 700
1919 690
2020 730
2121 710
2222 700
2323 680
2424 720
2525 710
2626 700
2727 690
2828 730
2929 700
3030 680
3131 720
3232 710
3333 700
3434 690
3535 730
3636 710
3737 700
3838 680
3939 720
4040 710
4141 700
4242 690
4344 730
4445 710
4546 700
4647 680
4748 720
4849 710
4950 700
5051 690
5153 730
5254 710
5355 700
5456 680
55;
56RUN;
2 Code Block
DATA STEP
Explanation :
This step converts the longitude (X) and latitude (Y) variables from the MAPS.STATES dataset from radians to degrees, which is necessary to align the projection types. States 2 (Alaska), 15 (Hawaii), and 72 (Puerto Rico) are excluded.
Copied!
1DATA states;
2 SET maps.states;
3 IF state ^in(2,15,72); /* Exclut l'Alaska, Hawaï et Porto Rico */
4 x = -x * 45/atan(1); /* Conversion de la longitude en degrés */
5 y = y * 45/atan(1); /* Conversion de la latitude en degrés */
6RUN;
3 Code Block
DATA STEP
Explanation :
This DATA step creates the 'plot_data' dataset from MAPS.USCENTER, filtering the lower 48 states and converting the longitude. It also generates the state name from the FIPS code and a `state_fips` formatted for joining with score data.
Copied!
1DATA plot_data;
2 SET maps.uscenter;
3 IF state ^in(2,15,72) and ocean^='Y';
4 long = -long;
5 statename = fipstate(state); /* Récupère le code FIPS de l'état */
6 state_fips = put(state, z2.); /* Crée une variable state_fips à partir de 'state' pour la jointure */
7RUN;
4 Code Block
Global Statements
Explanation :
Uses global TITLE and FOOTNOTE statements to define the main graph title and an explanatory footnote.
Copied!
1title 'Average Credit Score in Each State';
2footnote4 'Map only includes the lower 48 states in the United States';
3 
5 Code Block
PROC SGMAP
Explanation :
Executes the SGMAP procedure to generate the graph. It uses the 'states', 'scoreperstate', and 'plot_data' datasets. ESRIMAP defines the base map, CHOROMAP plots average credit scores by state with a color gradient, and TEXT adds state names. GRADLEGEND provides a legend for the color gradient.
Copied!
1PROC SGMAP mapdata=work.states
2 maprespdata=work.scoreperstate /* Utilisation du jeu de données autonome */
3 plotdata=work.plot_data;
4 
5 esrimap
6 url='http://services.arcgisonline.com/arcgis/rest/services/Canvas/World_Light_Gray_Base';
7 
8 choromap _Score_ / mapid=state id=state_fips /* Utilisation de state_fips pour la jointure */
9 density=1
10 numlevels=4 leveltype=none
11 colormodel=( sty greenyellow deepskyblue cornflowerblue beige)
12 name='choro';
13 
14 text x=long y=lat text=statename /
15 textattrs=(size=6pt);
16 gradlegend 'choro' / title='Average Credit Score'
17 extractscale;
18 
19RUN;
20 
21QUIT;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.