Pergunta

I have start and end weeks for a given customer and I need to make panel data for the weeks they are subscribed. I have manipulated the data into an easy form to convert, but when I transpose I do not get the weeks in between start and end filled in. Hopefully an example will shed some light on my request. Weeks start at 0 and end at 61, so forced any week above 61 to be 61, again for simplicity. Populate with a 1 if they are subscribed still and a blank if not.

ID  Start_week   End_week

1       6           61
2       0           46
3       45          61

what I would like

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 
Foi útil?

Solução

I see two ways to do it. I would go for an array approach, since it will probably be the fastest (single data step) and is not that complex:

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;

Alternatively, you can prepare a table for the transpose to work by creating one record per week per 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;

Outras dicas

Use an array to create the variables. The one gotcha is SAS arrays are 1 indexed.

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;

I have no working syntax but a guideline for you.

first make a table with CTE or physically with the numbers 0 to 61 as rows. Then join this table with the subscribed table. Something like

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

Now you will have a row for every week a customer is subscribed. Transpose that and you will have the in between weeks also filled in.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top