我有一个问题,我无法在Stackoverflow或其他地方找到解决方案,并且不确定是否可以通过SAS proc sql解决。
我的目标是基于数据集(a)和视图(b)生成视图(z)。问题在于,(a)可以在年初更新为实际年份,而(b)只能在该年后期更新。不过,我希望我的观点(z)在年初开始生成数据(即使是临时的),这自然也只有在(a)和(b)都有可用数据的情况下才有可能。所以我想要的是视图(z)使用(b)中的最新可用年份,并将其用作实际年份(因此基本上是将数据(b)中的数据推算为(a)中的最新数据年份。我尝试使用以下代码执行此操作,但是它并不能完全按照我想要的方式工作:
proc sql;
create view x1 as
select ste.jahr, ste.gnr, ste.einwg, stk.stkabs
from d18.fg_gji_steinh as ste
inner join
d18.fg_gji_skraft as stk
on
case when exists (select stkabs from d18.fg_gji_skraft where ste.jahr=stk.jahr)
then ste.jahr=stk.jahr and ste.gnr=stk.gnr
else input(ste.jahr,4.)=input(stk.jahr,4.)+1 and ste.gnr=stk.gnr
end
order by ste.jahr, ste.gnr ;
quit;
它会在第一年和最后一年生成预期数据,但不会在这几年之间生成数据,因为它会为一个观测结果生成两行。第一个观测值包含实际年份的数据,另一个观测值包含去年的数据。
有人知道如何解决此问题吗?
select a.jahr
, a.gnr
, a.einwg
, coalesce(b.stkabs,c.stkabs) as stkabs
from d18.fg_gji_steinh a
left join d18.fg_gji_skraft b
on a.gnr=b.gnr and a.jahr=b.jahr
left join d18.fg_gji_skraft c
on a.gnr=c.gnr and a.jahr=(c.jahr + 1)
/*
Input data to view: tables fg_gji_steinh and fg_gji_skraft,
the tax units and defintive tax force, respectively.
Data in fg_gji_skraft is missing in the last two years. This has to be filled
by the view with the last known data, in this example from year 2019.
Table fg_gji_steinh has to be joined with table fg_gji_skraft.
*/
data work.fg_gji_steinh;
jahr='2018'; gnr='1001'; einwg=1.5; output;
jahr='2018'; gnr='1002'; einwg=1.8; output;
jahr='2018'; gnr='1003'; einwg=2.0; output;
jahr='2019'; gnr='1001'; einwg=1.6; output;
jahr='2019'; gnr='1002'; einwg=1.8; output;
jahr='2019'; gnr='1003'; einwg=2.0; output;
jahr='2020'; gnr='1002'; einwg=1.8; output;
jahr='2020'; gnr='1010'; einwg=1.9; output;
jahr='2021'; gnr='1010'; einwg=2.1; output;
run;
data work.fg_gji_skraft;
jahr='2018'; gnr='1001'; stkabs=10; output;
jahr='2018'; gnr='1002'; stkabs=20; output;
jahr='2018'; gnr='1003'; stkabs=30; output;
jahr='2019'; gnr='1001'; stkabs=10; output;
jahr='2019'; gnr='1002'; stkabs=22; output;
jahr='2019'; gnr='1003'; stkabs=35; output;
jahr='2020'; gnr='1002'; stkabs=.; output;
jahr='2020'; gnr='1010'; stkabs=.; output;
jahr='2021'; gnr='1010'; stkabs=.; output;
run;
/*
Municipal merger format.
We assume that in 2018 there exist three municipalities: 1001, 1002, 1003.
The same in 2019.
At begin of year 2020 municipalities 1001 and 1003 merge to 1010.
At begin of year 2021 municipalities 1002 and 1010 merge to 1010.
*/
proc format;
value $cjrgdgdf
'20201001'='1010'
'20201002'='1002'
'20201003'='1010'
'20201010'='1010'
'20211001'='1010'
'20211002'='1010'
'20211003'='1010'
'20211010'='1010'
;
run;
/* definitive tax force */
%macro select1;
select
jahr,
gnr,
stkabs
from fg_gji_skraft
%mend select1;
/* join tax units and definitive tax force */
%macro select2;
select
t1.*,
t2.stkabs
from
fg_gji_steinh as t1
left join
( %select1 ) as t2
on t1.jahr=t2.jahr and t1.gnr=t2.gnr
%mend select2;
/* last data year of definitive tax force*/
%macro select3;
select max (jahr) as maxjahr
from ( %select1 )
where stkabs ne .
%mend select3;
/* last known definitive tax force */
%macro select4;
select
t1.jahr,
t1.gnr,
t1.stkabs
from ( %select2 ) as t1
where t1.jahr=( %select3 )
%mend select4;
/* convert last known tax force to territorial level of first missing data year. */
%macro select5a;
select
put(input(jahr,4.)+1,4.) as jahr,
put(calculated jahr||gnr,$cjrgdgdf.) as gnr,
sum(stkabs) as stkabs
from ( %select4 )
group by jahr, calculated gnr
%mend select5a;
/* convert last known tax force to territorial level of second missing data year. */
%macro select5b;
select
put(input(jahr,4.)+2,4.) as jahr,
put(calculated jahr||gnr,$cjrgdgdf.) as gnr,
sum(stkabs) as stkabs
from ( %select4 )
group by jahr, calculated gnr
%mend select5b;
/* join defintive tax force with provisional tax force. */
%macro select6;
select
t1.jahr,
t1.gnr,
coalesce(t1.stkabs, t2.stkabs, t3.stkabs) as stkabs
from
(%select2 ) as t1
left join
( %select5a ) as t2
on t1.jahr=t2.jahr AND t1.gnr=t2.gnr
left join
( %select5b ) as t3
on t1.jahr=t3.jahr AND t1.gnr=t3.gnr
%mend select6;
/* finally join tax units with tax force */
proc sql;
create view
fg_gji_steuerfuss_gb AS
select
t1.jahr,
t1.gnr,
t1.einwg,
t2.stkabs
from
fg_gji_steinh as t1,
( %select6 ) as t2
where
t1.jahr=t2.jahr and t1.gnr=t2.gnr
;
quit;