简体   繁体   English

SAS:直到循环并丢弃观察值

[英]SAS: Do until loop and dropping observations

My dataset has the following variables: 我的数据集具有以下变量:

Actual 实际
Expected 预期

First of all, I want to create a new variable 'Ratio' = sum(Actual)/sum(Expected). 首先,我要创建一个新变量'Ratio'= sum(Actual)/ sum(Expected)。 For the first observation I want this expression to include all of the values of Actual and Expected. 对于第一个观察,我希望该表达式包含所有的Actual和Expected值。 For the second iteration, I want to repeat this expression using only the second observation and onwards. 对于第二次迭代,我只想使用第二个观察值及以后重复此表达式。 Then for the third iteration, I want to include the third observation and onwards. 然后对于第三次迭代,我想包括第三次观察及以后。

This is an example of the data and the formula as it would look in Excel. 这是数据和公式的示例,就像在Excel中一样。

actual  expected    ratio  
15      33          =SUM(A2:$A$6)/SUM(B2:$B$6)  
10      50          =SUM(A3:$A$6)/SUM(B3:$B$6)  
20      64          =SUM(A4:$A$6)/SUM(B4:$B$6)  
60      77          =SUM(A5:$A$6)/SUM(B5:$B$6)  
45      81          =SUM(A6:$A$6)/SUM(B6:$B$6)  

Secondly, the dataset could have any number of observations so I would like the expression to continue until the 'Ratio' is below a value that I manually specify. 其次,数据集可以具有任意数量的观察值,因此我希望表达式继续,直到“比率”低于我手动指定的值为止。 Ideally the program will also incorporate the creation of a fourth variable with the number of observations excluded from the expression. 理想情况下,该程序还将合并第四个变量的创建,并从表达式中排除观察值的数量。 So for the first observation it would be '0', the second observation '1', the third observation '2' and so on. 因此,对于第一个观察值,它应该是“ 0”,第二个观察值是“ 1”,第三个观察值是“ 2”,依此类推。

I think this will need an array too? 我认为这也需要一个数组吗? Thanks in advance. 提前致谢。

You're asking for code to be written for you, which is off topic (rather than asking for help with code you've written); 您正在要求为您编写代码,这是不合时宜的(而不是寻求有关已编写代码的帮助); however, you do in part have an interesting question as far as technique; 但是,就技术而言,您确实有一个有趣的问题; I'll explain that technique, and if you need help with the technique adjust your question or ask a new one. 我将解释该技术,如果您需要有关该技术的帮助,请调整您的问题或提出新的问题。

What you'll want to do here is reverse sort your data, if possible. 如果可能的话,您想要在此处对数据进行反向排序。 If it doesn't have anything to sort by, you can either add an observation counter: 如果没有任何排序依据,则可以添加一个观察计数器:

data temp;
  set have;
  obs=_n_;
run;

Or you can use POINT to reverse-order your dataset, though this is somewhat slower. 或者,您可以使用POINT来对数据集进行反向排序,尽管这有些慢。 This is the basic form of the latter technique - if you can reverse sort, then you don't need the do loop or the set options. 这是后一种技术的基本形式-如果您可以反向排序,则不需要do循环或set选项。

data want;
  do obsnum = nobs to 1 by -1;
    set have nobs=nobs point=obsnum;
    act_sum+actual;   *accumulator for actual;
    exp_sum+expected; *accumulator for expected;
    ratio=act_sum/exp_sum;
  end;
run;

From here you should be able to work out the rest of your question. 从这里,您应该可以解决其余的问题。 You will of course have to re-sort by the proper sort method afterwards. 之后,您当然必须通过适当的排序方法进行重新排序。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM