Pages

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

Grouping variables or creating a new variable using

Grouping variables or creating a new variable using
IF/THEN ELSE statement

data a;
input dens percent @@;
datalines;
0.453 0.42..0.470 0.46..0.396 0.39 .0.430 0.46
;
run;

data a; set a ;
if dens>=0.450 and percent>=0.40 then type='T';
else type='J' ;
run; 

If you need to kill a SAS job, after issuing the kill -9 job# command

If you need to kill a SAS job, after issuing the kill -9 job# command

Use the following command to delete the SAS work directory related to this job:

cleanwork /tmp

=> you don't need to go to the /tmp directory to locate and remove your SAS  working directory.

Hint:
For this to work, add the following to your setenv PATH statement in your .cshrc file:
/usr/local/src/sas612/utilities/bin

(if you put this at the end of setenv PATH statement, don't forget the ":" before this path).

Lost SAS Code

If you happen to be one of the unlucky programmers who lost the SAS code … because you didn’t save it. There is solution for that…. SAS System automatically takes the backup of the SAS code for every 10 minutes (default);

Just look in the following location:

C:\Documents and Settings\Programmer Name\Application Data\SAS\EnhancedEditor\


Note: Replace “Programmer Name” with your login user id of the System you are using;If you go the specified location above, you will see a copy of the unsaved version of the SAS code.It will be quick if you search files with the extension name ‘.SAS’ (extension for auto-saved SAS codes)

If you want, you can also change the 10-minute time interval for Auto save…. Go to…
Tools ► Options ► Preferences ►Edit.
In the preferences dialog box, make sure to the change the time under Autosave every … And click on OK ..

How to use Lock Statement in SAS: -
A dataset may become locked explicitly using the LOCK statement or automatically if it is being modified by another SAS program. When a dataset has a lock on it, it cannot be read or modified until the lock is removed. One very easy method for waiting for the lock to be released is to use the FILELOCKWAIT libname option. For example,
libname MYLIB "/mypath/sunil" filelockwait=10;
If a dataset in MYLIB is locked and another SAS program attempts to read it, SAS will wait up to 10 seconds before giving an error.