简体   繁体   中英

how to create customized frequency table in SAS

Assume I have a data called orig with variable x and s, s being binary as follows:

x  s
12 1
3  0
3  0
34 1  
12 0

I want to create a new table or data which will be similar to the frequency table of orig. The first column will be the variables appeared in x, the second column will be the number of s variable "1" for those x, and third column will be the number of "0" and last one will be the total number of x. If we use the above table as an example the new table I am expecting is :

x    "0 of s"    "1 of s"   total
3      1          1         2
12     1          1         2
34     0          1         1

Pretty straightforward in proc sql . Use a group by statement and sum summary functions that evaluate to 1 if an expression is true, and 0 if it is not.

proc sql;
create table newtable as
select
  x,
  sum(s=0) '0 of s',
  sum(s=1) '1 of s',
  sum(1) as total
from orig
group by x;
quit;

I think this is what you're looking for. For your x=3 output, I think you want "0 of s"=2, "1 of s"=0, though, in order to be consistent with the original dataset that you gave.

data orig;
        input x s;
        datalines;
12 1
3  0
3  0
34 1
12 0
;

proc print data=orig;
        title 'Original Dataset';
run;

proc sort data=orig;
        by x;
run;

data orig_collapsed;
        set orig;
        retain s0 s1;
        by x;

        if first.x then do;
                s0=0;
                s1=0;
        end;


        if s=0 then s0=s0+1;
        if s=1 then s1=s1+1;

        if last.x then do;
                total = s0 + s1;
                output;
        end;

        keep x s0 s1 total;
run;

proc print data=orig_collapsed noobs;
        title 'Collapsed Dataset';
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