问题描述
所以我有以下三个变量的数据集:帐户,余额和时间。 账户余额时间
1 110 01/2006
1 111 02/2006
1 88 03/2006
1 61 04/2006
1 1203 05/2006
2 112 01/2006
2 111 02/2006
2 665 03/2006
2 61 04/2006
2 1243 05/2006
3 110 01/2006
3 111 02/2006
3 88 03/2006
3 61 04/2006
3 1203 05/2006
每个帐户都有更多记录。所以开始时间可能在我写的东西之前,结束时间可能在我写的东西之后。
所以我的问题是:
我试图找到最初12个月的每个帐户的最大余额。例如,对于05/2006的帐户3,我试图找到最大值(2006年4月的帐户3余额,2006年3月的帐户3余额,2006年3月的帐户3余额,........ .....,帐户3余额在2006年4月)。
你的想法是什么?我所做的就是对数组使用滞后函数。然而,它并不那么高效。因为如果我需要前120个月,我会遇到麻烦。
谢谢。
Best
Xintong
options mprint;
%宏观滞后;
data temp(drop = count i);
设定余额;
by acct time;
array x(*)lag_bal1 - lag_bal3;
%我= 1%到3;
lag_bal& i = lag& i。(余额);
%end;
if first.acct then count = 1;
do i = count to dim(x);
致电失踪(x(i));
end;
count +1;
max_ball = max(balance_,lag_bal1-lag_bal3);
%修正;
%滞后;
so i have the following dataset with three variables: account, balance, and time.
account balance time
1 110 01/2006
1 111 02/2006
1 88 03/2006
1 61 04/2006
1 1203 05/2006
2 112 01/2006
2 111 02/2006
2 665 03/2006
2 61 04/2006
2 1243 05/2006
3 110 01/2006
3 111 02/2006
3 88 03/2006
3 61 04/2006
3 1203 05/2006
each account has more records. so the starting time might be before what I wrote and the ending time might be after what i wrote.
so my question is:
I am trying to find the maximum balance for each account in prievious 12 monthes. For example, for account 3 on 05/2006, i am trying to find max(account 3 balance at 04/2006, account 3 balance at 03/2006, account 3 balance at 03/2006,............., account 3 balance at 04/2006).
what is in your mind? what i did is to use lag function with array. however, it is NOT so efficient. because i will be in trouble if i need previous 120 months.
Thank you.
Best
Xintong
options mprint;
%macro lag;
data temp (drop=count i);
set balance;
by acct time;
array x(*) lag_bal1 - lag_bal3;
%do i=1 %to 3;
lag_bal&i=lag&i.(balance);
%end;
if first.acct then count=1;
do i=count to dim(x);
call missing(x(i));
end;
count+1;
max_ball=max(balance, of lag_bal1-lag_bal3);
%mend;
%lag;
这篇关于如何在sas中动态改变观察值来找到最大值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!