[英]SAS: Looping through a table, and inserting rows
在這里的初學者,我希望這不會引起一個問題。
背景:我有一個表,其中的Inventor ID鏈接到Years。 這代表了發明人注冊專利的所有年份。
PROC SQL;
CREATE TABLE Inv_Year AS
SELECT a.InventorID, a.Year
FROM Inv_Year_Pat AS a;
QUIT;
所以行看起來像這樣
InventorID | Year
1 1989
2 2002
3 1990
3 1992
3 1994
任務:1990年,1992年和1994年發明的發明人3號。我需要填寫發明人活躍年之間的任何年份。 結果是...
InventorID | Year
1 1989
2 2002
3 1990
3 1991
3 1992
3 1993
3 1994
如何在SAS中解決此問題?
data want ; set have ; by InventorID ; prevYear = lag(Year) ; if first.InventorID then prevYear = . ; /* If not first row per InventorID and difference is > 1 year */ if not missing(prevYear) and sum(Year,-prevYear) > 1 then do ; /* Store current Year */ thisYear = Year ; /* Insert missing years */ do Year = prevYear + 1 to thisYear ; output ; end ; end ; else output ; drop prevYear thisYear ; run ;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.