EZ Study

Actuarial Biology Chemistry Economics Calculators Confucius Engineer
Physics
C.S.


How to generate state/County output in SAS
How to Use Zipcode related function in SAS

Question: You may have zipcode data, and want to generate the output in the state or county level, do we need to join some mapping table to get the state or county?

We can use the following SAS function to get what we need,very convenient.

    ZIPCITY('02138') returns 'Cambridge, MA'
    ZIPSTATE('02138') returns 'MA'
    ZIPNAME('02138') returns 'MASSACHUSETTS'
    ZIPNAMEL('02138') returns 'Massachusetts'
    ZIPFIPS('02138') returns 25 (FIPS state code for Massachusetts)


There seems no county function; to help you get county information, you can actually do a quick join with sashelp.zipcode datasets on a.zip=b.zip (both should be numericals).
  Click to see larger picture
Most updated calfornia unemployment rate by county
 
    /******************************************************/
    PROC import OUT=WORK.ca1
    DATAFILE="directory\California_unemployment_rate_by_county.csv"

    DBMS=CSV REPLACE ; GETNAMES=yes; DATAROW=2;
    guessingrows=3125; quit;

    proc sql; create table ca2
    as select b.county as county,b.STATE as state, b.IDNAME as State_name,
    a.county as county_name,a.unemploy_rate as unemployment_rate, a.*
    from ca1 as a left join mapsgfk.us_counties_attr as b
    on compress(lowcase(a.county))=compress(lowcase(b.IDNAME))
    and b.ID1NAME='California' ;quit;

Note that we have used a SAS self-contained library mapsgfk, any gfk map data in that library contains both projected and unprojected latitude/longtitude coordinates.
    /******************************************************/
    goptions gunit=pct cback=white htitle=4 htext=3
    colors=(blue green grey yellow orange pupple red very_dark_red ) ;
    proc gmap map=mapsgfk.us_counties data=ca2;

    id state county;
    block unemploy_rate/ levels=8 blocksize=1.5 relzero;
    choro unemploy_rate/ levels=8 ; run; quit;

    /******************************************************/
    /* to generate the state level output */
    goptions gunit=pct cback=white htitle=4 htext=3
    colors=(blue GREEN GREY dark yellow orange pupple red ) ;

    proc sql; create table output_state3
    as select a.*,b.*
    from Output_state2 as a inner join mapsgfk.us as b
    on a.store_state=b.statecode ;quit;

    proc gmap map=maps.us data=output_state3 all;
    id state;
    block rr_15/levels=8;
    choro rr_15/levels=8; run; quit;


If you are using a dataset that doesn't contain projected latitude/longtitude coordinates, then you need to use the gproject procedure to generate the projected latitude/longtitude coordinates based on the unprojected latitude/longtitude coordinates you had. In other words, if you generate a map that doesn't look like the map we usuallly see, then it's possibly you didn't project the coordinates.

  Click to see larger picture
use gproject procedure to generate normal US map
 

    /*********Example of using gproject *******************/
    goptions reset=all border;
    data us48; set maps.states; if state ne 2 and state ne 15 and state ne 72; run;
    pattern value=mempty color=blue;
    proc gmap map=us48 data=us48 all density=4;
    id state; choro state / nolegend levels=1; run;

    proc gproject data=us48 out=us48proj; id state; run;
    proc gmap map=us48proj data=us48proj all density=4;
    id state; choro state / nolegend levels=1;
    run; quit;


There are several other ways to generate those geoinformatical maps, click next tutorial for unemployment rate output from google fusion table map.

Related links:

Continue to next: How to generate geographical output via google fusion table?   SAS tutorial home
Back to: How to get beautiful geographical picture in SAS?   Statistics tutorial home