看看下面这个好用不。
注:我用的是5-100。程序第一部分是下载股票数据,第二部分(蓝色)是你要的程序。
%macro quotes (symbol=, start=, end=, prompt=NO, out=);
%* Richard A. DeVenezia, 4/9/00
%* Quotes: A macro to retrieve historical daily stock quotes from Yahoo;
%*
%* symbol - ticker symbol
%* start - starting date mm/dd/yy, default: 30 days prior to end
%* end - ending date mm/dd/yy, default: today
%* prompt - if YES then raise a macro window, otherwise proceed
%* out - table name, default: same as symbol
%*
%* Yahoo url that delivers a CSV of daily stock quotes looks like
%*
http://table.finance.yahoo.com
%* /table.csv?s=<SYMBOL>
%* &a=<FROM_MONTH>
%* &b=<FROM_DAY>
%* &c=<FROM_YEAR>
%* &d=<TO_MONTH>
%* &e=<TO_DAY>
%* &f=<TO_YEAR>
%* &g=d
%*
%* MONTH 0 is January
%*
%* 12/08/03 rad Thanks to Andrew Farrer for v.8 bugfix (%window)
%* 4/20/07 rad Use IS8601DA to read date value
%* 1/12/10 rad Thanks to Sunny in Atlanta for bug report {dash in symbol}
%* Add out= option, compress symbol for default
%*;
%local symbol start end;
%local s _s _e a b c d e f g url;
%if (%superq(symbol) eq ) %then
%let symbol=msft;
%let symbol = %upcase (&symbol);
%if (%superq(end) eq ) %then
%let end = %sysfunc(today(),mmddyy8);
%if (%superq(start) eq ) %then %do;
%let start = %sysfunc(inputn(&end,mmddyy8.));
%let start = %eval (&start-30);
%let start = %sysfunc(putn(&start,mmddyy8.));
%end;
%if (%upcase(&prompt) eq YES) %then %do;
%window Quotes rows=12 columns=30
#2@2 "Symbol: " symbol 8 c=blue a=rev_video
#4@2 "Start: " start 8 c=blue a=rev_video
#6@2 "End: " end 8 c=blue a=rev_video
;
%display Quotes;
%let symbol = %upcase (&symbol);
%end;
%if (%superq(out) eq ) %then
%let out = %sysfunc(compress(&symbol,%str(-./)));
%let _e = %sysfunc(inputn(&end ,mmddyy10.));
%let _s = %sysfunc(inputn(&start,mmddyy10.));
%let s = &symbol;
%let a = %sysfunc (month(&_s)); %let a=%eval(&a-1);
%let b = %sysfunc (day (&_s));
%let c = %sysfunc (year (&_s));
%let d = %sysfunc (month(&_e)); %let d=%eval(&d-1);
%let e = %sysfunc (day (&_e));
%let f = %sysfunc (year (&_e));
%let g = d;
%let and = %str(&);
/*
%let url =
http://ichart.finance.yahoo.com:80/table.csv;
*/
%let url =
http://table.finance.yahoo.com:80/table.csv;
%let url = &url.?s=&s;
%let url = &url.&and.a=&a.&and.b=&b.&and.c=&c;
%let url = &url.&and.d=&d.&and.e=&e.&and.f=&f;
%let url = &url.&and.g=&g;
%put &url;
filename quotes URL "&url";
data &out;
infile quotes dlm=',';
retain symbol "&symbol";
if _n_ = 1 then input; * skip header row;
* input Date date9. Open High Low Close Volume;
input Date is8601da. Open High Low Close Volume;
format Date mmddyy10.;
format Volume comma11.;
run;
%if &SYSERR ne 0 and &SYSERR ne 4 %then %do;
%put NOTE: --------------------;
%put NOTE: SYSERR=&SYSERR.;
%put NOTE: Check that you have used a valid stock symbol [&symbol];
%put NOTE: and a valid date range [&start] to [&end];
%put NOTE: --------------------;
%end;
%let syslast = &out;
%bye:
%mend;
* Obtain quote data from yahoo;
%quotes (symbol=msft, start=1/1/00);
%quotes (symbol=f, start=1/1/00);
%quotes (symbol=luv, start=1/1/00);
%quotes (symbol=goog, start=1/1/00);
* data preparation;
data have; set msft f luv goog;
proc sort; by symbol date;run;
data have; set have;
year=year(date);
month=month(date); run;
* create an indicator for every month;
proc sort data=have out=have1; by symbol year month;
data have2 (drop=close); set have1 (keep=symbol year month close);
by symbol year month;
if last.month;
if 5<=lag(close)<=100 then ok=1; else ok=0;
run;
data have2; set have2;
by symbol;
if first.symbol then ok=.;run;
*keep only stock data meeting the filtering rule;
proc sql;
create table want as
select a.*
from have as a, have2 as b
where a.symbol=b.symbol and a.year=b.year and a.month=b.month and b.ok=1;
quit;