Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sas 特殊条件和__Sas - Fatal编程技术网

Sas 特殊条件和_

Sas 特殊条件和_,sas,Sas,我有一个关于SAS编程的问题。它是关于条件和的。但这对我来说非常具体。因此,我想问一个例子。我有以下数据集: Group A Quantity 1 10 7 1 8 4 1 7 3 1 10 5

我有一个关于SAS编程的问题。它是关于条件和的。但这对我来说非常具体。因此,我想问一个例子。我有以下数据集:

  Group         A      Quantity             
    1          10          7           
    1          8           4           
    1          7           3           
    1          10          5           
    2          11          6           
    2          13          8           
    2          9           7           
    2          13          9           
我想在此数据集中再添加两列。新数据集应为:

  Group         A      Quantity         B     NewColumn    
    1          10          7           10         12  (7+5)
    1          8           4           10         12
    1          7           3           10         12
    1          10          5           10         12
    2          13          6           13         15   (6+9)        
    2          10          8           13         15
    2          9           7           13         15
    2          13          9           13         15
因此,B列应该等于每组的最大值,并且对于每组的所有观察值都是相同的。在本例中,组号1有4个值。他们是10,8,7,10。这些值中的最大值为10。因此,第一组B列的观测值均等于10。组编号2的最大编号为13。因此,第二组B列的观测值均等于13

C列更复杂。它的值取决于所有列的值。与B列相似,在组内也是相同的。更详细地说,它是数量栏中具体观测值的总和。这些具体观察结果应属于每组中具有最大值的观察结果。在我们的示例中,第一组为12。原因是,第一组的最大数量为10。属于10的数量是7和5。这些的总和是12。第二组为15。因为第二组的最大值是13,属于13的数量是6和9。所以总数是15。
我希望如此。我可以解释。非常感谢

您可以使用
proc-sql
执行此操作:

proc sql;
    select t.*, max_a as b,
           (select sum(t2.quantity)
            from t t2
            where t2.group = t.group and t.a = max_a
           ) as c
    from t join
         (select group, max(a) as max_a
          from t
          group by group
         ) g
         on t.group = g.group;
run;
如果数据来自底层数据库,则大多数数据库都支持窗口函数,这使得操作更简单。

这是未经测试的(我不使用sas),可能会出错,但三重循环应该可以工作。一次通过获取每组的最大值,第二次通过获取总和,第三次通过输出记录。比如:

data want ;
  do until(last.group) ; 
    by group ;
    set have ;
    B=max(A,B) ;
  end ;

  do until(last.group) ;
    set have ;
    by group ;
    if A = B then NewColumn = sum(NewColumn, Quantity) ;
  end;

  do until(last.group);
    set have ;
    by group;
    output ;
  end ;
run;