如果数据集没有行,则调用未解析的Symputx宏变量

时间:2014-03-01 01:27:16

标签: sas

我有以下代码检查文件名URL语句是否正确地传递了它的内容和infile语句。如果不是宏应该迭代十次尝试建立URL连接然后放弃:

%macro test_exst(iter);

/*filename loader url "http://www.sdhsdisdhsidhsidh.com";*/
/*filename loader url "http://www.bbc.co.uk/sport";*/
filename loader url "http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=na5";

/*  data step based on filename url above goes here, each pass will give 500 metrics x 1 symbol dataset*/


%put create dataset from csv submission;


%Let ITER = %Eval(&ITER + 1);
%let obscount = 1;

data mytest;
infile loader length=len MISSOVER /*delimiter = ','*/;
input record $varying30. len; 
format record $30.;
informat record $30.;
call symputx("obscount2",_n_);
run;

%put obscount = &obscount;
%put obscount2 = &obscount2;

%if &obscount2= .    %then %let obscount2 = &obscount;

%put obscount2 = &obscount2;


 %if &obscount2=1 and %eval(&iter. < 10) %then %do;
   %put Iteration &iter. failed, trying again;
   %test_exst(&iter.);
 %end;
%mend test_exst;

%test_exst(0);

只要正确创建数据集(即代码不会不恰当地迭代),这样就可以正常工作,但是当文件名URL连接不起作用时,宏变量&amp; obscount2。未正确解析,我收到一条错误消息,指出字符串已被错误地传递给%if或%eval语句。

我理解为什么会出现这种错误,但我似乎无法使该行的语法正确:

%if &obscount2= .    %then %let obscount2 = &obscount;

基本上我在这里试图说,如果'&amp; Obscount2'尚未解决,那么将它改为'&amp; obscount',而是预先定义为1.然后在行:

%if&amp; obscount2 = 1和%eval(&amp; iter。&lt; 10)%then%do;

我知道我的数据集只会有200或零观察,因为我的URL将在此代码的最终版本中编码的方式。如果&amp; Obscount2 = 1,那么它只能是因为它最初没有得到解决而被指向它。然后代码将循环,直到建立文件名URL连接。

关于如何完成代码的任何想法?

由于

2 个答案:

答案 0 :(得分:0)

您可以创建数据集mytest,然后根据此链接使用attrn函数检查它有多少观察值:

http://support.sas.com/kb/25/078.html

/* Sample data */

data one;
  input x;
datalines;
1
2
;


%let dsid=%sysfunc(open(one));
%let num=%sysfunc(attrn(&dsid,nlobs));
%let rc=%sysfunc(close(&dsid));

%put There are &num observations in dataset one.;

答案 1 :(得分:0)

这最终有效:

%macro test_exst(iter);

/*filename loader url "http://www.sdhsdisdhsidhsidh.com";*/
/*filename loader url "http://www.bbc.co.uk/sport";*/
filename loader url "http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=na5";

/*  data step based on filename url above goes here, each pass will give 500 metrics x 1 symbol dataset*/


%put create dataset from csv submission;


%Let ITER = %Eval(&ITER + 1);


data mytest;
infile loader length=len MISSOVER /*delimiter = ','*/;
input record $varying30. len; 
format record $30.;
informat record $30.;
run;

%let dsid=%sysfunc(open(mytest));
%let num=%sysfunc(attrn(&dsid,nlobs));
%let rc=%sysfunc(close(&dsid));

%let obscount = &num;

%put obscount = &obscount;

 %if &obscount=0 and &iter. < 10 %then %do;
   %put Iteration &iter. failed, trying again;
   %test_exst(&iter.);
 %end;
%mend test_exst;

%test_exst(0);