循环列表

时间:2015-04-01 12:26:36

标签: loops sas

我有以下宏:

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生成一个新的数据集。 我怎样才能做到这一点?感谢

2 个答案:

答案 0 :(得分: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的内容必须适合内存。如果您不关心订单,可以在一个数据步骤中完成。

答案 1 :(得分:0)

SRSwift感谢您的全面解答。它运行顺利,没有错误。唯一的问题是我使用以下命令在远程服务器(wharton)上运行它:

 %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