a********a 发帖数: 346 | 1 I want the following data set tranfer into another data set.
Id pre_status end_status day
1 0 1 0
1 1 2 210
1 2 4 252
I.e. the pre_status 0 in not included, and the day is transposed into two
columns.
I want the data transfer like the following:
Id pre_status end_status start_day, end_day
1 1 2 0 210
1 2 |
A*******s 发帖数: 3942 | 2 how do you get the value of start_day?
【在 a********a 的大作中提到】 : I want the following data set tranfer into another data set. : Id pre_status end_status day : 1 0 1 0 : 1 1 2 210 : 1 2 4 252 : I.e. the pre_status 0 in not included, and the day is transposed into two : columns. : I want the data transfer like the following: : Id pre_status end_status start_day, end_day : 1 1 2 0 210
|
a********a 发帖数: 346 | 3 start_day from the variable day, first start_day is from the first
observation, day=0, end_day is the second row. Then this end_day will be a
start_day in second row. Is this clear? Thanks |
A*******s 发帖数: 3942 | 4 data new;
set old;
retain lag_day;
start_day=lag_day;
end_day=day;
lag_day=day;
if pre_status ne 0;
drop day lag_day;
run;
【在 a********a 的大作中提到】 : start_day from the variable day, first start_day is from the first : observation, day=0, end_day is the second row. Then this end_day will be a : start_day in second row. Is this clear? Thanks
|
a********a 发帖数: 346 | 5 Thanks actuaries. Excellent data programming technique. |
d*******o 发帖数: 493 | 6 data one;
input Id pre_status end_status end_day;
start_day=lag(end_day);
if end_day ne 0 then output;
cards;
1 0 1 0
1 1 2 210
1 2 4 252
; |
w*******n 发帖数: 469 | 7 I like this one more.
【在 d*******o 的大作中提到】 : data one; : input Id pre_status end_status end_day; : start_day=lag(end_day); : if end_day ne 0 then output; : cards; : 1 0 1 0 : 1 1 2 210 : 1 2 4 252 : ;
|
S******y 发帖数: 1123 | 8 # It can be done in Python too. Furthermore, we also check data -
# if first record of a new id has non-zero pre_status code, raise error
# if start date > end date, raise error
in_file = 'D:\\startdt.txt'
f = open(in_file, 'r')
prev_day = None
prev_id = None
print 'Id, pre_status, end_status, start_day, end_day'
for line in f:
idx, pre_status, end_status, day = line.split()
if pre_status == '0':
pass
else:
if (prev_id == None) or (prev_id != None and prev_id != idx): |