如何在SAS webout文件

时间:2017-11-30 07:28:58

标签: sql sas sas-macro

我创建了一个数据集,如下所示

Custom_Data_Set
Branch | Code | Region | Col_1 | Col_2 | Fee_1 | Fee_2 | Total
 1st      01     East     val     val     150     250     400
 1st      01     East     val     val     100     120     220
 1st      01     East     val     val     130     120     250
 2nd      02     West     val     val     200     250     450
 2nd      02     West     val     val     300     400     700

不,我想将这些数据打印到我的网络文件中,如

Branch: 1st  Code: 01  Region: East
  Col_1 | Col_2 | Fee_1 | Fee_2 | Total
   val     val    150     250     400
   val     val    100     120     220
   val     val    130     120     250

   Total          380     490     870
Branch: 2nd  Code: 02  Region: West
  Col_1 | Col_2 | Fee_1 | Fee_2 | Total
   val     val    200     250     450
   val     val    300     400     700

   Total          500     650    1150

 GrandTotal       880     1140   2020

我可以使用

进行简单的打印
data _null_;
put '<html><body><table>';
run;

data _null_;
 put numberOfObservations=;
 set work.Custom_Data_Set nobs=numberOfObservations;
 file _webout; 
 put '<tr>
         <td>'; put Branch ; put'</td>
         <td>'; put Code ; put'</td>
         <td>'; put Col_1 ; put'</td>
         <td>'; put Col_2 ; put'</td>
         <td>'; put Fee_1 ; put'</td>
         <td>'; put Fee_2 ; put'</td>
         <td>'; put Total; put'</td>
         </tr>';
run;

data _null_;
put '<tr>
         <td>Total</td>
         <td></td>
         <td></td>
         <td></td>
         <td>'; put &Fee_1_total ; put'</td>
         <td>'; put &Fee_2_total ; put'</td>
         <td>'; put &_total; put'</td>
         </tr>
    </table></body></html>';
run;

但是我在打印总数和总计时只打印了一个分支条目,编码整个组的区域时遇到了问题。

我们可以使用分支和代码进行分组。

PS:Fee_1_total,Fee_2_total和_total是我通过proc sql进入语句并将它们指定为sum(relevent_column)的宏变量。

2 个答案:

答案 0 :(得分:0)

引用宏观分辨率。

put "&Fee_1_total";

put语句需要变量或带引号的字符串。您发布的代码会导致DATA Step put语句的上下文中已解析的宏变量为数字。

PROC REPORT也可以产生相同的输出:

data have;
input 
Branch : $3. Code : Region : $4. Col_1 : $3. Col_2 : $3. Fee_1 Fee_2 Total;
datalines;
 1st      01     East     val     val     150     250     400
 1st      01     East     val     val     100     120     220
 1st      01     East     val     val     130     120     250
 2nd      02     West     val     val     200     250     450
 2nd      02     West     val     val     300     400     700
run;

options nocenter;

title;
proc report data = have;
  by branch code region;
  column col_1 col_2 fee_1 fee_2 total;
  rbreak after / summarize;
run;

* same with variation in titling;
title "#byvar1: #byval1 #byvar2: #byval2 #byvar3: #byval3";
options nobyline;
proc report data = have;
  by branch code region;
  column col_1 col_2 fee_1 fee_2 total;
  rbreak after / summarize;
  format code z2.;
run;

options byline;

答案 1 :(得分:0)

您可以在一个数据步骤中完成整个过程。无需宏变量或PROC SQL汇总代码。只需确保数据按分组变量排序即可。

您可以使用DOW循环为每个BY组迭代数据步骤一次。在_N_=1语句中使用end=set选项创建的变量来知道何时写入开头和结尾。使用first.branch知道何时写入块标题行。您可以将总和自己累积到新的数据集变量中,并输出分组中最后一行之后的总行数。

data _null_;
  file _webout ;
  if _n_=1 then do;
    putlog nobs= comma20. ;
    put
  '<html><body><table>'
    ;
  end;
  do until (last.region) ;
    set have nobs=nobs end=eof;
    by branch code region ;
    if first.region then put
  '<tr><th colspan="5">' Branch= code= region= '</th></tr>'
 /'<tr><th>Col_1</th><th>Col_2</th><th>Fee_1</th><th>Fee_2</th><th>Total</th></tr>'
    ;
    put
  '<tr><td>' col_1 '</td>'
      '<td>' col_2 '</td>'
      '<td>' fee_1 '</td>'
      '<td>' fee_2 '</td>'
      '<td>' total '</td></tr>'
    ;
    sum_1 = sum(sum_1,fee_1);
    sum_2 = sum(sum_2,fee_2);
    sum_t = sum(sum_t,total);
  end;
  put
  '<tr><td colspan="2">Total</td>'
      '<td>' sum_1 '</td>'
      '<td>' sum_2 '</td>'
      '<td>' sum_t '</td></tr>'
   ;
   grand_1+sum_1;
   grand_2+sum_2;
   grand_total+sum_t;
   if eof then put
  '<tr><td colspan="2">GrandTotal</td>'
      '<td>' grand_1 '</td>'
      '<td>' grand_2 '</td>'
      '<td>' grand_total '</td></tr>'
/ '</table></body></html>'
   ;
run;

我将发布的数据复制到数据步骤中以创建要编程的内容。  我将COL_1和COL_2的值更改为不同。

在IE中打开时,结果表看起来像这样。

enter image description here