[英]How to split one row into separate ones sas
我有一個數據集(表A),如下所示:
Location_id | Arrival_Date | Length_of_Stay | Demand |
------------+---------------+--------------- +----------+
L_1 | 23-JUL-16 | 1 | 5 |
L_1 | 23-JUL-16 | 2 | 7 |
L_1 | 23-JUL-16 | 3 | 8 |
L_1 | 23-JUL-16 | 4 | 3 |
L_1 | 24-JUL-16 | 1 | 3 |
L_1 | 24-JUL-16 | 2 | 2 |
L_1 | 24-JUL-16 | 3 | 4 |
L_1 | 25-JUL-16 | 1 | 5 |
........... ............ ............. .......
我想轉移到下表:
Location_id | Stay_Date | Demand |
L_1 | 23-JUL-16 |23= 5+7+8+3| (Arrival JUL-23, 4 Length_of_stays)
L_1 | 24-JUL-16 |27= 7+8+3+3+2+4| (Arrival JUL-24, 3 Length_of_stays and Arrival JUL-23, 3 Length_of_stays)
L_1 | 25-JUL-16 |22= 8+3+2+4+5|(JUL-23, 2 length_of_stays; JUL-24, 2 length_of_stays; JUL-25, 1 length_of_stay)
L_1 | 26-JUL-16 | 7 |(JUL-23, 1 length_of_stay)
需求以Stay_Date度量,該時間根據Arrival_Date和Length_of_Stay計算。
如何在SAS中制作? 謝謝!
您仍然沒有很好地解釋您的問題。 我的假設:
每行都記錄了一個中止以及該中止的需求水平。
如果停留時間為多天(停留時間> 1),則為停留的每一天創建一個記錄。
您還應該在數據步驟中過帳示例數據。
data have;
informat arrival_date anydtdte.;
format arrival_date date9.;
infile cards dlm='|';
input Location_id $ Arrival_Date Length_of_Stay Demand ;
cards;
L_1 | 23-JUL-16 | 1 | 5 |
L_1 | 23-JUL-16 | 2 | 7 |
L_1 | 23-JUL-16 | 3 | 8 |
L_1 | 23-JUL-16 | 4 | 3 |
L_1 | 24-JUL-16 | 1 | 3 |
L_1 | 24-JUL-16 | 2 | 2 |
L_1 | 24-JUL-16 | 3 | 4 |
L_1 | 25-JUL-16 | 1 | 5 |
;
run;
*Create a record for each day of the visit;
data temp;
set have;
format stay_date date9.;
do i=1 to length_of_stay;
stay_date=arrival_date-1 + i;
output;
end;
run;
*summarize the totals;
proc sql;
create table want as
select stay_date, sum(demand) as total_demand
from temp
group by stay_date
order by stay_date;
quit;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.