Pages

Tuesday, January 22, 2013

Basic Differences Between Proc MEANS and Proc SUMMARY in SAS

Basic Differences Between Proc MEANS and Proc SUMMARY

Proc SUMMARY and Proc MEANS are essentially the same procedure.  Both procedures compute descriptive statistics.  The main difference concerns the default type of output they produce.  Proc MEANS by default produces printed output in the LISTING window or other open destination whereas Proc SUMMARY does not.  Inclusion of the print option on the Proc SUMMARY statement will output results to the output window.
The second difference between the two procedures is reflected in the omission of the VAR statement.  When all variables in the data set are character the same output: a simple count of observations, is produced for each procedure.  However, when some variables in the dataset are numeric, Proc MEANS analyses all numeric variables not listed in any of the other statements and produces default statistics for these variables (N, Mean, Standard Deviation, Minimum and Maximum). 
Using the SASHELP data set SHOES the example reflecting this difference is shown.
proc means data = sashelp.shoes;
run;
Basic differences between Proc MEANS and Proc SUMMARY1
proc summary data = sashelp.shoes print;
run;
Basic differences between Proc MEANS and Proc SUMMARY2
Inclusion of a VAR statement in both Proc MEANS and Proc SUMMARY, produces output that contains exactly the same default statistics.
Using the SASHELP data set SHOES the example reflecting this similarity is shown.
proc means data = sashelp.shoes;
  class product;
  var Returns;
run;
proc summary data = sashelp.shoes print;
  class product;
  var Returns;
run;
Basic differences between Proc MEANS and Proc SUMMARY4

Sunday, December 9, 2012

How do I read/write Excel files in SAS?


Reading an Excel file into SAS

Suppose that you have an Excel spreadsheet called auto.xls. The data for this spreadsheet are shown below.
MAKE           MPG  WEIGHT PRICE
AMC Concord    22   2930  4099
AMC Pacer      17   3350  4749
AMC Spirit     22   2640  3799
Buick Century  20   3250  4816
Buick Electra  15   4080  7827
Using the Import Wizard is an easy way to import data into SAS.  The Import Wizard can be found on the drop down file menu.  Although the Import Wizard is easy it can be time consuming if used repeatedly.  The very last screen of the Import Wizard gives you the option to save the statements SAS uses to import the data so that they can be used again.  The following is an example that uses common options and also shows that the file was imported correctly.
PROC IMPORT OUT= WORK.auto1 DATAFILE= "C:\auto.xls" 
            DBMS=xls REPLACE;
     SHEET="auto1"; 
     GETNAMES=YES;
RUN;
  • The out= option in the proc import tells SAS what the name should be for the newly-created SAS data file and where to store the data set once it is imported. 
  • Next the datafile= option tells SAS where to find the file we want to import. 
  • The dbms= option is used to identify the type of file being imported. 
  • The replace option will overwrite an existing file.
  • To specify which sheet SAS should import use the sheet="sheetname" statement.  The default is for SAS to read the first sheet.  Note that sheet names can only be 31 characters long.
  • The getnames=yes is the default setting and SAS will automatically use the first row of data as variable names.  If the first row of your sheet does not contain variable names use the getnames=no

Writing Excel files out from SAS

It is very easy to write out an Excel file using proc export in SAS.
Here is a sample program that writes out SAS data called mydata to an Excel file called mydata.xls into the directory "c:\dissertation".
proc export data=mydata outfile='c:\mydata.xls' dbms = xls replace;
run;

Friday, August 3, 2012

OLTP and OLAP


What is OLTP?
OLTP is abbreviation of On-Line Transaction Processing. This system is an application that modifies data the instance it receives and has a large number of concurrent users.

What is OLAP?
OLAP is abbreviation of Online Analytical Processing. This system is an application that collects, manages, processes and presents multidimensional data for analysis and management purposes.

What is the difference between OLTP and OLAP?
Data Source
OLTP: Operational data is from original data source of the data
OLAP: Consolidation data is from various source.

Process Goal
OLTP: Snapshot of business processes which does fundamental business tasks
OLAP: Multi-dimensional views of business activities of planning and decision making

Queries and Process Scripts
OLTP: Simple quick running queries ran by users.
OLAP: Complex long running queries by system to update the aggregated data.

Database Design
OLTP: Normalized small database. Speed will be not an issue due to smaller database and normalization will not degrade performance. This adopts entity relationship(ER) model and an application-oriented database design.
OLAP: De-normalized large database. Speed is issue due to larger database and de-normalizing will improve performance as there will be lesser tables to scan while performing tasks. This adopts star, snowflake or fact constellation mode of subject-oriented database design.

Describes the foreign key columns in fact table and dimension table?
Foreign keys of dimension tables are primary keys of entity tables.
Foreign keys of facts tables are primary keys of Dimension tables.

Dimensional Modeling


What is Dimensional Modeling?
Dimensional data model concept involves two types of tables and it is different from the 3rd normal form. This concepts uses Facts table which contains the measurements of the business and Dimension table which contains the context(dimension of calculation) of the measurements.

What is Fact table?
Fact table contains measurements of business processes also fact table contains the foreign keys for the dimension tables. For example, if your business process is “paper production” then “average production of paper by one machine” or “weekly production of paper” would be considered as measurement of business process.

What is Dimension table?
Dimensional table contains textual attributes of measurements stored in the facts tables. Dimensional table is a collection of hierarchies, categories and logic which can be used for user to traverse in hierarchy nodes.

What are the Different methods of loading Dimension tables?
There are two different ways to load data in dimension tables.
Conventional (Slow) :
All the constraints and keys are validated against the data before, it is loaded, this way data integrity is maintained.
Direct (Fast) :
All the constraints and keys are disabled before the data is loaded. Once data is loaded, it is validated against all the constraints and keys. If data is found invalid or dirty it is not included in index and all future processes are skipped on this data.

Friday, June 22, 2012

How to export SAS results to an Excel spreadsheet?

In order to use the techniques , you must have the following software:
1. Base SAS 9.1.3 or later, on any supported operating system and hardware.
2. Microsoft Excel 2002 or later (also referred to as Microsoft Excel XP).
3. An updated version of the SAS ExcelXP ODS tagset.

We will be using the Output Delivery System (ODS) to do so.  ODS allows you to generate tabular output from your raw output that can be placed into Excel sheets.  In the code below, we are creating an Excel file (giving it a name and location), indicating a style to be used ("minimal" in this example), and specifying a few other options. 
ODS TAGSETS.EXCELXP
file='D:\work\sas9\regression.xls'
STYLE= minimal
OPTIONS ( Orientation = 'landscape'
FitToPage = 'yes'
Pages_FitWidth = '1'
Pages_FitHeight = '100' );
 
ODS TAGSETS.EXCELXP file='D:\work\sas9\tab2.xls' STYLE=Printer OPTIONS ( Orientation = 'landscape' FitToPage = 'yes' Pages_FitWidth = '1' Pages_FitHeight = '100' embedded_titles = 'yes'); 

The first ODS statement ( ) closes the LISTING destination, which writes output to a listing file in batch mode, or to
the Output window when SAS is run interactively. We only want to generate XML output for use with Excel.
The second ODS statement ( ) uses the ExcelXP tagset to generate the XML output and then stores the output in a
file. The STYLE option controls the appearance of the output, such as the font and color scheme. To see a list of
ODS styles that are available for use at your site, submit the following SAS code:
ods listing;
proc template; list styles; run; quit;
The third ODS statement ( ) closes and releases the XML file so that it can be opened with Excel.

We employed a few of the "options" to format our results in Excel.  To see the full list of options, run the SAS code below:
filename temp temp; ods tagsets.ExcelXP file=temp options(doc='help'); ods tagsets.ExcelXP close;

USING ODS TO CREATE THE MULTI-SHEET EXCEL WORKBOOK
By default, the ExcelXP tagset will create a new worksheet each time a SAS procedure creates new tabular output.
Our sample code executes the REPORT procedure to create the first worksheet. Subsequent worksheets are
created by the PRINT procedure, with a new worksheet being created for each distinct BY group.



SAS CODE TO CREATE THE EXCEL WORKBOOK
Here is a listing of the basic SAS code used to create the Excel workbook:

options center;
ods listing close;
ods tagsets.ExcelXP path='output-directory' file='trial.xml' style=XLsansPrinter;
* Create a summary table to use as the summary/table of contents;
proc means data=sample.trial sum noprint;
by patient;
var arrhythmia flutteratr angina;
id sex drug;
output out=trialsummary(drop=_type_ _freq_) sum=;
run; quit;
* Create the summary/table of contents worksheet;
title 'Summary of Adverse Events';
footnote;
proc report nowindows data=trialsummary split='*';
columns patient sex drug arrhythmia flutteratr angina;
define patient / display;
define sex--drug / display;
define arrhythmia--angina / analysis n;
rbreak after / summarize;
run; quit;
* Create the detailed worksheets for each patient;
title 'Patient Visit Log';
footnote 'Click to return to AE Summary';
options missing=' ';
proc print data=sample.trial noobs label split='*';
by patient sex drug dosage;
pageby patient;
id visit visitdate;
var systolic diastolic;
var arrhythmia flutteratr angina;
label patient = 'Patient'
sex = ' Gender'
drug = ' Treatment'
dosage = ' Dosage (mg)';
run; quit;
options missing='.';
ods tagsets.ExcelXP close;

Asone can see ( ), the ExcelXP tagset is used to generate the output, and the XLsansPrinter style is used to
control the appearance aspects of the output. The MEANS procedure ( ) is used to create a summary table, and
PROC REPORT ( ) is run to create the first worksheet based on this summarized data. The PRINT procedure ( )



Tuesday, June 12, 2012

Creating a new variable by compressing several variables

Creating a new variable by compressing several variables


test family rep female male tree
1 A 1 F43 M28 1
1 B 1 F22 M02 2
1 B 2 F22 M02 4

data a ; set a;
ID=compress(test||family||rep||tree); *Combine factors using ||, remove any trailing blank using COMPRESS;

CROSSno=trim(female||male); *Combine FEMALE and MALE variables with ||, remove any trialing blank using TRIM;
CROSSblank=female||male ; * Trim or Compress is not used. New variable may contain blanks;

Proc print data=a ; run;
Result:

test family rep female male tree ID CROSSno CROSSblank
1 A 1 F43 M28 1 1A11 F43M28 F43 M28 
1 B 1 F22 M02 2 1B12 F22M02 F22 M02
1 B 2 F22 M02 4 1B24 F22M02 F22 M02

Using DIF and LAG functions

Using DIF and LAG functions

DIF function creates a new variable (D) by taking the difference of observations of another variable (X).Syntax, DIF<n>(argument); *<-- n specifies number of LAGS

data two;
input X @@;
Z=lag(x);
D=dif(x);
datalines;
1 2 6 4 7;
proc print data=two;
run;

Results of the PROC PRINT step follow:
X Z D
1 . .
2 1 1
6 2 4
4 6 – 2