1
如果有一個數據的步驟是這樣實現:報告多少次條件在SAS
data tmp;
do i=1 to 10;
if 3<i<7 then do;
some stuff;
end;
end;
run;
我要寫入日誌多少次,如果聲明是真實的。例如,在這個例子,我想有說在日誌中的一行:
如果陳述屬實3次
因爲條件爲真時i
是4,5或6 。 我怎樣才能做到這一點?
如果有一個數據的步驟是這樣實現:報告多少次條件在SAS
data tmp;
do i=1 to 10;
if 3<i<7 then do;
some stuff;
end;
end;
run;
我要寫入日誌多少次,如果聲明是真實的。例如,在這個例子,我想有說在日誌中的一行:
如果陳述屬實3次
因爲條件爲真時i
是4,5或6 。 我怎樣才能做到這一點?
使用retain
來保留一個計數器變量,可以非常容易地增加滿足if
條件的次數。
data tmp;
retain Counter 0;
do i=1 to 10;
if 3<i<7 then do;
Counter+1;
*some stuff;
end;
end;
put 'If statement true ' Counter 'time(s).';
run;
注意,因爲它是數據跳躍解除前發生(只有一個在示例中的數據步循環)的最後一件事這個寫入日誌一次。如果您想要爲具有多個循環的數據步驟執行此操作(例如,當存在從其他數據集讀取數據的set
語句時,您希望告訴SAS您只希望它在步驟結束時報告。你會這樣做:
* create an example input data set;
data exampleData;
do i=1 to 10;
output;
end;
run;
* use a variable 'eof' to indicate the end of the input dataset;
data new;
set exampleData end=eof;
retain Counter 0;
if 3<i<7 then do;
Counter+1;
*some stuff;
end;
if eof then put 'If statement true ' Counter 'time(s).';
run;