Thursday, May 24, 2012

Advanced tips on using PROC MEANS in SAS

Please consider the below description of combination in proc means in the class variable.

_type_  Description            Variables  Dimension
value   of combination         combined

1       right most variable       b       1 way

2       leftmost variable
        only if middle
        Variable Not present      a       2 way
3       right most variable
        and left most Variable  a * b     3 way

Consider this program for all examples.


data one ;
infile datalines ;
input sub1 $ sub2 $ sal ;
datalines ;
A B 100
A A 200
B B 1000
B A 1500
;
run ;


Example 1:-

proc means data=one ;
     class sub1 sub2 ;
     var sal ;
     output out   = t1
            sum() = ;
run ;

This procedure will produce all possible combination class variables including grand total.

Output:-

sub1    sub2    _TYPE_    _FREQ_    sal
                0         4         2800
        A       1         2         1700
        B       1         2         1100
A               2         2         300
B               2         2         2500
A       A       3         1         200
A       B       3         1         100
B       A       3         1         1500
B       B       3         1         1000


Example 2:-

proc means data=one nway ;
       class sub1 sub2 ;
       var sal ;
       output out   = t2
              sum() = ;
run ;

This procedure will produce only combination of class variables with the highest value of _type_ because nway options specified.

Output:-

sub1    sub2    _TYPE_    _FREQ_    sal
A       A       3         1         200
A       B       3         1         100
B       A       3         1         1500
B       B       3         1         1000


Example 3:-

proc means data=one ;
     class sub1 sub2 ;
     var sal ;
     types sub1*sub2 sub1 ;
     output out   = t3
            sum() = ;
run;

This procedure produces only combination of class variables specified in types statement.

Output:-

sub1    sub2    _TYPE_    _FREQ_    sal
A               2         2         300
B               2         2         2500
A       A       3         1         200
A       B       3         1         100
B       A       3         1         1500
B       B       3         1         1000


Example 4:-

proc means data=one ;
     class sub1 sub2 ;
     var sal ;
     ways 2 ;
     output out   = t4
            sum() = ;
run ;

In this procedure we specified ways statement ‘2’ so the output is only combination of 2 class variable.

Output:-

sub1    sub2    _TYPE_    _FREQ_    sal
A       A       3         1         200
A       B       3         1         100
B       A       3         1         1500
B       B       3         1         1000


Example 5:-

proc means data=one ;
     class sub1 sub2 ;
     var sal ;
     output out   = t5 (where =(_type_ in (1)))
            sum() = ;
run ;


Output:-

sub1    sub2    _TYPE_    _FREQ_    sal
        A       1         2         1700
        B       1         2         1100

This procedure produces only 1 way combination (which is right most variable).

No comments:

Post a Comment