SAS 数据步骤视图和数据包装在宏 for 循环中 [英] SAS data step view and data wrap in a macro for loop

查看:18
本文介绍了SAS 数据步骤视图和数据包装在宏 for 循环中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一个大学研究项目,我通过 SAS 从 WRDS 检索数据,并且对 SAS 比较陌生.我正在尝试以 WRDS 提供的特定间隔检索数据,这实际上对我非常有用;结构如下

For a university research project I am retrieving data from WRDS via SAS and am relatively new to SAS. I am trying to retrieve data in a specific interval provided by WRDS which actually works very for me; the structure is as follows

[1]Define some macro variable
[2]Use data step view
[3]Make manipulation on data
[4]Export the data to csv

特别是我正在检索每一年的股票数据.不是一直更改变量,而是允许我提供年份作为输入的宏将是最优雅"的解决方案(灵感来自这里:[SAS Loop through list of macro variable][1]).但是,我的宏没有按预期工作(我还稍微更改了结构,添加了一个附加步骤而不是导出到 CSV).

In particular I am retrieving data for a stock for every single year. Instead of changing the variable all the time a macro that would allow me to provide the years as input would be the "most elegant" solution (inspired from here: [SAS Loop through list of macro variable][1]). However, my macro doesn't work as expected (I also changed the structure a bit adding an append step instead of exporting to CSV).

步骤 [3] 现在报告错误:ERROR 180-322:语句无效或使用顺序不正确.

Step [3] is now reporting error: ERROR 180-322: Statement is not valid or it is used out of proper order.

我将代码放在这里(第 3 部分我保留原样,因为它会产生问题,其他部分我已经缩短了一点(我评论过):

I am putting the code here (part 3 I leave as it is because it's creating problems, the other I have shortened a bit (I commented):

%macro get_stock_ts(list_years);

%local i tables;
%do i=1 %to %sysfunc(countw(&list_years,%str( )));
%let tables=%scan(&list_years,&i,%str( ));

proc datasets lib = work memtype = all nolist;
    delete _:;
quit; 

%local stock = "COP";       

%local taq_ds=taq.&tables:; 
%local filename = &tables._&stock; 

data  _v_&tables / view=_v_&tables;
  set &taq_ds;  
  where symbol = &stock and                             
        (time between '9:30:00't and '16:00:00't) and       
        mode = 12 and                                   
        EX = 'N';                                       
run; 

data xtemp2; 
 set _v_&tables; 
 by symbol date time; 
 format itime rtime time12.; 
 if first.symbol = 1 or first.date = 1 then do;         
    rtime = time; 
    iprice = bid; 
    oprice = ofr; 
    itime = &start_time; 
 end; 

 if time >= itime then do;                                              
       output;                                                          
       itime = itime + &interval_seconds; 
       do while(time >= itime);                                         
           output; 
           itime = itime + &interval_seconds; 
       end; 
end; 
rtime = time; 
iprice = bid; 
oprice = ofr; 
retain itime iprice oprice;                                             
run; 

proc append base = all data = work.xtemp2 force;
run;

proc printto log="/home/Logs/ &filename.log" new; run;
proc printto log=log; run;                                          

%end;
%mend get_stock_ts;

然后我调用例如:

%get_stock_ts(cq_2009)

您是否知道将代码作为独立运行正常工作的原因是什么,但是一旦我将其包装为宏,好"就会开始产生问题?

Would you know how it comes that running the code as standalone works fine, but as soon as I wrap it as a macro the "good" starts to create problems?

我已经调整了上面的代码并获得了以下内容.是不是因为宏和局部宏变量语句有问题?

edit: I have adjusted the code above and getting the following. Is it because macro and local macro variables have a problem with the statement?

 1          OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 72         
 73         %get_stock_ts(cq_2009)
 ERROR: Invalid symbolic variable name =.
 ERROR: Invalid symbolic variable name "COP".
 ERROR: Invalid symbolic variable name =.
 ERROR: Symbolic variable name TAQ.CQ_2009 must contain only letters,     digits, and underscores.
 ERROR: Invalid symbolic variable name TAQ.CQ_2009.
 ERROR: Invalid symbolic variable name :.
 ERROR: Invalid symbolic variable name =.
 ERROR: Invalid symbolic variable name =.
 ERROR: Invalid symbolic variable name '9:30:00'T.
 ERROR: Invalid symbolic variable name =.
 ERROR: Invalid symbolic variable name 1.
 ERROR: Invalid symbolic variable name *.
 ERROR: Invalid symbolic variable name 60.
 ERROR: Variable symbol is not on file WORK.ALL.

 NOTE: The SAS System stopped processing this step because of errors.
 NOTE: DATA statement used (Total process time):
   real time           0.00 seconds
   cpu time            0.00 seconds

 ERROR: Variable SYMBOL not found.
 ERROR: Variable DATE not found.
 ERROR: Variable TIME not found.
 NOTE: The SAS System stopped processing this step because of errors.
 NOTE: PROCEDURE SORT used (Total process time):
   real time           0.00 seconds
   cpu time            0.00 seconds

 ERROR: File WORK.XTEMP2.DATA does not exist.

推荐答案

这行是错的.

%local stock = "COP";   

您正在尝试定义名为 ="COP" 的局部宏变量.你可能打算这样做.

You are trying the define local macro variables named = and "COP". You probably meant to do this.

%local stock ;
%let stock = "COP";   

这篇关于SAS 数据步骤视图和数据包装在宏 for 循环中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆