请选择 进入手机版 | 继续访问电脑版
楼主: heyiheyiheyi
5228 6

[原创博文] 请教SAS 选取符合条件的股票日数据 [推广有奖]

  • 0关注
  • 0粉丝

大专生

93%

还不是VIP/贵宾

-

威望
0
论坛币
173 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
879 点
帖子
29
精华
0
在线时间
94 小时
注册时间
2010-2-11
最后登录
2019-8-26

heyiheyiheyi 发表于 2010-7-23 17:44:17 |显示全部楼层 |坛友微信交流群

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
麻烦大家解答一个问题:某个月的股票数据,只有在上个月末最后一个交易日的股价在$5-$1000之间,才可选取作为样本。怎么用SAS编程解决呢?
我本来想用一个变量(取名为OK)的取值0或者1来作为选取标准,代码如下:
Data CRSPstocks;
Set sasuser.sasexperiment;
   d=Day(Date);m=month(Date);y=Year(Date); ok=1;PRC=ABS(PRC);
Run;

Proc sort data=CRSPstocks;
  By COMNAM y m;
Run;


Data Select1;
Set CRSPstocks;
By COMNAM y m;
   if LAST.m=1 then
     if PRC<5 or PRC>1000 then ok=0;
  Else OK=lag(ok);
Run;


本来期望,如果月末OK=0的话,用lag(ok)把下个月的OK也取值为0了,但是看了结果不对,下个月的OK取值仍然为1。请问怎么解决这个问题?谢谢!!
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:日数据 experiment sasuser Stocks Select 交易日 样本

回帖推荐

BraveMadMan 发表于4楼  查看完整内容

看看下面这个好用不。 注:我用的是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 %* en ...

本帖被以下文库推荐

zhentao 发表于 2010-7-24 20:38:40 |显示全部楼层 |坛友微信交流群
你的意思是如果每月最后的一个观测满足你的5-1000之间的条件后,把该月末的观测取出来,还是把整个月的每一天的数据都取出来?

使用道具

zhentao 发表于 2010-7-24 20:41:11 |显示全部楼层 |坛友微信交流群
另外,你的else是跟第一个if还是跟第二个if?你的ok变量是在判断步才取得值,那么判断条件不满足的时候你去lag值,这个lag到哪里去找上一个ok值啊。

使用道具

BraveMadMan 发表于 2010-7-25 02:11:14 |显示全部楼层 |坛友微信交流群
看看下面这个好用不。


注:我用的是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;
Don't get lost in technical details. What is the big picture?

使用道具

heyiheyiheyi 发表于 2010-7-26 16:51:23 |显示全部楼层 |坛友微信交流群
我回家用SAS运行看看,太感谢了!

使用道具

hait69396 发表于 2014-6-24 19:34:52 |显示全部楼层 |坛友微信交流群
瞻仰,真是厉害!楼上高手!

使用道具

zzwin1006 发表于 2015-4-10 10:24:46 |显示全部楼层 |坛友微信交流群
厉害厉害,这个代码够读好几天了

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-3-29 14:58