Search code examples
sasreshapetabulate

weighted crosstable in SAS


Let's assume I have the following table:

id | var1 | var2 | count
1  | a    | b    | 3
2  | b    | a    | 4 
3  | c    | e    | 1

Now I would like to calculate a crosstable:

PROC TABULATE;
   CLASS var1 var2;
   TABLE var1, var2;
RUN;

However, my problem is that a count-variable of lets say 4 signifies that the particular case should actually appear 4 times in the table. So the table above should be:

id | var1 | var2 | count
1  | a    | b    | 3
1  | a    | b    | 3
1  | a    | b    | 3
2  | b    | a    | 4 
2  | b    | a    | 4 
2  | b    | a    | 4 
2  | b    | a    | 4 
3  | c    | e    | 1

Is there a possibility to weight the number of cases directly within PROC TABULAR or how can I add additional cases according to the value of count?


Solution

  • Use FREQ statement

    data test;
       infile cards dsd dlm='|' firstobs=2;
       input id (var1-var2)(:$1.) count;
       list;
       cards;
    id | var1 | var2 | count
    1  | a    | b    | 3
    2  | b    | a    | 4 
    3  | c    | e    | 1
    ;;;;
    proc print;
       run;
    PROC TABULATE;
       FREQ count;
       CLASS var1 var2;
       TABLE var1, var2;
       RUN;