2014-03-01 50 views
0

我有以下代碼,它檢查文件名URL語句是否正確地將它的內容傳遞給infile語句。如果沒有宏觀應該通過十次試圖使URL連接進行迭代,然後放棄:如果數據集沒有行,則調用Symputx宏變量未解決

%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連接不起作用宏變量& obscount2。沒有正確解析,並且出現錯誤消息,指出字符串已錯誤地傳遞給%if或%eval語句。

我明白爲什麼這個錯誤發生,但我似乎無法得到正確的語法爲線:

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

基本上在這裏我想說的是,如果「& Obscount2」一直沒有解決然後使其等於 '& obscount' 代替,然後在該行已被預先定義爲1:

%如果& obscount2 = 1和%EVAL(& ITER < 10)%,那麼%做;

我知道我的數據集僅會因爲我的網址將在此代碼的最終版本進行編碼方式的性質200層或零的意見。如果& Obscount2 = 1在這裏它只能是因爲它已經被指出,它沒有被最初解決。然後代碼將循環,直到建立文件名URL連接。

關於如何完成代碼的任何想法?

感謝

回答

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.; 
+0

感謝。已將其納入上述解決方案。 – user3043997

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);