還有這個解決方案可能更適合您的需求。
首先創建一個將用作模板的HTML文件。不管你想要把一個值,使用宏變量作爲一個佔位符,像這樣:
<html>
<h1> My title is &title </h1><br>
Name: &name <br>
Value of Blah: &blah
</html>
讓它作爲吸引目光,只要你喜歡。
接下來創建一個宏,將導入HTML模板,用實際值替換佔位符,並將結果保存到一個新的文件:
/*****************************************************************************
** PROGRAM: MACRO.RESOLVE_FILE.SAS
**
** READS IN A FILE AND REPLACES ANY MACRO REFERENCES IN THE FILE WITH THE
** ACTUAL MACRO VALUES. EG. IF THE FILE WAS AN HTML FILE AND IT CONTAINED
** THE FOLLOWING HTML:
**
** <TITLE>&HTML_TITLE</TITLE>
**
** THEN THE PROGRAM WOULD READ THE FILE IN AND RESOLVE IT SO THAT THE OUTPUT
** LOOKED LIKE THIS:
**
** <TITLE>ROB</TITLE>
**
** ... WHEN THE MACRO VARIABLE "HTML_TITLE" EXISTED AND CONTAINED A VALUE OF
** "ROB". THIS IS USEFUL WHEN YOU NEED TO CREATE "DYNAMIC" HTML FILES FROM
** SAS BUT DONT WANT TO DO IT FROM A DATASTEP USING PUT STATEMENTS. DOING
** IT THIS WAY IS MUCH CLEANER.
**
** PARAMETERS: NONE
**
******************************************************************************
** HISTORY:
** 1.0 MODIFIED: 22-JUL-2010 BY:RP
** - CREATED.
** 1.1 MODIFIED: 18-FEB-2011 BY:RP
** - ADDED LRECL OF 32K TO STOP TRUNCATION
*****************************************************************************/
%macro resolve_file(iFileIn=, iFileOut=);
data _null_;
length line $32767;
infile "&iFileIn" truncover lrecl=32767;
file "&iFileOut" lrecl=32767;
input;
line = resolve(_infile_);
len = length(line);
put line $varying. len;
run;
%mend;
創建一些測試數據。還可以創建一些命令來調用上面的宏和數據集的值傳遞:
data mydata;
attrib name length=$10 format=$10. label='FirstName'
blah length=6 format=comma6. label='SomeValue'
cmd1 length=$1000
cmd2 length=$1000
;
title = 1;
name = "Rob" ;
blah = 1000;
cmd1 = cats('%let title=',title,';',
'%let name=',name,';',
'%let blah=',blah,';');
cmd2 = cats('%resolve_file(iFileIn=c:\template.html, iFileOut=c:\result',title,'.html);');
output;
title = 2;
name = "Pete";
blah = 100 ;
cmd1 = cats('%let title=',title,';',
'%let name=',name,';',
'%let blah=',blah,';');
cmd2 = cats('%resolve_file(iFileIn=c:\template.html, iFileOut=c:\result',title,'.html);');
output;
run;
使用call execute
運行,我們在之前的數據集創建的CMD1和CMD2。我們必須一次只在一行上執行調用執行,以便使用正確的宏變量,因此可以使用循環。
proc sql noprint;
select count(*) into :nobs from mydata;
quit;
然後通過DataSet迭代同時執行的命令之一,並建立每行到一個新文件:使用您的首選技術首先計算數據集中的行數
%macro publish;
%local tmp;
%do tmp = 1 %to &nobs;
data _null_;
set mydata(firstobs=&tmp obs=&tmp);
call execute (cmd1);
call execute (cmd2);
run;
%end;
%mend;
%publish;
那應該做的伎倆。
這真的很酷。我不知何故錯過了這個答案(並通過導出數據解決了我的問題,然後使用一個Excel工作簿來生成表格),但我不得不再次做類似的事情,並且肯定會嘗試這種方法。 – orh 2012-08-20 22:00:06
謝謝 - 以防萬一你最終使用它,我已經更新了'%resolve_file'宏,並修復了一些錯誤並增強了輸出HTML的可讀性(它現在保留了輸出文件中的前導空格)。 – 2012-08-21 01:19:13