数据集 firstobs 中的错误 [英] Error in data set firstobs

查看:48
本文介绍了数据集 firstobs 中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 SAS 非常非常陌生,一直在阅读 stackoverflow 问题和 SAS 文档,以编写用于特定目的的代码.我一直很难实现我的目标和理解 SAS,原因有两个:我只能通过 SSH 在远程服务器上执行代码,因为我本地没有 SAS(因此,每次更改时,我都需要上传文件,执行并取回日志和 lst 文件,检查错误),而且我读过的大多数介绍性主题都不能立即适用于我的任务(我只对使用 SAS 自动执行某个数据提取过程感兴趣).

I am very, very new to SAS and have been reading stackoverflow questions and SAS documentation, to write code for a very specific purpose. I have been having a hard time achieving my goal and understanding SAS for two reasons: I can only execute code on a remote server though SSH because I do not have SAS locally (so, on every change, I need to upload the file, execute and get the log and lst files back, check for errors) and most of the introductory topics I have read about are not immediately applicable for my task (I am only interested in using SAS to automate a certain data extraction procedure).

我的目标是:

  • 读取 CSV 文件中的某些股票代码(即股票的标识符);
  • 循环每个股票代码,通过某些宏检索我需要的信息.

到目前为止,我已成功读取 .csv 并将该数据导入数据集.为了测试我需要的基础知识是否正常工作,我编写了以下代码.我的目标是通过循环将代码分配给某个变量"当前代码(可能不是正确的名称)并打印它.csv 文件只有两行,第一行是IBM",另一行是DELL".

So far, I've succeeded in reading the .csv and importing that data to a dataset. To test whether the basics of what I need are working correctly, I made the following code. My goal was, through a loop, to assign the ticker to a certain "variable" currentticker (probably not the right name for it) and print it. The csv file only has two lines it says "IBM" in the first and "DELL" in the other.

libname mydir '~/';

data companies;
  infile 'sastests/data/tickers.csv' delimiter=',';
  input ticker $;
run;

proc sql;
 select    count(*)
 into      :OBSCOUNT
 from      companies;
quit;

proc print data=companies;
  var ticker;
run;

%do iter = 1 to &OBSCOUNT;
  data currentticker;
    set companies (firstobs = iter obs = iter);
  run;
  proc print data = currentticker;
  run;
%end;

当我查看日志文件时,我立即在数据集的 firstobs 选项中收到错误消息.

When I go through the log file, I get an error immediately in the firstobs option of data set.

Invalid value for the FIRSTOBS option.

为什么会这样?iter 不应该是一个数字,因此作为 FIRSTOBS 有效吗?

Why is this so? Shouldn't iter be a number, thus being valid as a FIRSTOBS?

非常感谢您.

编辑 1:标题不能很好地描述问题.

Edit 1: Title was not a good description of the problem.

编辑 2:用于单个代码的宏示例.查找必须由 &ticker 提供.将调用lookup,然后是getopt,最后是export_tab.此代码非本人原创,WRDS 提供示例代码后稍加修改.

Edit 2: Examples of the macros to be used for a single ticker. Lookup would have to be feeded with &ticker. lookup would be called, then getopt and finally export_tab. This code is not of my authorship, I modified it slightly after it was provided as sample code by WRDS.

%macro lookup;

  data idcodes (keep=secid);
  set optionm.secnmd;
  where lowcase(ticker) = &ticker;

  proc sort data=idcodes nodupkey;
    by secid;

  proc print data=idcodes;

%mend;

%macro getopt(year);

  proc sql;
    create table temp as
      select a.* 
      from
        optionm.vsurfd&year as a,
        idcodes as b
      where
        a.secid = b.secid;
  run;

  proc datasets;
    append base=work.&outputfile
    data=work.temp;
  run;

%mend;

%macro export_tab;

  proc export data=&outputfile outfile="&outputfile._out.txt" dbms=tab replace;
  run;

%mend;

推荐答案

这基本上是另一个答案,所以把它放在这里.这就是我处理第二部分的方式——根本没有宏.我假设他们的年度数据集已经按 secid 排序了;如果没有,这可能会更复杂一些,只是为了避免合并.

This is basically another answer, so putting it here. This is how I'd approach the second part - no macros at all . I'm assuming their yearly datasets are sorted by secid already; if not, this might be a bit more complicated, just to avoid the merge then.

proc sql;
select quote(ticker) into :tickerlist separated by ',' from companies;
quit;

data idcodes;  *you could also create this by merging optiomn.secnmd to companies by ticker.;
set optionm.secnmd;
where lowcase(ticker) in (&tickers.);
run;

proc sort data=idcodes nodupkey;
by secid;
run;

proc print data=idcodes;
run;

data lotsofyears/view=lotsofyears;
set
optionm.vsurfd2010
optionm.vsurfd2011
optionm.vsurfd2012
optionm.vsurfd2013
;  *or however many you need, you could generate this list if it is long;
by secid;
run;

data mydata;
merge lotsofyears(in=a) idcodes(in=b);
by secid;
filenm=cats("c:\mydir\mydata_",ticker,".dat"); *or secid if that is better;
run;
proc sort data=mydata;
by ticker;
run;
data _null_;
set mydata;
file a filevar=filenm dlm='09'x lrecl=32767;
put (_all_)($); *or perhaps a more complex put statement - see what proc export generates;
run;

这篇关于数据集 firstobs 中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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