如何在SAS中检查一个变量增加时另一个变量也增加。

huangapple go评论65阅读模式
英文:

How to check when one variable increases the other also increases in SAS

问题

我需要检查当一个变量增加时,另一个变量必须随之增加(并在每个组内产生一个不遵循此趋势的标志)。

具体来说,我正在尝试在第一组内检查,较高的等级必须具有较高的比率,对于第二组、第三组等等也是如此。当数据不符合这个趋势时,例如在第一组中,类别3的比率低于类别2的比率,我想创建另一个列来标记这个问题。

data work.tst;
    input group class rate;
    cards;
1 1 0.1
1 2 0.5
1 3 0.45
1 4 0.7
2 1 1
2 2 2
2 3 4
2 4 6
;
run;

我不知道如何获取这个结果。任何帮助都将不胜感激。谢谢!

英文:

I need to check when one variable increases, the other variable has to increase (and produce a flag where it does not follow this trend) within each group.

Specifically, I'm trying to check within group 1, higher class must have higher rate, and same for group 2,3,..,n. When data does not follow this trend, e.g. class 3 rate is lower than class 2 rate in group 1, I want to create another column to flag the issue.

data work.tst;
    input group class rate;
    cards;
1 1 0.1
1 2 0.5
1 3 0.45
1 4 0.7
2 1 1
2 2 2
2 3 4
2 4 6
;
run;

I don't know how to obtain this. Any helps is appreciated. Thanks!

答案1

得分: 2

以下是翻译结果:

听起来数据是按照组和类别排序的,你想测试速率是否在增加。

你可以使用 DIF() 函数来计算观察值之间速率的变化(实际上是在调用 DIF() 函数之间的变化)。然后,对于增加,你希望变化是一个正值。

示例:

data want;
  set tst ;
  by group class;
  change=dif(rate);
  if first.group then change=.;
  decreasing = . < change < 0 ;
run;

结果:

 Obs    group    class    rate    change    decreasing

  1       1        1      0.10       .           0
  2       1        2      0.50      0.40         0
  3       1        3      0.45     -0.05         1
  4       1        4      0.70      0.25         0
  5       2        1      1.00       .           0
  6       2        2      2.00      1.00         0
  7       2        3      4.00      2.00         0
  8       2        4      6.00      2.00         0

如果你希望对于没有变化的情况也进行标记,那么请使用 <= 0

英文:

Sounds like the data is SORTED by GROUP and CLASS and you want to test if RATE is increasing or not.

You can use the DIF() function to calculate the change in RATE between observations (actually between the calls to the DIF() function). Then for increasing you will want the change to be a positive value.

Example:

data want;
  set tst ;
  by group class;
  change=dif(rate);
  if first.group then change=.;
  decreasing = . &lt; change &lt; 0 ;
run;

Result

Obs    group    class    rate    change    decreasing

 1       1        1      0.10       .           0
 2       1        2      0.50      0.40         0
 3       1        3      0.45     -0.05         1
 4       1        4      0.70      0.25         0
 5       2        1      1.00       .           0
 6       2        2      2.00      1.00         0
 7       2        3      4.00      2.00         0
 8       2        4      6.00      2.00         0

If you want no change to also be flagged then use <= 0 instead.

huangapple
  • 本文由 发表于 2023年6月21日 23:58:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76525158.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定