*create a dataset;
data tblTrial;
do i=1 to 20;
x=i;
if ^mod(x,5) then x=.;
output;
end;
drop i;
run;
proc print;run;
*get means and differences;
data tblTrial_1;
set tblTrial end=endof;
k=10;*assume k=10;
retain Ntot NnmissK NnmissK_N nSumK nSumK_N 0;
Ntot+1;
if ^missing(x) then do;*excluding missing values;
if Ntot<=k then do;
NnmissK+1;
nSumK=sum(x,nSumK);
end;
else do;*after k;
NnmissK_N+1;
nSumK_N=sum(x,nSumK_N);
end;
end;
if endof then do;
if NnmissK then nMeanK=nSumK/NnmissK;
else do;
nSumK=.;
nMeanK=.;
end;
if NnmissK_N then nMeanK_N=nSumK_N/NnmissK_N;
else do;
nSumK_N=.;
nMeanK_N=.;
end;
nDiff=nMeanK-nMeanK_N;
output;
end;
drop x;
run;
*very good, but not work for this dataset;
data ahuige;
input A B;
cards;
. 1
2 3
5 7
7 9
;run;
%let k=2;
%macro cal(VAL); sum((monotonic()<=&k)*&val)/sum(monotonic()<=&k)-sum((monotonic()>&k)*&val)/sum(monotonic()>&k)%mend;
proc sql;
select distinct %CAL(A) as AA,%CAL(B) as BB
from ahuige;
;