作者west1996 (焦了六年變脆了)
看板Statistics
標題Re: [程式] 如何在SAS中計算local max & local mini
時間Thu Sep 17 17:58:03 2009
※ 引述《eminem (阿姆)》之銘言:
: [軟體程式類別]:SAS
: [程式問題]:資料處理
: [軟體熟悉度]:
: 低(1~3個月)
: [問題敘述]:希望在一組股價的時間序列中,找出全部的local max 和 local mini.
: 比方說100 120 115 114 99 150 130 145 100,其中的local max為120,
: 150,145, 而local mini為99,130
: [程式範例]:
: -----------------------------------------------------------------------------
假設target是var1
data localmax(keep=temp2) localmin(keep=temp2);
retain temp1 temp2;
set XXX;
if _n_=1 then temp1=var1;
if _n_=2 then temp2=var1;
if _n_>3 then
do;
if (temp2-temp1)*(var1-temp2)<0 and (temp2-temp1)>0 then
output localmax;
else if (temp2-temp1)*(var1-temp2)<0 and (temp2-temp1)<0 then
output localmin;
temp1=temp2;
temp2=var1;
end;
run;
這裡沒有考慮邊界點以及等號的的定義問題,需要的話再modify一下就行了
當然,coding上或許還有不小的進步空間,上面的版本完全只是土法煉鋼
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.109.40.59
※ 編輯: west1996 來自: 140.109.40.59 (09/17 17:59)
推 eminem:先謝過~(仍在研讀中) 09/17 20:02
→ west1996:上面有錯if _n_>3要改成_n_>=3 09/18 12:37