1

我有给定客户的开始和结束周,我需要为他们订阅的周制作面板数据。我已将数据处理成易于转换的形式,但是当我转置时,我没有填写开始和结束之间的几周。希望一个例子能对我的请求有所帮助。周从 0 开始,到 61 结束,因此为了简单起见,强制任何高于 61 的周为 61。如果他们仍然订阅,则填充 1,否则填充空白。

ID  Start_week   End_week

1       6           61
2       0           46
3       45          61

我想要什么

ID   week0  week1  ... week6 ... week45 week46  week47 ... week61
1      .      .    ...   1   ...    1      1       1   ...    1 
2      1      1    ...   1   ...    1      1       0   ...    0
3      0      0    ...   0   ...    1      1       1   ...    1 
4

3 回答 3

2

使用数组创建变量。一个问题是 SAS 数组是 1 索引的。

data input;
input ID  Start_week   End_week;
datalines;
1       6           61
2       0           46
3       45          61
;

data output;
array week[62] week0-week61;
set input;
do i=1 to 62;
    if i > start_week and i<= (end_week+1) then
        week[i] = 1;
    else
        week[i] = 0;
end;
drop i;
run;
于 2013-09-25T13:26:11.350 回答
2

我看到了两种方法。我会采用数组方法,因为它可能是最快的(单个数据步骤)并且并不那么复杂:

data RESULT (drop=start_week end_week);
    set YOUR_DATA;
    array week_array{62} week0-week61;
    do week=0 to 61;
        if week between start_week and end_week then week_array[week+1]=1;
        else week_array[week+1]=0;
    end;
run;

或者,您可以通过为每个 id 每周创建一条记录来准备一个表以供转置工作:

data BEFORE_TRANSPOSE (drop=start_week end_week);
    set YOUR_DATA;
    do week=0 to 61;
        if week between start_week and end_week then subscribed=1;
        else subscribed=0;
        output;
    end;
run;
于 2013-09-25T13:27:02.770 回答
0

我没有有效的语法,但给你一个指导方针。

首先用 CTE 或以 0 到 61 的数字作为行来制作一个表。然后将此表与订阅的表连接起来。就像是

FROM sub
INNER JOIN CTE 
ON CTE.week BETWEEN sub.Start_week AND sub.End_week

现在,您每周都会有一个客户订阅的行。转置它,您将同时填写几周之间的时间。

于 2013-09-25T13:17:02.203 回答