循环以有条件地导出表格SAS(大量的variables)

所以我试图根据一个特定的variables分组来分解一个大的数据集(7万个variables)。 Excel或CSV是输出的理想格式,但对可变数字(260或某物)有限制。 任何想法如何我可以在SAS(或R / SQL,否则)做到这一点?

我知道macros的作品,我以前用过。 错误消息读取variables的限制已达到。

创build一个Excel文件肯定是有限制的,但不是一个CSV文件。 以下是使用虚拟SAS数据集的示例:

 data a; array x(*) x1-x1790; do j=1 to 5; do i=1 to dim(x); x(i) = ranuni(0); end; output; end; run; proc export data=a outfile="c:\temp\tempfile.csv" dbms=CSV replace; run; 

这里是相关的日志:

 NOTE: The file 'c:\temp\tempfile.csv' is: Filename=c:\temp\tempfile.csv, RECFM=V,LRECL=32767,File Size (bytes)=0, Last Modified=23Jan2013:15:27:13, Create Time=23Jan2013:15:27:13 NOTE: 6 records were written to the file 'c:\temp\tempfile.csv'. The minimum record length was 9636. The maximum record length was 23087. NOTE: There were 5 observations read from the data set WORK.A. NOTE: DATA statement used (Total process time): real time 0.26 seconds cpu time 0.09 seconds 5 records created in c:\temp\tempfile.csv from A. NOTE: "c:\temp\tempfile.csv" file was successfully created. NOTE: PROCEDURE EXPORT used (Total process time): real time 2.04 seconds cpu time 0.26 seconds 

注意第一行包含列标题。

更新:如果您拥有最新版本的SAS(9.3 TS1M1或更高版本),则可以创buildOffice 2010 Excel电子表格,该表格最多包含1,048,576行16,384列。 在这种情况下,您将使用DBMS=XLSX

鲍勃的答案是好的,如果你可以用XLSX或CSV。 如果你想制作一个.xls的excel文件(255列限制),或者没有9.3TS1M1,那么做起来相当容易。 具体取决于你想如何指定进入每个文件的列。

假设你只想把每个255列分成一个单独的文件,并在中点分割两个文件(35000logging到文件A,35001-结束到文件B,每组variables)。 你会做这样的事情:

 options mprint symbolgen; data test; array xs x1-x1700; do id = 1 to 70000; do _t = 1 to dim(xs); xs[_t]=ranuni(7); end; output; end; run; %macro export_file(varstart=,varend=,varnumstart=0,varnumend=0,recstart=1,recend=0,keeplist=,dset=, libname=WORK, outfile=,sheet="sheet1"); %if &varnumstart ne 0 %then %do; proc sql noprint; select name into :varstart from dictionary.columns where libname=upcase("&libname.") and memname=upcase("&dset.") and varnum=&varnumstart.; select name into :varend from dictionary.columns where libname=upcase("&libname.") and memname=upcase("&dset.") and varnum=&varnumend.; quit; %end; %if &varstart=%str() or &varend=%str() %then %do; %put "ERROR: MISSING PARAMETERS. PLEASE CHECK YOUR MACRO CALL AND RERUN. MUST HAVE VARSTART AND VAREND OR VARNUMSTART AND VARNUMEND."; %abort; %end; data _for_Export/view=_for_export; set &libname..&dset; keep &varstart.--&varend. %if &keeplist ne %str() %then %do; &keeplist %end; ; if _N_ ge &recstart.; %if &recend ne 0 %then %do; if _N_ le &recend.; %end; run; proc export data=_for_export file=&outfile. dbms=excel replace; sheet=&sheet.; run; proc datasets nolist noprint lib=work; delete _for_export/memtype=view; quit; %mend export_file; %export_file(varnumstart=1,varnumend=250, keeplist=id,recstart=1,recend=35000,dset=test,outfile="c:\temp\test.xls",sheet="sheet1"); %export_file(varnumstart=1,varnumend=250, keeplist=id,recstart=35001,recend=99999,dset=test,outfile="c:\temp\test.xls",sheet="sheet2"); %export_file(varnumstart=251,varnumend=500, keeplist=id,recstart=1,recend=35000,dset=test,outfile="c:\temp\test.xls",sheet="sheet3"); %export_file(varnumstart=251,varnumend=500, keeplist=id,recstart=35001,recend=99999,dset=test,outfile="c:\temp\test.xls",sheet="sheet4"); 

我尝试导出sheet4时失败,不知道是否对.xls文件的总大小有一定的限制,但是可以轻松修改此项以创build单独的文件。 如果您需要为每个单独的文件指定不连续的特定variables名称,这将不起作用,但是您可以很容易地修改从dictionary.columns中提取的SQL代码,而不是从您创build的包含variables名称的表中拉出要在每个文件中。