【问题标题】:SAS Macro: How can I record proc means output in one dataset?SAS 宏:如何在一个数据集中记录 proc 表示输出?
【发布时间】:2020-04-12 12:24:16
【问题描述】:

[我有这段代码。但是,由于循环 t 从 1 到 310,proc 单变量中的宏生成了太多单独的数据集。如何修改此代码以将所有 proc 单变量输出包含到一个数据集中,然后修改其余代码以更有效地运行? ]


%let L=10; %* 10th percentile *;
%let H=%eval(100 - &L); %* 90th percentile*;
%let wlo=V1&L V2&L V3&L ;
%let whi=V1&H V2&H V3&H ;
%let wval=wV1 wV2 wV3 ;
%let val=V1 V2 V3;

%macro winsorise();

%do v=1 %to %sysfunc(countw(&val));
%do t=1 %to 310;
proc univariate data=regressors noprint;
var &val;
output out=_winsor&t._V&v pctlpts=&H &L
prtlpre=&val&t._V&v;
where time_count<=&t;run;
%end;
data regressors (drop=__:);
set regressors;
if _n_=1 then set _winsor&t._V&v;
&wval&t._V&v=min(max(&val&t._V&v,&wlo&t._V&v),&whi&t._V&v);
run;
%end;
%mend;

谢谢。

【问题讨论】:

  • time_count 是整数吗?当您的 regressors 数据从条件 time_count &lt; 1 变为条件 time_count &lt; 310 时,您要处理多少行?如果每个整数都为 1:310,则 310 个不同 time_count 范围的数据可以三角形“堆叠”成 310 个不同的组,行数为 1,2,3,...310。 310 行将爆炸为 48,205 行 ( = 310 * 311 / 2 ),但可以使用 BY 语句进行处理。

标签: sas


【解决方案1】:

假设您有数据time_countx1x2x3,每 0.5 个时间单位的样本。

data regressors;
  call streaminit(123);
  do time_count = 0 to 310 by .5;
    x1 = 2 ** (sin(time_count/6) * log(time_count+1));
    x2 = log2 (time_count+1) + log(time_count/10+.1);
    x3 = rand('normal', 
    output;
  end;
  format x: 7.3;
run;

根据整数 time_count 级别将数据堆叠成组。堆栈由具有小于 (&lt;=) 标准的完全外连接构成。每个组由组中的 top time_count 标识。

proc sql;
  create table stack as
  select 
    a.time_count
  , a.x1
  , a.x2
  , a.x3
  , b.time_count as time_count_group            /* save top value in group variable */
  from      regressors as a
  full join regressors as b                     /* self full join */
  on a.time_count <= b.time_count               /* triangular criteria */
  where
  int(b.time_count)=b.time_count                /* select integer top values */
  order by
  b.time_count,  a.time_count
  ;
quit;

现在一次性计算所有组的所有变量的所有统计数据。没有宏观,没有混乱,没有大惊小怪。

proc univariate data=stack noprint;
 by time_count_group;
 var x1 x2 x3;
 output out=_winsor n=group_size pctlpts=90 10 pctlpre=x1_ x2_ x3_; 
run;

【讨论】:

    猜你喜欢
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多