options noxwait; x cd p:\mcdc\acs2006\tools\; %let pgm=convert_profiles; filename pgm "&pgm..sas"; *----We have downloaded a set of large xls files with complete data for the US (almost) including the standard profiles data in 1 of the 4 worksheets included in each xls file. We target those worksheets, convert each to a SAS dataset and then use that data to write complete CSV files, 1 per subdirectory. E.g. we take the subdir with all PUMAs in the US and write a pair of CSV files (1 with the data cell values, the other with the MOE values) which get stored in the maind data directory. Another SAS program will read those files and turn them into SAS datasets. jgb, 9/11/2007 2:09:17 PM ----*; %include 'cnvtxls.sas'; *--load basic convert macro to be invoked in a loop to do entire directories--; %macro cnvtdir(sl,dd,sd,special=0,continue=0); %*---convert all files in a specified directory of xls files with profiles sheets within as downloaed from Bureau web site Sept 2007 ---; %let path=%str(&dd\&sd); %put path=&path; x cd &path; data _null_; retain path "&path. "; length lastlev $32; lastlev=scan(path,-1,'\'); lastlev=translate(trim(lastlev),'_',' '); call symput('lastlev',trim(lastlev)); path='"'||trim(path)||'"'; call symput("qpath",trim(path)); run; x dir &qpath /A-D /B > c:\temp\&lastlev._files; data _null_; infile "c:\temp\&lastlev._files" missover end=last; input; _ix= index(_infile_,'.xls'); if _ix=0 then goto cklast; length _filename $80; _filename=substr(_infile_,1,_ix-1); %if &special %then %do; %*--process only files with special problem characters--; if indexc(_filename,'.()')=0 then goto cklast; *<---and skip processing this file----; %end; i+1; length macvarname $8 macvarval $80; macvarname='fn'||left(put(i,4.)); macvarval=trim(_infile_); macvarval=translate(_filename,"`?!^#","'-.()"); *--replace tics with back tics and dashes with ?s, etc--; ***macvarval=compress(macvarval,'.'); call symput(macvarname,trim(macvarval)); list; cklast: if last then call symput('nfiles',left(put(i,4.))); run; %put ***Begin converting files in &dd\&sd with sumlev=&sl ***; %put nfiles=&nfiles %str( ) fn&nfiles=&&fn&nfiles; %put _all_; filename prodata "&dd\&sl.profile_data.csv" lrecl=4096; filename promoes "&dd\&sl.profile_moes.csv" lrecl=4096; filename cnvtlog "&dd\&sl.cnvtlog.txt"; %if &continue=0 %then %do; %*--continue=1 says to pick up converting of directory where you left off, so do not start the output files anew----; data _null; file prodata; put "Profile data from &dd.\&sd written &sysdate"; file promoes; put "Profile moe values from &dd.\&sd written &sysdate"; file cnvtlog; put "Conversion log written &sysdate"; run; %end; %do i=1 %to &nfiles; %let f=&&fn&i; %cnvtxls(sumlev=&sl,datadir=&dd,subdir=&sd,filename=%superq(f)) %end; run; %mend cnvtdir; options NOmprint; /* ========================begin comment ================================= %cnvtdir(020,%str(P:\mcdc\acs2006\usprofiles),020Regions) ; %cnvtdir(040,%str(P:\mcdc\acs2006\usprofiles),040States) %cnvtdir(050,%str(P:\mcdc\acs2006\usprofiles),050County,special=1,continue=1) %cnvtdir(160,%str(P:\mcdc\acs2006\usprofiles),160Place,special=1,continue=1) %cnvtdir(310,%str(P:\mcdc\acs2006\usprofiles),%nrbquote(310CBSA)) %cnvtdir(400,%str(P:\mcdc\acs2006\usprofiles),%str(400UrbanArea)) %cnvtdir(500,%str(P:\mcdc\acs2006\usprofiles),%str(500CongressionalDistricts)) %cnvtdir(795,%str(P:\mcdc\acs2006\usprofiles),795PUMA) ==========================end comment==================================== */ /* add in missing files */ %cnvtdir(795,%str(P:\mcdc\acs2006\usprofiles),%str(MISSING)) run;