0

我使用了以下代码,它运行良好,得到以下结果:

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 语句会有所帮助,但没有

4

3 回答 3

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 ;
于 2014-09-04T09:44:22.853 回答
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;

我看到的唯一“规则”是您希望保留变量的最后一个非零值。只要您的原始数据按呈现顺序排列,它就应该可以工作。

于 2013-10-17T00:33:25.977 回答
0

像这样的东西应该工作。如果 last.account_number 则输出,并使用 retain 跨行保留值。如果没有丢失,我使用合并设置为零,您可以通过几种不同的方式来做到这一点。

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;
于 2013-10-16T17:55:07.343 回答