Creating All Possible Combinations in a Table Using SAS

前端 未结 3 1447
一生所求
一生所求 2020-12-11 09:16

I have a table with four variables and i want the table a table with combination of all values. Showing a table with only 2 columns as an example.

NAME    AM         


        
相关标签:
3条回答
  • 2020-12-11 09:44

    There are a couple of useful options in SAS to do this; both create a table with all possible combinations of variables, and then you can just drop the summary data that you don't need. Given your initial dataset:

    data have;
    input NAME $ AMOUNT  COUNT;
    datalines;
    RAJ 90  1
    RAVI    20  4
    JOHN    30  5
    JOSEPH  40  3
    ;;;;
    run;
    

    There is PROC FREQ with SPARSE.

    proc freq data=have noprint;
    tables name*amount*count/sparse out=want(drop=percent);
    run;
    

    There is also PROC TABULATE.

    proc tabulate data=have out=want(keep=name amount count);
    class name amount count;
    tables name*amount,count /printmiss;
    run;
    

    This has the advantage of not conflicting with the name for the COUNT variable.

    0 讨论(0)
  • 2020-12-11 09:52

    Here's a variation on @JustinJDavies's answer, using an explicit CROSS JOIN clause:

    data have;
      input NAME $ AMOUNT  COUNT;
      datalines;
    RAJ     90  1
    RAVI    20  4
    JOHN    30  5
    JOSEPH  40  3
    run;
    
    PROC SQL;
      create table combs as
        select * 
          from have(keep=NAME)
            cross join have(keep=AMOUNT)
            cross join have(keep=COUNT)
          order by name, amount, count;
    QUIT;
    

    Results:

    NAME AMOUNT COUNT 
    JOHN 20     1 
    JOHN 20     3 
    JOHN 20     4 
    JOHN 20     5 
    JOHN 30     1 
    JOHN 30     3 
    JOHN 30     4 
    JOHN 30     5 
    ...
    
    0 讨论(0)
  • 2020-12-11 10:01

    Try

    PROC SQL;
    CREATE TABLE tbl_out AS
    SELECT a.name AS name 
           ,b.amount AS amount
           ,c.count AS count
    FROM tbl_in AS a, tbl_in AS b, tbl_in AS c
    ;
    QUIT;
    

    This performs a double self-join and should have the desired effect.

    0 讨论(0)
提交回复
热议问题