我想提取数据集的所有第n行,然后每个后续的第33行,并将它们存储在一个新的数据集中。
我有一个数据集,其中包含n乘以33个估价值,每个估价值都在彼此下方,我想将所有估价值a0
提取到一个名为A0
的数据集中,然后将所有a1
估计提取到一个名为A1
的数据集中,等等,直到我有33个数据集。
我可以为每个元素这样做,但这需要大量的代码,我想简化它。这段代码命名了一个数据集,然后将所有元素提取到其中。
data a0;
set _parest1;
if mod(_n_,33) = 1;
run;
这是一个特定的问题,是一个更大问题的一部分。我有许多数据集,其中包含34个估计参数(a0,a1…a33),我想取所有每个估计的平均值。
这里有一种替代方法:哈希的哈希方法。这主要来自Paul Dorfman关于这个主题的论文,Data Step Hash Objects as Programming Tools。
data estimates;
do id = 1 to 50;
output;
end;
run;
data _null_;
if 0 then set estimates;
length estimate 8;
if _n_=1 then do;
declare hash e ();
declare hash h(ordered:'a');
h.defineKey('estimate');
h.defineData('estimate','e'); *this 'e' is the name of the other hash!;
h.defineDone();
declare hiter hi('h');
end;
do _n_ = 1 by 1 until (eof);
set estimates end=eof;
estimate = mod(_n_-1,5);
rc_h = h.find();
if rc_h ne 0 then do;
e = _new_ hash(ordered:'a');
e.defineKey('estimate','id');
e.defineData('estimate','id');
e.defineDone();
h.replace();
end;
e.replace();
end;
do rc = hi.next () by 0 while ( rc = 0 ) ;
e.output (dataset: cats('out',estimate)) ;
rc = hi.next() ;
end ;
run;
这允许您任意输出任何特定数量的数据集,这很好。在这里,您将5替换为33并调整变量名称("估计"是估计数字,我用MOD计算它,但也许您已经在数据集中拥有它,而"id"当然是无论您的id是该行-行号很好,_N_
甚至-如果您有其他数据变量(您可能会这样做),您可以将它们添加到e
的defineData。
使用firstobs=
数据集选项从第n条记录开始;
data want;
set have(firstobs=10);
if _n_ = 1 then output;
else if mod(_n_,33) = 1 then output;
run;
所以要循环这个,使用一个宏。例如:
data test;
do i=1 to 100;
output;
end;
run;
%macro loop_split(n,mod, ds, outPre);
%local i j;
%do i=1 %to &n;
%let j=%eval(&i-1);
data &outPre&j;
set &ds(firstobs=&i);
if _n_ = 1 then output;
else if mod(_n_,33) = 1 then output;
run;
%end;
%mend;
%loop_split(33,33,test,want);
我将n
和mod
的值分开,因为它们不必相同,但在您的情况下。