x cd /pub/data/acs2006/Tools; %let pgm=cnvt_summary_files; filename pgm "&pgm..sas"; /*--Read the summary files to create a sas dataset with all the base tables for the US. Uses code that was generated from the m-r data dictionary file in basetables_metadta setup. J. Blodgett, OSEDA, Oct., 2007 Last revised 11/5/2007 4:02:49 PM (comments and typos fixed. Code to create usalltabs removed. -- */ title "&pgm.: Read zipped CSV ('summary') files to create acs2006.usalltabs"; libname acs2006 '/pub/data/acs2006'; libname acs (acs2006); libname summary '/pub/data/acs2006/summary'; libname temp '/pub/data/acs2006/temp'; *<===========directory has been deleted. The XXtabs datasets were moved to a new directory: .../summary/tabsByState (jgb, 11/20/2007 9:34:11 AM) ==========================================; filename varlabs '/pub/data/acs2006/BasevarLabels.sas'; *<--see basetables_metdata.sas for creation of this and following 2 files-; filename basecode '/pub/data/acs2006/Tools/basecnvtcode.sas'; *<---critical assignment stmts for &svars1, &svars2,...,&svars135 --; filename lengths '/pub/data/acs2006/Tools/length_statements.sas'; /* =======================Begin comment================ (this code has already been run) proc sort data=acs.allgeos out=acs.allgeos(index=(state)); by state LogRecNo; run; proc means data=acs.allgeos n min max maxdec=0; class state; run ; ======================End comment================== */ x cd /pub/data/acs2006/summary/csvsegs; *--As we dynamically unzip the all_xx.zip files downloaded from the Bureau ftp site, we specify that the 144 individual segment-level .zip files inside get placed into this directory, which we here designate as the current directory. Hence when we do our unzip via the pipe in the input routine we do not need to specify any path info since it defaults to this (the current) directory.---; %include basecode; *---assigns all the global svarsXXX specs (macro variables)---; %macro doit(state,stab); x "unzip -j ../all_&stab..zip"; *---causes 144 .zip files to get written to the csvsegs subdirectory --; %do iseg=1 %to 135; %---we do not process segments 136 to 144 nor segment 9. These are imputation table and PR table data.--; %if &iseg=9 %then %goto endloop; %if %length(&iseg)=1 %then %let segno=00&iseg; %else %if %length(&iseg)=2 %then %let segno=0&iseg; %else %let segno=&iseg; data temp.&stab.seg&segno; filename inpipe pipe "unzip -j -p &stab.0000&segno..2006-1yr.zip tab4/sumfile/prod/2006/data/&stab.0000&segno.e.2006-1yr"; infile inpipe dsd lrecl=4000 missover; length default=5; *--by coding convention all variables beginning with "_" will not be kept on the data set--; length _acssf $5 _fileid $6 _stab $2 _chariter $3 _segno $4 LogRecNo 5; missing N; *---this was in case the letter N appeared on the input CSV files. But it did not--; *<------------Here is the most important statement in the program. Most important/difficult part is knowing the list of variables to read from the segment file. This is handled by storing the list of variables in a svars&segno macro variable, which gets resolved here. Values were assigned in the basetables_metdata.sas program and those results were saved in a sas program file which was included in this program, above-----; input _acssf -- LogRecNo &&&svars&segno ; if _stab ne "&stab" then do; put "****stab check filed does not match parm**** parm value=&stab , value on file is " _stab ; list; stop; end; ***if _n_=1 then put _all_; drop _: ; run; %endloop: %end; %*---We run out of memory trying to merge 135 files at a time. So we employ an old strategy that seems to work, whereby we merge our datasets into a set of larger ones and then merge those merged datasets. Its a pain, but it works. --; %let iseg=0; %do setgrp=1 %to 14; data tabsseg&setgrp; merge %do j=1 %to 10; %let iseg=%eval(&iseg+1); %if &iseg gt 135 %then %goto endtabsegs; %if %length(&iseg)=1 %then %let segno=00&iseg; %else %if %length(&iseg)=2 %then %let segno=0&iseg; %else %let segno=&iseg; %if &iseg ne 9 %then %str( temp.&stab.seg&segno(in=in&iseg) ); %end; %endtabsegs: ; by LogRecNo; run; %end; %*-----OK, we have taken the 135 or so segment-based datasets and created 14 grouped sets, which we shall now merge. NOTE that this is also where we merge the acs.allgeos dataset, which is critical for providing all the geocode and other id variables. ---; data temp.&stab.tabs; merge acs.allgeos(where=(state="&state") in=ingeos) %do setgrp=1 %to 14; tabsseg&setgrp(in=in&setgrp) %end; ; by LogRecNo; run; %put Procesing for &stab completed ***; %mend doit; options mprint; %*doit( ,us); options NOmprint; *---invoke a macro for every state in U.S.---; %doit(01,al) ; %doit(02,ak) ; %doit(04,az) ; %doit(05,ar) ; %doit(06,ca) ; %doit(08,co) ; %doit(09,ct) ; %doit(10,de) ; %doit(11,dc) ; %doit(12,fl) ; %doit(13,ga) ; %doit(15,hi) ; %doit(16,id) ; %doit(17,il) ; %doit(18,in) ; %doit(19,ia) ; %doit(20,ks) ; %doit(21,ky) ; %doit(22,la) ; %doit(23,me) ; %doit(24,md) ; %doit(25,ma) ; %doit(26,mi) ; %doit(27,mn) ; %doit(28,ms) ; %*doit(29,mo) ; %doit(30,mt) ; %doit(31,ne) ; %doit(32,nv) ; %doit(33,nh) ; %doit(34,nj) ; %doit(35,nm) ; %doit(36,ny) ; %doit(37,nc) ; %doit(38,nd) ; %doit(39,oh) ; %doit(40,ok) ; %doit(41,or) ; %doit(42,pa) ; %doit(44,ri) ; %doit(45,sc) ; %doit(46,sd) ; %doit(47,tn) ; %doit(48,tx) ; %doit(49,ut) ; %doit(50,vt) ; %doit(51,va) ; %doit(53,wa) ; %doit(54,wv) ; %doit(55,wi) ; %doit(56,wy) ; options source2; *--------Final step involves taking the collection of all-tables-for-1-state-or-us and concatenating them so we have all u.s. geography in our datasets, but then partitioning the datasets so that we only keep some tables (by topic) in each resulting dataset. The result is a series of datasets that cover all u.s. geography and a set of related topics. ---------; data summary.ustabs00_07(label="US tables Re Age, Sex, Race, Ethnicity, Ancestry, Citizenship, POB, Migration" keep=logrecno _char_ b01: b02: b03: b04: b05: b06: b07: c02: c03: c04: c05: c06: c07: ) summary.ustabs08 (label="US tables Re Journey to Work, Worker Characteristics, by Place of Work" keep=logrecno _char_ b08: c08: ) summary.ustabs09_16(label="US tables Re Households, Families, Chidren, Marital Status, Fertility, Education, Language" keep=logrecno _char_ b09: b10: b11: b12: b13: b14: b15: b16: c09: c11: c12: c13: c14: c15: c16: ) _ summary.ustabs17_20(label="US tables Re Poverty, Income, Earnings, Disability" keep=logrecno _char_ b17: b18: b19: b20: c17: c18: c19: c20: ) summary.ustabs21_24(label="US tables Re Employment, Industry, Occupation" keep=logrecno _char_ b21: b22: b23: b24: c21: c22: c23: c24:) summary.ustabs25_26(label="US tables Re Housing and Group Quarters" keep=logrecno _char_ b25: b26: c25: ) ; if 0 then set acs.allgeos; *---puts the geocode variables in the pdv first---; %include lengths; *---Estblishes variable order----*; %macro doit(state,stab); temp.&stab.tabs %mend doit; set temp.ustabs %doit(01,al) %doit(02,ak) %doit(04,az) %doit(05,ar) %doit(06,ca) %doit(08,co) %doit(09,ct) %doit(10,de) %doit(11,dc) %doit(12,fl) %doit(13,ga) %doit(15,hi) %doit(16,id) %doit(17,il) %doit(18,in) %doit(19,ia) %doit(20,ks) %doit(21,ky) %doit(22,la) %doit(23,me) %doit(24,md) %doit(25,ma) %doit(26,mi) %doit(27,mn) %doit(28,ms) %doit(29,mo) %doit(30,mt) %doit(31,ne) %doit(32,nv) %doit(33,nh) %doit(34,nj) %doit(35,nm) %doit(36,ny) %doit(37,nc) %doit(38,nd) %doit(39,oh) %doit(40,ok) %doit(41,or) %doit(42,pa) %doit(44,ri) %doit(45,sc) %doit(46,sd) %doit(47,tn) %doit(48,tx) %doit(49,ut) %doit(50,vt) %doit(51,va) %doit(53,wa) %doit(54,wv) %doit(55,wi) %doit(56,wy) open=defer; *<---says to process files in sequence. Each is closed before the next one is opened. Saves memoary and makes it possible.---; %include varlabs; if _n_=1 then put "****Execution Begins***** "; run;