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

Thursday, March 29, 2012

Sending email through SAS

Today we will learn how to send mail though SAS. Below I have mentioned only method and we will come up with new methods very soon.

Small description about this method:
Below program will send an mail to respective user with log file attached and report of "failure or success of program".


Method 1: DATA STEP

FILENAME MYLIB EMAIL 'myname@mysite.com' EMAILID = "Microsoft Outlook" ;

DATA _NULL_ ;
IF &SYSERR = 0 THEN DO ;
FILE MYLIB
TO='abc@mysite.com'
SUBJECT= 'TEST: Log file for review'
ATTACH= ('C:\My Folder\LOG FILES\TEST.log') ;
PUT 'Hi,' ;
PUT ' ' ;
PUT 'Attached you will find the sas log ready for your review.' ;
PUT ' ' ;
PUT 'Thanks,' ;
PUT 'Aniruddha' ;
END ;
ELSE DO ;
FILE MYLIB
TO='abc@mysite.com'
SUBJECT= 'TEST: Error in Log file' ;
ATTACH= ('C:\My Folder\LOG FILES\TEST.log') ;
PUT 'Hi,' ;
PUT ' ' ;
PUT 'ATTENTION PLEASE!' ;
PUT 'There was an error when this program ran.' ;
PUT 'Please check the program and resubmit.' ;
PUT ' ' ;
PUT 'Thanks,' ;
PUT 'Aniruddha' ;
END ;
RUN ;

/*****************************************************************************************
We may receive an message as below:

A program is trying to automatically send e-mail on your behalf.
Do you want to allow this? If this is unexpected, it may be a
virus and you should choose "No".

To Overcome this message visit: http://support.sas.com/kb/5/335.html
*****************************************************************************************/

Reference: SUGI 29- Automated distribution of SAS® results, by Jacques PagĂ©, Les Services Conseils HARDY, Quebec, Qc

NOTE: Thanks to Google!

Hope this helps you!...