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).

Monday, May 21, 2012

Introduction to SAS

History:
1966, anthony j. barr (one of the founder member)

1976, sas.inc http://www.sas.com/


SAS is a data warehousing tool, falls in the catagory of :
ETL -> extraction transforming loading
Reporting & Analytics tool

SAS is the combination of:
1. High level language
2. Data manipulation tool
3. Data analytics tool
4. ETL & Reporting tool

Quick navigation through sas window:
editor window  - f5 - is used to write your sas code.
log window      - f6 - is used to to see syntax error and information
                                about the code submitted
output window - f7 - is used to see the result of successfully executed code
explorer window     - is used to toggle in between the libraries and hard drives.
result window         - is used to store all your result in table of contents manner.

to execute or to submit code -
1. press f3 or
2. click on submit button

Stages in SAS -
1. extraction
2. manipulation
3. analysis
4. reporting


Components of SAS-
1. DATA steps - extraction and manipulation
2. PROC steps - analysis,reporting


SAS terminology:
------------------------------------
  General               SAS
------------------------------------
   tables                  datasets


  rows                   observations
  columns              variables