x cd /mscdc/data/stp154/Tools; %let pgm=stp154c; filename pgm "&pgm..sas"; %let data=/mscdc/data/stp154; filename in "&data/wa53"; *<===========; libname stp154 "&data"; libname user '/tmp/gaussian/stp154'; title 'Convert STP154 Data (journey-to-work by tract & place) for ' 'Seattle Study Area'; *<===========; *--this is revised run with different summary levels-; *<===================; proc format; value $sc '53033'='King' '53053'='Pierce' '53057'='Skagit' '53061'='Snohomish' '53055'='San Juan' other=' '; value $rloc '0'='In study area' '1'='In work state, outside s.a.' '2'='Outside work state'; value lvl 1='1rtp' 2='2rc ' 3='3wt'; run; *--generate a (permanent) custom format code to assign names to all places (any size) in Washington state.-; libname stf903x '/mscdc/data/stf903x'; filename text '/tmp/gaussian/stp154/Sbig10pl.sas'; /* *<==================comment out already run code================ *--user specified that he only wanted place detail for 10 largest places in the study area. All other place codes will be collapsed. We go to the stf903x data set to get top 10-; proc sort in=stf903x.waplaces(keep=place areaname county totpop) out= big10pl; by descending totpop; data big10pl2; set big10pl(obs=10); *--some editing of names-; *--remove word 'city' from areaname-; ix=index(areaname,'city'); if ix then substr(areaname,ix)=' '; length labelvar $16; labelvar=place ||' '||areaname; drop ix; put _all_; run; *--write source code to text file, format module to library.formats-; %genfmat(sasin=big10pl2,fmatname=big10pl, argvars=place, arglen=4, labelvar=labelvar, other=%str(----), runproc=1) ; options nocenter; *---process the data. access input file for state(s): wa Select subset for target area: Seattle 5-county Area. ---*; data stp154rd(label='stp154 - raw data, temp'); infile in firstobs=8894 ; *<===first obs with tract in 5-co area-; length default=4; *--for work location we ignore place and mcd fields. just look at tract; input wstate $1-2 wcounty $1-5 wtractin $6-11 +8 rstate $20-21 rcounty $20-24 rtractin $25-30 rmcd $31-33 rplacecd $34-37 +1 (workers watract waplace wacomcd)(6.); if put(wcounty,$sc.) ne ' '; *--work county filter-------------; *--collapse for residence geography based on outside the select work area but still in the state of work, and then outside the state; length rloc $1; label rloc='Resident location code'; length rplace $16; if rstate ne wstate then rloc='2'; else if put(rcounty,$sc.)=' ' then rloc='1'; else rloc='0'; *if rstate ne wstate or workers < 10 then do; *--this statement edited after running Seattle to allow for multi-state regions where there can be significant cross-state travel---; if workers < 10 then do; rstate='xx'; rcounty='xxx'; rtractin='xxxxxx'; rplace='xxxx'; end; else rplace=put(rplacecd,$big10pl.); *--recode; length wtract rtract $7; wtract=substr(wtractin,1,4)||'.'||substr(wtractin,5,2); rtract=substr(rtractin,1,4)||'.'||substr(rtractin,5,2); label wstate='FIPS state code, place of work' wcounty='County of work' wtract='Tract of work' rtract='Tract of residence' rmcd='MCD of residence (New Eng)' rplace='Place (city) of residence' workers='Count of workers, inc. allocs' watract='Workers allocated to wtract' waplace='Workers allocated to wplace' wacomcd='Workers allocated to wmcd'; drop wtractin rtractin rplacecd; drop rmcd wacomcd; *<----------outside New England!-----------; output; run; proc print data=stp154rd(obs=50); by wstate wcounty; format wcounty $sc.; title2 'Sample of Raw Data'; run; *--now sort and aggregate to various geographic levels--; proc sort data=stp154rd out=aggin; by wcounty wtract rcounty rtract rplace; run; %agg(aggin=aggin,aggout=aggout, aggby=wcounty wtract rcounty rtract rplace, aggvars=workers watract waplace,naggvars=3, agglvls=4,fillby=y, idvars=rloc) *--added step: look at grand totals at wtract level and rank the tracts by the total workers in the work tract. final file to be sorted by the ranking-; data wt_tots/view=wt_tots; set aggout; where _lvl_=4; keep wcounty wtract workers; run; proc sort data=wt_tots out=wt_tot2; by descending workers; run; data tranks; length trank 3; format trank 4.; set wt_tot2; trank+1; rename workers=tworkers; run; proc sort data=tranks ; by wcounty wtract; run; ============================end commented out==================== */ data stp154.seattle(label='5-county Seattle Area for K Burns'); length trank 3 tworkers 4; retain wcounty ' ' wtract ' ' lvl ' ' rloc rcounty rtract rplace workers watract waplace ; merge aggout(in=in1 where=(_lvl_ ne 2)) tranks( in=in2); by wcounty wtract; drop _nag_ _lvl_; if not (in1 and in2) then do; put '*****logic problem -- check it out: ' / _all_; abort; end; if _lvl_ gt 2 then _lvl_=_lvl_-1; *--recode-; if lag(_lvl_) ne _lvl_ then lvl=put(_lvl_,lvl.); label tworkers='Total workers in work tract'; label lvl='Geog. aggregation level'; run; proc sort data=stp154.seattle; by lvl trank descending workers ; run; proc freq data=stp154.seattle(keep=lvl); table lvl; run; title2 'stp154.seattle: sample subset -- 1st 25 at each lvl'; data sample; set stp154.seattle; by lvl; if first.lvl then _n=1; else _n+1; drop _n; if _n le 25; run; proc print data=sample; by lvl; id trank wcounty wtract; run; proc contents data=stp154.seattle position; run;