简体   繁体   中英

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 ?

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;

Why not use normal datastep to duplicate row? Than do what every you wanted.

data want;
  set have;
  do _i = 1 to count;
    output;
  run;
  drop _i;
run;
proc print;run;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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