x cd /pub/data/popests/Tools; %let pgm=genCurSSestsData; filename pgm "&pgm..sas"; *--In this setup we access the Bureau's subcounty estimates for &year (latest estimate year, vintage) and generate a set of 4 CSV files corresponding to the 4 Excel worksheets with data for a single state at various levels and sorts. jgb, 5-21-14. Run for 2014 vintage data, 6-2015. Run for 2015 vintage data, 6-2016. Run for 2016 vintage data, 5-2017. --*; libname popests '/pub/data/popests/temp'; *****<===========embargoed data!!!!!!!!!!******; %let year=2016; %let yr=%substr(&year,3,2); %let outlib= %str(/pub/data/popests/curxxests_csvfiles&yr); *--we created this and for embargo made it unreadable--; %let state=29; %let stab=mo; x cd &outlib; *--this is where we write the output CSV files---; %macro gendlm(setname,dlm=comma,csvlen=2048); %*---Generate delimited file----; %if &dlm=tab %then %let fext=txt; %else %let fext=csv; %let file=&setname..&fext.1; %let ffile=&setname..&fext; *--final file.---; filename _csvout "&file" lrecl=&csvlen ; options nosource; proc export data=&setname dbms=csv outfile=_csvout replace; run; options source; %*---edit the dlm file by adding variable labels as part of the first row, i.e. we want var names AND labels as one value---; proc contents data=&setname noprint out=_varlabs&setname(keep=name label varnum); run; proc sort; by varnum; run; data _null_; infile _csvout lrecl=&csvlen; input; file "&ffile" lrecl=&csvlen; *<---final output file, with variable labels added--; if _n_=1 then do; put _infile_; if 0 then set _varlabs&setname nobs=_nvars; do _j=1 to _nvars; set _varlabs&setname; length _qlabel $132; retain _dlm %if &dlm=tab %then %str('09'x); %else %str(','); ; _qlabel=quote(trim(compress(label,_dlm))); put _qlabel @; if _j lt _nvars then put _dlm $1. @; else put; end; end; else put _infile_; run; x rm "&file"; *<----delete original file to save space----; %mend gendlm; %macro doit(state,stab); data &stab.counties &stab.cities (drop=mcd fipco county) &stab.citybyco(drop=mcd) &stab.all &stab.mcds(drop=placefp) ; set popests.ussc&yr end=last; where state="&state"; retain _mcds "0"; select (sumlev); when('040') do; output &stab.counties; output &stab.cities; output &stab.citybyco; output &stab.mcds; end; when('050') do; output &stab.counties; output &stab.citybyco; end; when('061') do; output &stab.mcds; _mcds="1"; ; end; when('157') output &stab.citybyco; when('162') output &stab.cities; otherwise ; end; output &stab.all; if last then call symput('mcds',_mcds); drop original_estimate; drop concit funcstat cnty State; drop _mcds; label stab='St'; run; %put mcds=&mcds; %gendlm(&stab.counties) %gendlm(&stab.cities) %gendlm(&stab.citybyco) %if &mcds %then %gendlm(&stab.mcds); %gendlm(&stab.all) %mend doit; %doit(29,mo); %doit(17,il) ; %doit(20,ks) ;