【问题标题】:how to vertically sum a range of dynamic variables in sas?如何在sas中垂直求和一系列动态变量?
【发布时间】:2020-03-12 10:20:55
【问题描述】:

我在 SAS 中有一个数据集,其中的月份将每月动态更新。我需要每个月垂直计算总和并将总和粘贴到下面,如图所示。

Proc 意味着/ proc summary 和 proc print 对我没有用。

我之前得到了以下代码:

`%let month = month name;
%put &month.;

data new_totals;
set Final_&month. end=end;  
&month._sum + &month._final;    
/*feb_sum + &month._final;*/
output;                
if end then do;         
measure = 'Total';    
&month._final = &month._sum;  
/*Feb_final = feb_sum;*/
output;               
end;
drop &month._sum;   
run; `

问题是所有月份都被硬编码,这是我不想要的。我对循环或数组不太熟悉,所以需要一个解决方案,拜托。

enter image description here

【问题讨论】:

  • 您想要一个包含这些值的 SAS 数据集还是用于报告目的?
  • 我不明白。你的照片看起来就像打印出来的。试试proc print; var _all_; sum _numeric_;run;
  • @draycut 我需要它作为数据步骤,因为数据需要在以后的许多其他步骤中使用。

标签: sas


【解决方案1】:

最好使用PRINTREPORT 等报告程序来生成所需的输出。

data have;
  length group $20;
  do group = 'A', 'B', 'C';
    array month_totals jan2020 jan2019 feb2020 feb2019 mar2019 apr2019 may2019 jun2019 jul2019 aug2019 sep2019 oct2019 oct2019 nov2019 dec2019;
    do over month_totals;
      month_totals  = 10 + floor(rand('uniform', 60));
    end;
    output;
  end;
run;

ods excel file='data_with_total_row.xlsx';

proc print noobs data=have;
  var group ;
  sum jan2020--dec2019;  
run;

proc report data=have;
  columns group jan2020--dec2019;
  define group / width=20;
  rbreak after / summarize;
  compute after;
    group = 'Total';
  endcomp;
run;

ods excel close;

数据结构

您正在使用的数据集是“困难的”,因为数据的日期方面实际上在元数据中,即列名。在 SAS 中,一个更好的方法是也有一个带有列的分类数据

  • group(分类角色)
  • month(分类角色)
  • total(连续角色)

可以使用where 子句轻松过滤此类数据,并且REPORTTABULATE 等报告过程可以在class 语句中使用month 变量。

例子:

data have;
  length group $20;
  do group = 'A', 'B', 'C';
    do _n_ = 0 by 1 until (month >= '01feb2020'd);
      month = intnx('month', '01jan2018'd, _n_);
      total = 10 + floor(rand('uniform', 60));
      output;
    end;
  end;
  format month monyy5.;
run;

proc tabulate data=have;
  class group month;
  var total;
  table 
    group all='Total'
    ,
    month='' * total='' * sum=''*f=comma9.
  ;
  where intck('month', month, '01feb2020'd) between 0 and 13;
run;


proc report data=have;
  column group (month,total);
  define group / group;
  define month / '' across order=data ;
  define total / '' ;

  where intck('month', month, '01feb2020'd) between 0 and 13;
run;

【讨论】:

    【解决方案2】:

    这是一个基本的方法。借用 Richard 的样本数据。

    data have;
      length group $20;
      do group = 'A', 'B';
        array months jan2020 jan2019 feb2020 feb2019 mar2019 apr2019 may2019 jun2019 jul2019 aug2019 sep2019 oct2019 oct2019 nov2019 dec2019;
        do over months;
          months  = 10 + floor(rand('uniform', 60, 1));
        end;
        output;
      end;
    run;
    
    proc summary data=have;
        var _numeric_;
        output out=temp(drop=_:) sum=;
    run;
    
    data want;
        set have temp (in=t);
        if t then group='Total';
    run;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-21
      • 2021-04-30
      • 1970-01-01
      • 1970-01-01
      • 2012-06-03
      • 1970-01-01
      相关资源
      最近更新 更多