x cd /pub/data/pl942000/Tools; %let pgm=create_us_datasets; filename pgm "&pgm..sas"; /* ----Generates national data sets for pl942000. Access the data (xxsums) for each state and aggregate to get summaries for the country, regions, divisions, states, counties, places >2500 and MCDs in New England. Aggregate to get sets for metro areas . John Blodgett, OSEDA, U. of Missouri Outreach & Extension. April, 2001. ---- */ *---Edited 4-16-01 to add geocode var to the usregdiv data set. Run interactively----*; libname user '/SAS_temp/user'; libname pl942000 '/pub/data/pl942000'; options ls=157 ps=120; title "PL942000: &pgm -- Create US Level Data Sets"; *--Stored in /mscdc/sascode/allsets.sas--; *--Sample of code (posted to SAS-L by Ian Whitlock) to access all datasets in a SAS data lib and capture the results in a "code loop" store in a macro var. Has lots of variations, applications -- this code to be used as a template. E.g. change "proc print" to "proc univariate" and add a filter on memname to the where clause. jgb, 4-99. --; /*<=========================Comment out===================================== proc sql ; *-----This code created the list of sets in the SET statement below----; select 'pl942000.'||trim(memname) into :sumsets separated by " " from dictionary.tables where libname = "PL942000" and substr(memname,3)="SUMS"; ; quit ; data _null_; file 'temp.sas'; put "&sumsets"; %put &sumsets; run; ===========================End commented out================================ */ data pl942000.usstates(label='All states in U.S.' drop=county tract logrecno cntysc--gcuni) pl942000.uscntys (label='All counties and equivalents in U.S.' drop=cntysc--concit masc cd106 gcuni ) pl942000.usplaces(label='All places (cities) of 2500+ in U.S.' drop=county cnty--cousubsc concit masc cd106) pl942000.nemcds (label='New England towns'); length geocode $12; set pl942000.AKSUMS pl942000.ALSUMS pl942000.ARSUMS pl942000.AZSUMS pl942000.CASUMS pl942000.COSUMS pl942000.CTSUMS pl942000.DCSUMS pl942000.DESUMS pl942000.FLSUMS pl942000.GASUMS pl942000.HISUMS pl942000.IASUMS pl942000.IDSUMS pl942000.ILSUMS pl942000.INSUMS pl942000.KSSUMS pl942000.KYSUMS pl942000.LASUMS pl942000.MASUMS pl942000.MDSUMS pl942000.MESUMS pl942000.MISUMS pl942000.MNSUMS pl942000.MOSUMS pl942000.MSSUMS pl942000.MTSUMS pl942000.NCSUMS pl942000.NDSUMS pl942000.NESUMS pl942000.NHSUMS pl942000.NJSUMS pl942000.NMSUMS pl942000.NVSUMS pl942000.NYSUMS pl942000.OHSUMS pl942000.OKSUMS pl942000.ORSUMS pl942000.PASUMS pl942000.RISUMS pl942000.SCSUMS pl942000.SDSUMS pl942000.TNSUMS pl942000.TXSUMS pl942000.UTSUMS pl942000.VASUMS pl942000.VTSUMS pl942000.WASUMS pl942000.WISUMS pl942000.WVSUMS pl942000.WYSUMS open=defer; *--will process the files sequentially rather than having all 51 open at the same time--; select (sumlev); when ('040') output pl942000.usstates; when ('050') output pl942000.uscntys; when ('060') if division='1' then do; if state=' ' then state=substr(county,1,2); output pl942000.nemcds; end; when ('160') if pop100 ge 2500 then output pl942000.usplaces; otherwise delete; end; return; drop geo_id tract logrecno statece bg block urbanrur sldu sldl vtd vtdi zcta3 zip arealand areawatr lsadc partflag sdelm sdsec sduni taz uga puma5 puma1 _arg; run; proc sort data=pl942000.usstates; by state; run; proc sort data=pl942000.uscntys; by state; run; proc sort data=pl942000.nemcds; by state cousubfp; run; proc sort data=pl942000.usplaces; by state placefp; run; *--------Aggregate the states to create totals for the country, regions and divisions------------*; proc sort data=pl942000.usstates out=states; by region division; run; %agg (setin=states,setout=usregdiv,aggby=region division,idvars=, aggvars=totpop--mrovr18 landsqmi areasqmi intptlon intptlat, means=intptlon intptlat,mweights=totpop,agglvl=2,grand=1) run; data pl942000.usregdiv(label='U.S. Totals, Regions and Divisions'); length sumlev $3 areaname $40 geocode $2; set usregdiv; select (_lvl_); when (1) do; sumlev='030'; geocode='d'||division; areaname=put(division,$divisio.); end; when (2) do; sumlev='020'; geocode='r'||region; areaname=put(region,$usregio.); end; when (99) do; sumlev='010'; geocode='us'; areaname='United States Totals'; end; end; rename _nag_=nstates; drop _lvl_; run; proc sort data=pl942000.usregdiv; by sumlev geocode; ; run; proc print data=pl942000.usregdiv; title2 'pl942000.usregdiv'; run; *----------Aggregate the county level data and the New England town level data to get metro area totals-------; data metparts; set pl942000.uscntys (in=inc) pl942000.nemcds (in=innemcds); if inc and division='1' and necma = '9999' then delete; *--only keep New Eng counties in necmas-; necmaflg=' '; if msacmsa='####' and necma ne '9999' then do; *--this is a New England county-; msacmsa=necma; pmsa='9999'; necmaflg='1'; cmsa2='99'; end; keep state msacmsa pmsa cmsa2 necmaflg _numeric_; run; proc sort data=metparts; by msacmsa pmsa state; run; %agg (setin=metparts,setout=usmetros,aggby=msacmsa pmsa state,idvars=cmsa2 necmaflg, aggvars=totpop--mrovr18 landsqmi areasqmi intptlon intptlat, means=intptlon intptlat,mweights=totpop,agglvl=2,grand=0) run; data aggin2; set usmetros; if _lvl_=1 and pmsa ne '9999' ; *--we need to get the cmsa within state summaries by agging these pmsa within state; run; proc sort data=aggin2; by msacmsa state; run; %agg (setin=aggin2,setout=cmsa310,aggby=msacmsa state,idvars=cmsa2 necmaflg, aggvars=totpop--mrovr18 landsqmi areasqmi intptlon intptlat, means=intptlon intptlat,mweights=totpop,agglvl=2,grand=0) run; data pl942000.usmetros(label='US MSAs, NECMAs, CMSAs and PMSAs - Total and Within State'); length sumlev $3 geocode $8 mtype $1 msacmsa pmsa $4 areaname $64 ; set usmetros cmsa310(in=in310); *--we shall use sumlev codes of 302 and 312 for complete and within-state NECMAs, resp. --; if in310 then do; mtype='C'; if _lvl_=1 then sumlev='310'; else sumlev='300'; areaname=put(msacmsa,$metro.); end; else select (_lvl_); when (1) do; if pmsa='9999' then do; if necmaflg='1' then sumlev='312'; else sumlev='310'; if necmaflg then mtype='N'; else mtype='M'; areaname=put(msacmsa,$metro.); geocode=msacmsa||'-'||state; end; else do; mtype='P'; sumlev='311'; areaname=put(pmsa,$metro.); geocode=pmsa||'-'||state; end; end; when (2) do; if pmsa='9999' then do; if necmaflg='1' then sumlev='302'; else sumlev='300'; if necmaflg='1' then mtype='N'; else mtype='M'; areaname=put(msacmsa,$metro.); geocode=msacmsa; end; else do; mtype='P'; sumlev='301'; areaname=put(pmsa,$metro.); geocode=pmsa; end; end; otherwise do; put '****Whoops! **** ' / _all_; stop; end; end; *--select stmt--; if sumlev ='310' and msacmsa='9999' then if state ne ' ' then areaname='Non-metro portion of '||put(state,$state.); else delete; if sumlev ='300' and msacmsa='9999' then delete; format intptlon 11.6 intptlat 10.6 areasqmi landsqmi best10.; drop _lvl_ necmaflg _nag_; run; proc sort data=pl942000.usmetros; by msacmsa pmsa state; run; proc tabulate data=pl942000.usmetros missing; class sumlev state mtype; var totpop; format state $state.; table (all state)*(all mtype*sumlev), totpop*(n='N Metros'*f=5. sum='Population'*f=comma11.); title2 'Tabulation of Pop Data by Metro Areas from the pl942000.usmetros Data Set'; run cancel;