IPUMS-CPS collects the voter turnout information for each quadrennial presidential election and mid-term election. I used respondents' feedback for the question whether he/she voted in the 2008 presidential election, and their residence information, to visualize the state turnout rates for 2008 election.
Before the data visualization by PROC GMAP, I should firstly input the data and compute the turnout rates by state. DATA step and PROC MEANS were used as follows:
%let path=...;
libname mylab "&path";
filename ASCIIDAT "&path/cps.dat";
Then, the output dataset "turnout_rates_by_state" included two variables: the state FIPS code, and the turnout rate, that could be used for shown in the US map. If we simply used PROC GMAP without additional option settings, the graph might look like this, which was not really beautiful. I added two more important settings, the customized color and printing out turnout rate on each state. SAS Macros were employed so that this program could be packaged and called directly in the future. The SAS code is as follows:
%macro turnout_map;
%local i colors colorscount;
%let colors = cb0020 df4949 ef9277 f5c0a9 f6e4dd e0ebf1 b3d5e6 82bbd8 4196c7 0471b0;
%let colorscount = %sysfunc(countw(&colors.));
libname mylab "...";
data turnout_rates_by_state;
set mylab.turnout_rates_by_state;
format turnout_rate percent7.2;
run;
data mapanno1;
length function $8 text $16 size 8;
retain xsys ysys '2' hsys '3' when 'a' style "'Albany AMT'";
merge turnout_rates_by_state maps.uscenter( where = ( fipstate( state ) ne 'PR' ) );
by state;
text = catx( " / ", fipstate(state), strip(putn(turnout_rate,'percent7.2')));
lagocean = lag( ocean );
size = 1.5;
if ocean = 'Y' then do;
function = 'label';
position = '6';
output;
function = 'move';
output;
end;
position = '5';
if ocean = 'N' then do;
if lagocean = 'Y' then do;
function = 'draw';
size = round( 1.5 / 4, .01 );
end;
else do;
function = 'label';
position = '2';
text = fipstate(state);
output;
position = '5';
text = putn(turnout_rate, 'percent7.2' );
end;
end;
output;
run;
options nodate nonumber orientation = landscape;
%do i = 1 %to &colorscount.;
pattern&i. v = s c = cx%scan(&colors., &i.);
%end;
legend1 label = (position = top j = l 'Turnout Rate') shape = bar(.1in, .1in);
title ls = 1.5 "State Turnout Rates (2008 Election)";
ods listing close;
ods pdf file = '...\turnout_map.pdf' notoc;
proc gmap data = turnout_rates_by_state map = maps.us ( where = ( fipstate(state) ne 'PR' ) ) all;
id state;
choro turnout_rate / anno = mapanno1 legend = legend1 levels = &colorscount.;
run;
ods pdf close;
ods listing;
%mend;
Where the data "mapanno1" was constructed to setup the state postal codes and turnout rates shown on the map. And I defined the customized colors in the global variables "colorscount". Then PROC GMAP could generate the state turnout rates map for 2008 election, and a pdf file would be exported by "ods pdf".
Before the data visualization by PROC GMAP, I should firstly input the data and compute the turnout rates by state. DATA step and PROC MEANS were used as follows:
%let path=...;
libname mylab "&path";
filename ASCIIDAT "&path/cps.dat";
data mylab.cps;
infile ASCIIDAT pad missover lrecl=14;
input
STATEFIP 1-2
WTSUPP 3-12 .4
VOTEREG 13-14
;
run;
data vote08 (rename = (STATEFIP = state));
set mylab.cps;
if VOTEREG = 99 then delete;
else if VOTEREG = 2 then vote_indicator = 1;
else vote_indicator = 0;
run;
proc sort data = vote08;
by state;
proc means data = vote08 noprint;
by state;
var vote_indicator;
weight WTSUPP;
output out = turnout_rates_by_state mean(vote_indicator) = turnout_rate;
run;
Then, the output dataset "turnout_rates_by_state" included two variables: the state FIPS code, and the turnout rate, that could be used for shown in the US map. If we simply used PROC GMAP without additional option settings, the graph might look like this, which was not really beautiful. I added two more important settings, the customized color and printing out turnout rate on each state. SAS Macros were employed so that this program could be packaged and called directly in the future. The SAS code is as follows:
%macro turnout_map;
%local i colors colorscount;
%let colors = cb0020 df4949 ef9277 f5c0a9 f6e4dd e0ebf1 b3d5e6 82bbd8 4196c7 0471b0;
%let colorscount = %sysfunc(countw(&colors.));
libname mylab "...";
data turnout_rates_by_state;
set mylab.turnout_rates_by_state;
format turnout_rate percent7.2;
run;
data mapanno1;
length function $8 text $16 size 8;
retain xsys ysys '2' hsys '3' when 'a' style "'Albany AMT'";
merge turnout_rates_by_state maps.uscenter( where = ( fipstate( state ) ne 'PR' ) );
by state;
text = catx( " / ", fipstate(state), strip(putn(turnout_rate,'percent7.2')));
lagocean = lag( ocean );
size = 1.5;
if ocean = 'Y' then do;
function = 'label';
position = '6';
output;
function = 'move';
output;
end;
position = '5';
if ocean = 'N' then do;
if lagocean = 'Y' then do;
function = 'draw';
size = round( 1.5 / 4, .01 );
end;
else do;
function = 'label';
position = '2';
text = fipstate(state);
output;
position = '5';
text = putn(turnout_rate, 'percent7.2' );
end;
end;
output;
run;
options nodate nonumber orientation = landscape;
%do i = 1 %to &colorscount.;
pattern&i. v = s c = cx%scan(&colors., &i.);
%end;
legend1 label = (position = top j = l 'Turnout Rate') shape = bar(.1in, .1in);
title ls = 1.5 "State Turnout Rates (2008 Election)";
ods listing close;
ods pdf file = '...\turnout_map.pdf' notoc;
proc gmap data = turnout_rates_by_state map = maps.us ( where = ( fipstate(state) ne 'PR' ) ) all;
id state;
choro turnout_rate / anno = mapanno1 legend = legend1 levels = &colorscount.;
run;
ods pdf close;
ods listing;
%mend;
Where the data "mapanno1" was constructed to setup the state postal codes and turnout rates shown on the map. And I defined the customized colors in the global variables "colorscount". Then PROC GMAP could generate the state turnout rates map for 2008 election, and a pdf file would be exported by "ods pdf".
Hi Jason,
ReplyDeleteThank you for the tutorial. I am trying to use SAS Maps to show substance use rates in the US. I do not have data for all states, and when I try to graph my map , there are state missing from the image. I was wondering why this happens?