2015-04-01 232 views
0

我有以下宏:遍历列表

rsubmit; 


data indexsecid; 
input secid 1-6; 
datalines; 
108105 
109764 
102456 
102480 
101499 
102434 
107880 
run; 
%let endyear = 2014; 

%macro getvols1; 
    * First I extract the secids for all the options given a date and 
    an expiry date; 
    %do yearnum = 1996 %to &endyear; 
    proc sql; 
     create table volsurface1&yearnum as 
     select a.secid, a.date, a.days, a.delta, a.impl_volatility, 
     a.impl_strike, a.cp_flag 
     from optionm.vsurfd&yearnum as a, indexsecid as b 
     where a.secid=b 
     and a.impl_strike NE -99.99 
     order by a.date, a.secid, a.impl_strike; 
    quit; 
%if &yearnum > 1996 %then %do; 
proc append base= volsurface11996 data=volsurface1&yearnum; 
run; 
%end; 
%end; 



%mend; 
%getvols1; 


proc download data=volsurface11996; 
run;  
endrsubmit; 

data _null_; 
set work.volsurface11996; 
length fv $ 200; 
fv = "C:\Users\user\Desktop\" || TRIM(put(indexsecid,4.)) || ".csv"; 
file write filevar=fv dsd dlm=',' lrecl=32000 ; 
put (_all_) (:); 
run; 

在上面的代码我有:其中a.secid = 108105。现在我列出了几个secid的列表,我需要为每个secid运行一次宏。我正在运行一次,并为每个secid生成一个新的数据集。 我该怎么做?由于

+0

为什么你想要每secid生成一个数据集?通常将所有内容都保存在一个大数据集中并使用按组处理更简单。 – user667489 2015-04-01 12:28:44

+0

好吧,我想将它导出为CSV,然后在matlab中读取它,如果我对每个secid都有一个CSV文件,它对我来说更容易。 – phdstudent 2015-04-01 12:32:07

+0

列表中有多少个secid值?你想要在另一个数据集中存在的所有值吗? – user667489 2015-04-01 12:40:41

回答

1

下面是一个使用


%let startyear = 1996; 
%let endyear = 2014; 

data volsurface1; 
    /* Read in all the input tables */ 
    set optionm.vsurfd&startyear.-optionm.vsurfd&endyear.; 
    where impl_strike ~= -99.99; 
    /* Set up a hash table containing all the wanted secids */ 
    if _N_ = 1 then do; 
    declare hash h(dataset: "indexsecid"); 
    _rc = h.defineKey("secid"); 
    _rc = h.defineDone(); 
    end; 
    /* Only keep observations where secid is matched in the hash table */ 
    if not h.find(); 
    /* Select which variables to output */ 
    keep secid date days delta impl_volatility impl_strike cp_flag; 
run; 

/* Sort the data */ 
proc sort data = volsurface1; 
    by secid date secid impl_strike; 
run; 

/* Write out a CSV for each secid */ 
data _null_; 
    set volsurface1; 
    length fv $200; 
    fv = "\path\to\output\" || trim(put(secid, 6.)) || ".csv"; 
    file write filevar = fv dsd dlm = ',' lrecl = 32000; 
    put (_all_) (:); 
run; 

由于我没有你的数据,这是未经测试。我能看到的唯一约束是indexsecid的内容必须适合内存。如果您不关心订单,则可以在一个数据步骤中完成。

0

SRSwift谢谢您的全面解答。它运行平稳无误。唯一的问题是,我使用的是运行其上的远程服务器(沃顿):

%let wrds=wrds.wharton.upenn.edu 4016; 
options comamid=TCP remote=wrds; 
signon username=_prompt_; 
rsubmit; 

,并在登录它说,它写的文件,我的文件夹在服务器上,但我看不到上的任何文件服务器。日志说:

NOTE: The file WRITE is: 
     Filename=/home/uni/user/108505.csv, 
     Owner Name=user,Group Name=uni, 
     Access Permission=rw-r--r--, 
     Last Modified=Wed Apr 1 20:11:20 2015