简体   繁体   English

如何创建一个新变量来计算特定变量之和(按ID),该变量包含SAS中的多个观察值?

[英]How can I create a new variable which calculates sum of a specific variable (by ID) containing multiple observation in SAS?

For example, I want to create a new dataset (Data2) from Data1. 例如,我想从Data1创建一个新的数据集(Data2)。

A new variable, cost in data2 is calculated as sum of multiple observation by ID in material of data1. 计算新变量data2中的成本,作为对data1物料中ID的多次观察总和。

(Data1) (数据1)

ID  material
1   4
1   4
1   4
2   2
2   4
2   4
3   2
3   6
3   6
4   5
4   5
4   5
4   5
5   2
5   4
5   4
5   8

(Data2) (DATA2)

ID  cost    
1   12  #4+4+4
2   10  #2+4+4
3   14  #2+6+6
4   20  #5+5+5+5
5   18  #2+4+4+8

I have used SAS EG version only for simple analysis, and recently I started to use proc sql procedure. 我仅使用SAS EG版本进行简单分析,最近我开始使用proc sql过程。 As a beginner in SAS coding (proc sql), it was very hard to approach the answer, for myself. 作为SAS编码(proc sql)的初学者,对于我自己来说很难找到答案。 Thank you very much, in advance. 提前非常感谢您。

Base SAS has several procedures that will present aggregated values over a group. 基本SAS有几个过程,将在一个组中显示汇总值。 MEANS , SUMMARY , and reporting procedures such as REPORT and TABULATE . MEANSSUMMARY和报告程序,如REPORTTABULATE The procedures can also save output data sets containing the computed aggregates. 该过程还可以保存包含计算出的聚合的输出数据集。

data have; input
ID  material_cost;datalines;
1   4
1   4
1   4
2   2
2   4
2   4
3   2
3   6
3   6
4   5
4   5
4   5
4   5
5   2
5   4
5   4
5   8
run;

title "Proc MEANS";
proc means data=have sum noNobs maxdec=0;
  class id;
  var material_cost;
run;

title "Proc SUMMARY";
proc summary data=have print sum noNobs maxdec=0;
  class id;
  var material_cost;
run;

title "Proc REPORT";
proc report data=have;
  columns id material_cost;
  define id / group;
run;

title "Proc TABULATE";
proc tabulate data=have;
  class id;
  var material_cost;
  table id, material_cost*sum / NoCellMerge;
run;

If you want to use PROC SQL , this is a straight forward use of GROUP BY 如果要使用PROC SQL ,这是GROUP BY的直接使用

proc sql;
  select id, sum(material) as sum from mydataset group by id;
  quit;

You could manually compute this in a datastep also if you don't want to use PROC SQL 如果不想使用PROC SQL也可以在数据步骤中手动计算

proc sort data=mydataset;
  by id;
run;

data sums;
  set mydataset;
  by id;

  if first.id then sum = 0;
  sum + material;
  if last.id then output;

  keep id sum;

run;

proc print data=sums;
run;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在SAS中的数据集之上添加新观察值? - How can I add a new observation on top of a data set in SAS? SAS:通过分组变量(不包括观察值)来计算平均值 - SAS: Compute mean by grouping variable excluding observation 在 SAS 中,如何选择 ID 组中其他变量之间具有特定关系的所有 ID 组? - In SAS, how can I select all the ID groups which has specific relationship between another variables within the ID group? 在定义新变量时,有没有办法在 SAS/SQL 中引用先前观察值? - Is there a way of referencing the prior observation's values in SAS/SQL when defining a new variable? SAS如何计算一个变量,该变量是每个观察值的所有其他观察值的平均值? - SAS how to calculate a variable that is the mean of the values of all the other observations for each observation? 如何通过另一个变量中的观察值对变量中的唯一值求和? - How can I sum the unique values in a variable by the observations in another variable? 如何创建一个触发器来计算从一个表到另一个表的具有匹配 id 的字段的总和? - how to create a trigger that calculates the sum of one field with matching id from one table to another? SAS创建可变频率 - SAS create a frequency of variable frequencies 我需要在 SAS 中使用 if 语句创建变量吗? - Do I need to create variable using if statement in SAS? 将包含多个 ID 号的 Python 变量传递到外部 BigQuery 脚本 - Pass a Python Variable containing multiple ID Numbers into external BigQuery Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM