分位数相关问题(PROC UNIVARIATE)
(2014-08-07 11:41:01)
标签:
教育sas |
分类: 03SAS数据处理 |
编者按:求解分位数是我们在复杂一些的计算中经常要遇到的问题,我们的目标就是要
首先能够输出任何的分位数,这个可以使用 PROC UNIVARIATE
灵活输出,之后就是保存到一个数据集中供之后的分析使用。
当然,在我们这个帖子中,还展示了滚动 求解 分位数的一个例子,两大版主的 回复 都很精彩,我们不妨可以学习他们的
编程思路,来解决类似的问题。
proc univariate data=sashelp.class;
run;
proc univariate data=aa
var
cla_Ss_sum;
output out=a
pctlpre=p pctlpts=(30 70);
run;
run;
根据n和a计算v。。。。
data have;
input n a;
cards;
1 0.1
2 0.1
3 0.1
4 0.1
5 0.2
6 0.2
7 0.2
8 0.2
9 0.2
10 0.7
11 0.7
12 0.7
13 0.7
14 0.7
15 0.7
16 0.7
;
%macro lags(var,n);
lag(&var)
%do i=2 %to
&n;
,lag&i(&var)
%end;
%mend;
data wanted;
set have;
v=pctl(95,%lags(a,5));
if _n_<=5 then call
missing(v);
run;
data test;
do n=1 to
10000;
a=ranuni(0);
output;
end;
run;
data testl;
set
test;
v=pctl(95,%lags(a,100));
if _n_<=100 then call
missing(v);
run;
1 0.1
2 0.1
3 0.1
4 0.1
5 0.2
6 0.2
7 0.2
8 0.2
9 0.2
10 0.7
11 0.7
12 0.7
13 0.7
14 0.7
15 0.7
16 0.7
;
%macro lags(var,n);
%mend;
data wanted;
run;
data test;
run;
data testl;
run;
方案2:
这个解决方案 更加的高超!!!
if _n_ >5 then v =pctl(95, of t[*] );
t[5 -mod(_n_, 5)] =a;
run;
STATA :
gen x1 = .
levelsof year, local(year)
foreach x of local year {
summ var1 if year == `x', d
local a = r(p25)
local b= r(p75)
replace x1= cond(var1<`a', 0, cond(var1>`b', 1,.)) if year == `x'
}
levelsof year, local(year)
foreach x of local year {
summ var1 if year == `x', d
local a = r(p25)
local b= r(p75)
replace x1= cond(var1<`a', 0, cond(var1>`b', 1,.)) if year == `x'
}
clear
set obs 100
gen x1=_n
sort x1
local N=4
gen gg = group(`N')
forv i=1/`N' {
qui {
sum x1 if gg==`i'
sca max`i'=r(max)
}
di in yellow max`i'
}
sum x1 ,detail
di r(p25)
di r(p50)
di r(p75)
e.g
use C:\Stata12.1SE-2013-2-25\sim_data.dta,clear
bysort year: egen LRq33 = pctile(x1) if x1!=., p(33)
bysort year: egen LRq66 = pctile(x1) if x1!=., p(66)
gen qLR = 2
replace qLR=1 if x1<=LRq33
replace qLR=3 if x1>=LRq66
sort qLR year
tabstat x1 y , by(qLR) f(%4.3f)