我使用了以下代码,它可以完美地运行,以获得以下结果:
data No_int_weeksPaid;
set no_internet4;
keep account_number week0-week61;
by account_number;
array week{62} week0-week61;
do i = 1 to 62;
if i > subscription_start and i <= (subscription_end+1) then
week{i} = weeks_paid ;
else
week{i} = 0;
end;
drop i;
run;
给我这样的东西:
Account# Week0 week1 week2 week3 week4
1 0 1 1 1 1
1 0 0 0 5 5
2 1 1 1 1 1
2 0 2 2 2 2
2 0 0 0 4 4
我希望将所有帐户#放在一行并覆盖这些值,以便我得到这样的内容:
Account# Week0 week1 week2 week3 week4
1 0 1 1 5 5
2 1 2 2 4 4
我认为by语句会有所帮助,但不是
答案 0 :(得分:0)
这样的事情应该有效。如果是last.account_number则输出,并使用retain来保持跨行的值。如果没有丢失,我使用coalesce设置为零,你可以用几种不同的方式做到这一点。
data No_int_weeksPaid;
set no_internet4;
keep account_number week0-week61;
retain week0-week61; **CHANGED**
by account_number;
array week{62} week0-week61;
do i = 1 to 62;
if i > subscription_start and i <= (subscription_end+1) then
week{i} = weeks_paid ;
else
week{i} = coalesce(week[i],0); **CHANGED**
end;
drop i;
if last.account_number then output; **CHANGED**
run;
答案 1 :(得分:0)
假设我明白你想做什么,试试这个:
data have;
input Account Week0 week1 week2 week3 week4;
datalines;
1 0 1 1 1 1
1 0 0 0 5 5
2 1 1 1 1 1
2 0 2 2 2 2
2 0 0 0 4 4
run;
data want;
set have(rename=(Week0=oWeek0 Week1=oWeek1 Week2=oWeek2
Week3=oWeek3 Week4=oWeek4));
by account;
retain Week0 Week1 Week2 Week3 Week4;
array new{*} Week0 Week1 Week2 Week3 Week4;
array old{*} oWeek0 oWeek1 oWeek2 oWeek3 oWeek4;
keep Account Week0 Week1 Week2 Week3 Week4;
if First.account then
do i=1 to dim(new);
new{i} = old{i};
end;
else do i=1 to dim(new);
if old{i} ne 0 then new{i} = old{i};
end;
if last.Account;
run;
我看到的唯一“规则”是你希望保留变量的最后非零值。只要您的原始数据按照提供的顺序排列,它就应该有效。
答案 2 :(得分:0)
如果你想要它作为proc sql
,可能最容易构建一个快速宏来为你做迭代:
%MACRO Week(W) ;
%DO N=1 %TO &W ;
max(Week&N) as Week&N,
%END ;
0 as _null_
%END ;
proc sql ;
create table output as
select Account, %Week(61)
from No_int_weeksPaid
group by Account
;
quit ;