简体   繁体   English

SAS使用转置的多个标志

[英]SAS multiple flags using transpose

I seek your help in creating the following table in SAS. 在SAS中创建下表时,请寻求您的帮助。

I have this table: 我有这张桌子:

Policy no   Policy type ID_Payor1   ID_Payor2   ID_Insured1 ID_Insured2 ID_Owner1   ID_Owner2
123 P1  A   -   B   -   A   -
124 P2  B   -   -   -   -   -
124 P1  A   -   C   -   C   -

What I am looking to create is something like this that would consolidate numbers of policytypes that each ID has: 我想要创建的是类似这样的东西,它将合并每个ID具有的策略类型的数量:

ID  Countflag_P1_Payor  Countflag_P1_Owner  Countflag_P1_Insured    Countflag_P2_Payor  Countflag_P2_Owner  Countflag_P2_Insured    
A   2   1   0   0   0   0   
B   0   0   1   1   0   0   
C   0   1   1   0   0   0   

Really would appreacite your help.. 真的会感谢您的帮助。

Thank you, 谢谢,

I think you want a proc freq or summary here to begin with to accumulate the counts. 我认为您首先要在这里获取过程频率或摘要以累积计数。 Creating a format for the ABC and using the preloadfmt option with proc summary seems to be the way to go. 创建ABC格式并将preloadfmt选项与proc摘要一起使用似乎是一种方法。

The reason you want to create the format and do the proc summary that way is because you need to generate a dataset that has counts for A, B, and C for every policy_type -- even if the combinations don't exist. 您想要创建格式并以这种方式进行proc摘要的原因是,您需要生成一个数据集,该数据集针对每个policy_type都具有A,B和C的计数-即使组合不存在。 Like there aren't any C for ID_payor1 in your sample data, but you still want to generate rows that show a 0 count for C in that column. 就像示例数据中没有ID_payor1的C,但您仍要生成在该列中显示C计数为0的行。

    data table1;
 input Policy_no  
       Policy_type $ ID_Payor1 $ ID_Payor2 $ 
       ID_Insured1  $ ID_Insured2 $  ID_Owner1 $  ID_Owner2  $;
 datalines;
123 P1 A . B . A .
124 P2 B . . . . .
124 P1 A . C . C .
;


proc format;
   value $abc
            'A'='A'
            'B'='B'
            'C'='C'
            '.'='-'
    ;
run;

proc summary data=table1 completetypes nway;
   class  ID_Payor1 ID_Payor2 
          ID_Insured1 ID_Insured2 ID_Owner1 
          ID_Owner2 /preloadfmt missing;
   class policy_type;
   types policy_type * (ID_Payor1 ID_Payor2 ID_Insured1
                        ID_Insured2 ID_Owner1 ID_Owner2);
   output out=sumt1;
   format ID_Payor1 ID_Payor2 ID_Insured1 ID_Insured2 ID_Owner1 ID_Owner2 $abc.;
run;


proc print data=sumt1;

At this point your sumt1 dataset has for every column a freq count for AB and C (and missing as -) by each variable and P1 and P2. 此时,您的sumt1数据集为每个列以及每个变量以及P1和P2的AB和C的频率计数(并且缺少-)。 It's not nearly what you want yet but now is ready to be transposed. 它几乎不是您想要的,但是现在可以转换了。 This dataset is too big to print here -- it's long rather than wide and has a lot of missing values in columns. 该数据集太大而无法在此处打印-它很长而不是很宽,并且列中缺少很多值。 But check out the results of the proc print at that point to see what you're getting. 但是,请查看proc打印的结果,看看您得到了什么。

For transposing multiple columns, we need to run proc transpose once on each column and then merge the results. 要转置多列,我们需要在每列上运行一次proc transpose,然后合并结果。 A macro seems the way to go here. 宏似乎是要走的路。

Before transposing I'm also subsetting the large dataset so we only have rows that contain data for the column we're transposing. 在转置之前,我还要对大型数据集进行子集设置,因此我们只有包含要转置的列的数据的行。

%global freqtables;

%MACRO transall(mCol);

   data tmp_col;   
    set sumt1; 
     if &mCol. in ('A','B','C');

   proc transpose data=tmp_col 
                  out=outTrans_&mCol.(rename= &mCol.=IDabc) prefix=&mCol._;
      by &mCol.;
      id policy_type;
      var _FREQ_;
   run;

   %let freqtables = &freqtables outTrans_&mCol.(drop= _NAME_); 
     %* using freqtables macro variable to autogenerate a list;
     %* of tables for merging later;
%MEND transall;

%transall(ID_Payor1);
     %transall(ID_Payor2);
     %transall(ID_Insured1);
     %transall(ID_Insured2);
     %transall(ID_Owner1);
     %transall(ID_Owner2);   

*bonus points for creating another macro to loop through your variables; *奖励点,用于创建另一个宏以遍历您的变量; *instead of the above -- especially if you have many more columns; *代替以上内容-特别是如果您有更多列;

data combined_counts;
        merge &freqtables;
        by IDabc;
       run;
proc print data=combined_counts; 

At this point you should have a table like the one you are looking for. 此时,您应该有一张想要的桌子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM