x cd /pub/data/sf12000/Tools; %let pgm=agersexh; filename pgm "&pgm..sas"; *---This program will read a pair of sf1 (census 2000) "pct" sas data sets, as converted by the cnvtsf1.sas setup in this directory, and generate a 1-obs-per-non-zero-data-cell alternate version of the very detailed demographic data found in the pct12 table of the pct data set and the pct12 tables of the pct12r data set. Includes sample proc tabulate steps illustrating relative ease of generating complex custom tabulations. This module makes use of a number of SAS format codes to provide names of geographic entities - specifically, counties, places and county subdivisons (mcd's). The format codes are called $county, $mofplace and $mofmcd. These modules were created by the sas program that can be found at https://mcdc.missouri.edu/sastools/sas/formats/Tools/genmocodes.sas . This module in turn assumes access to a 2000 public law 94 data set with geographic codes for a state. Such SAS data sets can be accessed (and transferred to your local site as transport format SAS data sets) using the uexplore/xtract software avaialable at https://mcdc.missouri.edu/applications/uexplore.php?/data//pl942000@secure . See https://mcdc.missouri.edu/sastools/sasnotes.html for a general discussion of using SAS software at this site. It will explain, for example, how you can get access to the %latlond macro that is utilized in one of the sample steps at the end (which gives you the ability to calculate distances between 2 pts with latitude longitude coords in decimal degrees, and thus to select geographic areas within n miles of the center point of a city.) John Blodgett, OSEDA, U. of Missouri Outreach & Extension, blodgettj@umsystem.edu. Under contract with the Missouri Census Data Center. July, 2001. ---*; *-------Revision history: 6-03-01: Begin coding. 6-10-01: Ready for public. --------*; title 'MCDC SF1 Processing Run agersexh.sas: Creates Alternative Microdata View of PCT12 Data'; libname sf1 '/pub/data/sf12000'; libname user '/SAS_temp/user'; *---What we are creating here is a kind of microdata set that can be used to create tables as you like using tools like proc tabulate. We output table cells with dimensions: -Geography: 080 tract within place and mcd is microdata value. So data can be aggregated to county and to place and/or county subdivision (MCD) within county. Internal point lat-long coordinates are kept to allow selecting and aggregating based on distances (see sample below). This is the smallest summary level for which the PCT data are available (otherwise we might go all the way to census block). -sex (m and f) -age : numeric values 0 thru 102. Except for 100-102 values the code is the actual age in years. -race: 6 race-alone codes (1w=White, 2b=Black, 3i=American Indian+, 4a=Asian, 5h=Hawn + PI, 6o=Other race) and a multi-race category (mr). -NOTE: Using table p12 we also output observations where race and hispanic codes are blank (missing). These observations can be selected for use when race and hispanic detail are not of interest. They should generally be segregated from the observations that have non-missing values for these dimension variables. SAS procs like tabulate will normally ignore cases where a class variable is missing so these cases get ignored when race or hispanic are used as class variables. -hispanic: '0' for non-hispanic, '1' for hispanic. (Or blank -- see NOTE, above). Persons is the single "fact" variable on the data set. Sum this variable across various combinations of the dimensions to get whatever geography, age, race, sex, hispanic combinations you need. Note that we do not output zero cells! So, if there are no black, non-hispanics in Boone county, tract 10 then there will not be any obs with sumlevl=080, tract=0010.00, race=2b, hispanic=0. See the sample proc tabulate code at bottom of this setup. -----*; data sf1.moagersexhng(label='Mo Age, Race, Sex, Hispanic pop counts, no geocodes' sortedby=geo_id); merge sf1.mogeos(keep=geo_id sumlev where=( sumlev ='080') in=ingeos) sf1.mopctng(keep=geo_id pct12i1-pct12i209 in=inpct) sf1.mopct12rng(in=inpct12r); by geo_id; if ingeos; if not (inpct and inpct12r) then do; _nmiss+1; if _nmiss le 20 then put '***Missing data for ' geo_id= sumlev= inpct= inpct12r=; else abort abend; end; length Sex $1 Race $2 Hispanic $1 Age Persons 4; array pct12{2,0:102} pct12i3-pct12i105 pct12i107-pct12i209; array pct12r1(6,2,0:102) pct12ai3-pct12ai105 pct12ai107-pct12ai209 pct12bi3-pct12bi105 pct12bi107-pct12bi209 pct12ci3-pct12ci105 pct12ci107-pct12ci209 pct12di3-pct12di105 pct12di107-pct12di209 pct12ei3-pct12ei105 pct12ei107-pct12ei209 pct12fi3-pct12fi105 pct12fi107-pct12fi209; array pct12g(2,0:102) pct12gi3-pct12gi105 pct12gi107-pct12gi209; array pct12rnh(6,2,0:102) pct12Ii3-pct12Ii105 pct12Ii107-pct12Ii209 pct12ji3-pct12ji105 pct12ji107-pct12ji209 pct12ki3-pct12ki105 pct12ki107-pct12ki209 pct12Li3-pct12Li105 pct12Li107-pct12Li209 pct12mi3-pct12mi105 pct12mi107-pct12mi209 pct12ni3-pct12ni105 pct12ni107-pct12ni209; array pct12O(2,0:102) pct12Oi3-pct12Oi105 pct12Oi107-pct12Oi209; array racecode(6) $2 _temporary_ ('1w','2b','3i','4a','5h','6o'); *--The six pct12 tables give counts by the six race-alone categories. The six pct12 tables give the corresponding counts for the race-alone/non-hispanic categories. For ease of retabulation we create output that has cells with race and hispanic (1/0 for yes/no) codes. We get the hispanic cell by subtracting the non-hispanic from the total. -; array totalsr1(6) pct12ai1 pct12bi1 pct12ci1 pct12di1 pct12ei1 pct12fi1; array totalsrnh(6) pct12Ii1 pct12ji1 pct12ki1 pct12Li1 pct12mi1 pct12ni1; if pct12i1 = 0 then return; *---if total pop is 0, no people to count---; *--process pct12 for total persons (regardless of race or hispanic origin)--; *--This is a little dangerous because it may be easy to create double-counted tables with tabulate. But for those apps where race and hispanic are not relevant you can select on those categories being blank and get totals by just age and/or sex much faster. --*; race=' '; hispanic=' '; do isex=1 to 2; if isex=1 then sex='m'; else sex='f'; do age=0 to 102; Persons=pct12(isex,age); if Persons then output; *<--the if says to NOT output zero values--; end; end; *--isex-; *--now we generate full set of sex (2), by race(7) by hispanic(2) by age(103) categories. What saves us from creating a monster here is that we DO NOT KEEP 0 CELLS--; do irace=1 to 6; if totalsr1(irace)=0 then continue; *<----skip processing if table total is 0--; race=racecode(irace); do isex=1 to 2; if isex=1 then sex='m'; else sex='f'; do age=0 to 102; hispanic='1'; *--Subtract non-hispanic from total to get the race/hispanic count-; Persons=pct12r1(irace,isex,age) - pct12rnh(irace,isex,age); if Persons then output; hispanic='0'; Persons=pct12rnh(irace,isex,age); if Persons then output; end; end; *--isex-; end; *--irace-; *--we do the multi-race category separately--; if pct12gi1 then do; race='mr'; *<---multirace--; do isex=1 to 2; if isex=1 then sex='m'; else sex="f"; do age=0 to 102; hispanic='1'; Persons=pct12g(isex,age) - pct12O(isex,age); if Persons then output; *<--multi-race hispanic-; hispanic='0'; Persons=pct12O(isex,age); if Persons then output; end; end; *--isex-; end; *--pct12g processing-; keep geo_id sex age race hispanic Persons; *--all we keep on this very large set for geography is the single key variable geo_id. See next step.--; run; *<==========Create the view with geographic codes added back in on the fly. (Saves a lot of storage space)======; *<===The $county. format code can be created with code found at https://mcdc.missouri.edu/sas/formats/Scounty.sas. Use that to create the $county code which must then be available whenever this view is accessed. If this is not convenient, just remove the line creating CountyName variable and the reference to it in tabulate step, below.--; proc sql; create view sf1.moagersexh(label='MO - Age by Race by Sex by Hispanic Person Counts') as select *, put(county,$county.) as CountyName, case when placefp='99999' then 'Unincorporated Rmndr' else put(placefp,$mofplace.) end as place, put(cousubfp,$momcd.) as township from moagersexhng d, mogeos(keep=geo_id geocode state county placefp cousubfp placeSC placeCC cousubSC cousubCC tract intptlon intptlat areasqmi) g where d.geo_id = g.geo_id ; quit; *---Create a sample county subset, filtering the totals-across-race/hispanic cases----; data booneco; set sf1.moagersexh; where county='29019' and race ne ' ' ; run; *-----And now show some proc tabulate steps to demonstrate usefulness--------; proc format; *---Note how easy it is to change to whatever age intervals you want by just tweaking this code-; value age 0-17='0-17' 18-34='18-34' 35-54='35-54' 55-64='55-64' 65-high='65 +'; *--yesno will be used with the hispanic dimension variable-; value $yesno '0'='No' '1'='Yes'; value $race '1w'='White alone' '2b'='Black alone' '3i'='Amer Indian alone' '4a'='Asian alone' 'mr'='Multi-race' other='Other single race'; run; options missing='0'; *<---so the cells with no data print as 0's instead of .'s--; options nodate; proc tabulate data= booneco; by CountyName; *<===delete if not using $county. format code-; class place age sex race hispanic ; format age age. hispanic $yesno. race $race.; var Persons; table (place='City' all='County Totals')* (all='Total' age), (all='Total' race hispanic)*persons=' '*sum=' '*f=comma7.; title2 'Sample Proc Tabulate Output : Places Within a County Broken Down by Age, Race and Hispanic Origin'; run ; *---Generate a table for the n-mile radius around the centroid of a place (Hallsville, Mo)---; %let miles=20; %let cityname=Hallsville; proc sql noprint; select intptlon,intptlat into :x0, :y0 from sf1.mogeos where sumlev='160' and areaname contains "&cityname"; *--Note use of the automagincally included latlond macro that returns a distance in miles based on 2 sets of lat-long coordinates as arugments. Credit to D. Ward for this code.--; create table circgeos as select geo_id , county, put(placefp,$mofplace.) as place, %latlond(&x0,&y0,intptlon,intptlat) as dmiles from sf1.mogeos where sumlev='080' and %latlond(&x0,&y0,intptlon,intptlat) le &miles; *--OK, we selected the geocodes in the above query, now merge those with our data--; *--Could do create view here instead of create data. Probably would be a little more efficient--; create table circdata as select * from sf1.moagersexhng d, circgeos g where d.geo_id = g.geo_id; quit; proc tabulate data= circdata; class county place age sex race hispanic ; format age age. county $county. ; format hispanic $yesno. race $race.; var Persons; table (place='City' all="&miles.-mile radius of &cityname")* (all='Total' age), (all='Total' race hispanic)*persons=' '*sum=' '*f=comma7.; title2 'Sample Proc Tabulate Output Using moagersexh Data Set and Circular Area Filtering'; run; *---Finally, do an example where we use the totals-across-race and hispanic only--; proc tabulate data=sf1.moagersexh(where=(substr(county,3,3) in ('019','027','051') and race =' ')); class county age sex; var persons; format age age. county $county.; table county all='Tri-County Totals', (age all='Totals')*sex*Persons*n=' '*f=comma7.; title2 'County Totals of 2000 Poulation by Age and Sex'; run;