sas根据max和min值添加列名和行

时间:2017-09-28 13:23:15

标签: sas

我希望数据被转置(日期)并相应地进行排列并填补空白 例如:enter image description here

/*COUNT ID BASED ON bucket and Sold_month*/
    PROC SQL;
CREATE TABLE want AS
SELECT bucket, Sold_month,COUNT(ID) as IID from TRANS_2 GROUP by bucket, 
Sold_month;
quit;

/*Format date*/
proc datasets ;
modify want;
FORMAT Sold_month YYMMN6.;
quit;

/*TRANSPOSE*/
PROC TRANSPOSE data=want out=wantf  (drop=_:)  prefix=D_ ;
by bucket;
var IID;
id Sold_month;
format IID best.;
run;

/*Sorting the dates Sold month*/
proc contents data=wantf out=col_names(keep=name) noprint;
run; 

proc sort data=col_names out=col_names_sorted;
 by name;
 run; 
 proc sql;
 create table col_names_sorted_n as
 select name from col_names_sorted where name<>'bucket';
 run;


 data _null_;
 set col_names_sorted_n;
 by name;
 retain sorted_cols;
 length sorted_cols $2500.;
 if _n_ = 1 then sorted_cols =name ;
 else sorted_cols = catx(' ', sorted_cols, name);
 call symput('sorted_cols', sorted_cols);
 run; 

  %put &sorted_cols;
  /* Final SOLD DATA- */
data output_sorted;
retain bucket &sorted_cols;
set wantf ;
run;

/*ADDING 0 to missing values*/
proc stdize data=output_sorted out=Transaction reponly missing=0;
run;

这样可行,但我看不到桶的最大值和最小值(0-max)和sold_dt(从最小年月到最大年月的所有year_month)都没有得到我想要的缺失列名和行。我不想手动输入所有列/桶但是得到最大值和最小值。但是桶应该从0开始到最大可用

1 个答案:

答案 0 :(得分:1)

最简单的方法是使用PROPL SUMMARY with COMPLETETYPES选项。

proc summary data=TRANS_2 nway completetypes ;
  class bucket sold_month ;
  var id;
  format sold_month yymmn6. ;
  output out=tall n=iid;
run;

proc transpose data=tall out=want(drop=_name_) prefix=d_ ;
  by bucket ;
  id sold_month;
  var iid ;
run;

如果你完全没有BUCKET值,例如你根本没有BUCKET = 4的记录,那么你需要添加一些记录。可能更容易将它们添加到汇总表中,在这种情况下,您可以跳过COMPLETETYPES并自行完成。总结数据:

proc sql noprint;
  create table tall as
    select bucket
         , intnx('month',sold_month,0,'b') as sold_month format=yymmn6.
         , count(id) as IID
    from TRANS_2
    group by 1,2
  ;
  create table tall_dummy as 
    select max(bucket) as bucket
         , min(sold_month) as min   
         , max(sold_month) as max
    from tall
  ;
quit;

生成所有桶号的表,以及所有月数为零的计数。

data tall_dummy ;
  set tall_dummy ;
  keep bucket sold_month iid;
  iid=0;
  do bucket = 0 to bucket ;
    do i=0 to intck('month',min,max);
      sold_month=intnx('month',min,i,'b') ;
      output;
    end;
  end;
  format sold_month yymmn6.;
run;

然后与实际的桶*月组合合并,并用实际计数替换零。

data tall_full ;
  merge tall_dummy tall ;
  by bucket sold_month;
run; 

然后你可以转置。

proc transpose data=tall_full out=want(drop=_name_) prefix=d_ ;
  by bucket ;
  id sold_month;
  var iid ;
run;