确定不同组的变化率 [英] Determine rates of change for different groups

查看:31
本文介绍了确定不同组的变化率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 SAS 问题,我知道对于熟悉数组编程的 SAS 用户来说可能相当简单,但我对这方面不熟悉.

I have a SAS issue that I know is probably fairly straightforward for SAS users who are familiar with array programming, but I am new to this aspect.

我的数据集如下所示:

Data have;          
Input group $ size price;
Datalines;
A 24 5
A 28 10
A 30 14
A 32 16
B 26 10
B 28 12
B 32 13
C 10 100
C 11 130
C 12 140
;
Run;

我想要做的是确定家庭中前两个项目的价格变化率,并将该比率应用于家庭中的所有其他成员.

What I want to do is determine the rate at which price changes for the first two items in the family and apply that rate to every other member in the family.

所以,我最终会得到这样的结果(仅适用于 A...):

So, I’ll end up with something that looks like this (for A only…):

Data want;         
Input group $ size price newprice;
Datalines;
A 24 5 5 
A 28 10 10
A 30 14 12.5
A 32 16 15
;
Run;

推荐答案

您需要学习的技术是 retaindiff/lag.两种方法都可以在这里工作.

The technique you'll need to learn is either retain or diff/lag. Both methods would work here.

以下说明了解决此问题的一种方法,但您需要额外的工作来处理大小不变(意味着分母为 0)和其他潜在异常等情况.

The following illustrates one way to solve this, but would need additional work by you to deal with things like size not changing (meaning a 0 denominator) and other potential exceptions.

基本上,我们使用保留来使值跨记录持久化,并在计算中使用它.

Basically, we use retain to cause a value to persist across records, and use that in the calculations.

data want;
  set have;
  by group;
  retain lastprice rateprice lastsize;
  if first.group then do;
    counter=0;
    call missing(of lastprice rateprice lastsize); *clear these out;
  end;
  counter+1;                                       *Increment the counter;
  if counter=2 then do;
    rateprice=(price-lastprice)/(size-lastsize);   *Calculate the rate over 2;
  end;
  if counter le 2 then newprice=price;             *For the first two just move price into newprice;
  else if counter>2 then newprice=lastprice+(size-lastsize)*rateprice; *Else set it to the change;
  output;
  lastprice=newprice;        *save the price and size in the retained vars;
  lastsize=size;
run;

这篇关于确定不同组的变化率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆