Friday, November 21, 2008

How to detect missing values using ARRAYS

Using an array in SAS to detect missing values

Direct link: http://ssc.utexas.edu/consulting/answers/sas/sas65.html

Question:
How do I exclude observations from my PROC FREQ analysis when a value is missing from a list of variables?

Answer:
In the SAS DATA step, you can create a new variable ("miss" in the example below) that is set equal to 1 when a variable has a missing value, 0 otherwise. Use the ARRAY statement and a DO loop to check for missing values across a list of variables; the syntax is:

DATA one ;
INFILE xxx;
INPUT a b c d e;
miss=0;
ARRAY vv(5) a b c d e ;
DO i=1 TO 5 ;
IF vv(i)=. THEN DO;
miss=1 ;
i=5;

END;
END;
RUN;


PROC FREQ;
WHERE miss =0;
TABLES a b c d e ;
RUN ;

Here, the array "vv" has 5 elements (a,b,c,d,e), and the loop "i" is likewise set to 5.

For each observation, the loop iterates 5 times, checking for missing values across the list of 5 variables. When a missing value is encountered, the variable "miss" is set to 1 and the loop stopped for that observation.

"Miss" was initially set to zero, and it is only changed if an observation has missing data on any of the five variables. The PROC FREQ then uses the WHERE statement to restrict processing to observations having "miss" set to zero.
Want to know more about Missing Values...

Wednesday, November 19, 2008

SAS System Options in the UNIX environment

SAS System Options in the UNIX environment

Direct link:http://ssc.utexas.edu/consulting/answers/sas/sas55.html

Question:
How do I specify SAS system options in the UNIX environment?

Answer:
How you specify SAS system options depends on how you use SAS in the UNIX environment.
If you use SAS via an X-terminal or X-terminal emulation software such as Exodus or MacX, the command to launch SAS on ITS UNIX systems is
/usr/local/sas/sas

SAS system options are preceded by a hyphen and immediately follow the SAS command. For example, if you want to have SAS write its work files (including temporary datasets) to a directory called "mysasdir" located one level below your own current working directory, the syntax for invoking this option would be:

/usr/local/sas/sas -work ./mysasdir

If you use the SAS display manager system via a vt100 terminal interface such as telnet, the usual command to launch SAS is:
/usr/local/sas/sas -fsd ascii.vt100

The -fsd portion of this command is a SAS option which means "full screen device". You could add another SAS system option to this command, such as the -work option mentioned above:
/usr/local/sas/sas -fsd ascii.vt100 -work ./mysasdir

If you run SAS noninteractively by supplying the SAS program file "mysasprog" in the current directory, you would change your command from:
/usr/local/sas/sas mysasprog
to:
/usr/local/sas/sas -work ./mysasdir mysasprog

This would direct SAS to write any work files to the "mysasdir" directory as it processes the contents of your SAS program "mysasprog".

Each of these examples assumes you want to use a particular SAS system option once or a few times. If you intend to use a SAS system option repeatedly, it can be a nuisance to specify the same option each time you invoke SAS. For this type of situation, you can copy the config.sas612 file located in the /usr/local/sas/sas612 directory and edit it using a UNIX text editor.

The config.sas file is a SAS configuration file that contains default settings for a number of SAS system options; you may change the default settings of these options as well as delete or add options of your choosing to this file. If you then launch SAS from the same directory as the config.sas612 file, SAS will use those options.

How to Debug the SAS code

Debugging SAS code

Direct link: http://ssc.utexas.edu/consulting/answers/sas/sas54.html

Question:
I have a huge SAS program that isn't working. The results I get are not right but there are no errors or warnings in the SAS log. How can I figure out where I went wrong?

Answer:
To debug a SAS program that produces no syntax errors, follow these six steps:

1. Check to see that your original data input is correct for all variables.

2. If the data is input to SAS correctly, go to the other end of the program. Select a variable or a small set of variables involved in the analyses where you get the wrong results. Use PROC FREQ, PROC MEANS, and/or PROC PRINT to examine these variables. There should be a problem with at least one; identify exactly how these variables are incorrect.

3. Now follow these variables back through each operation you performed, always looking at the characteristics in question. In this way you can narrow down the exact step where an error occurs. Prior to the questionable step, the variable characteristics will be appropriate; after the step they will be inappropriate.

4. Look carefully at the code for that step. Continue using PROC PRINT, PROC FREQ, and PROC MEANS to examine the effect of each statement. In this way, you can identify the exact statement or statement group that is not working as you expect.

5. Next, get a clear understanding of how the statement is working (as opposed to how you think it should work) by consulting the SAS Help function; click on the Help button in the SAS menu bar and scroll to SAS Help and Documentation; then, search for the particular statement or procedure. The results in hand should help you interpret the documentation.

6. Finally, determine the appropriate code for your needs. Remember to check for other statements that involve this mistake.

How to call SAS macros on UNIX

SAS FAQ #64: Calling SAS macros on UNIX systems

source: http://ssc.utexas.edu/consulting/answers/sas/sas64.html

Question:
On UNIX how do I call an external SAS MACRO that is in another file and not physically included in my SAS program?
Answer:
Put the SAS MACRO in a file having a name "macro_name.sas", where "macro_name" is the macro name from the SAS %MACRO statement.
When you want to use this macro in a SAS program, perform the following steps:

1) Use a FILENAME statement of the form:
FILENAME wheremac 'dir';
where "wheremac" is a fileref pointing to the directory where the macro file is stored, and "dir" is the directory path to where the macro file is stored.

2) Use an OPTIONS statement of the form:
OPTIONS SASAUTOS=wheremac;
to point to the directory containing the macro file.

3) Use a call for your macro as you normally would with the "%macroname" specification, where "macroname" is the first name of the macro file (and the name of the macro in the %MACRO statement).
For example if you had a macro to do a PROC PRINT stored in a file called "prt.sas", it could look like this:

%MACRO prt;
PROC PRINT;
RUN;

%MEND;

If you stored this file in your $HOME directory, you could run the SAS job:

FILENAME wheremac '$HOME/';
DATA one;
INPUT a b ;
CARDS;
1 23 2;
OPTIONS SASAUTOS=wheremac;
%prt
The above code will create the data set and print the data to the SAS listing file.

Friday, November 7, 2008

SAS Certification Assignments

SAS Certification Assignments Contents[hide]
1 Session 1
1.1 exercise 1
1.2 exercise 2
1.3 exercise 3
1.4 exercise 4
1.5 exercise 5
1.6 exercise 6
2 Session 2
2.1 exercise 1
2.2 exercise 2
2.3 exercise 3
2.4 exercise 4
2.5 exercise 5
3 Session 3
3.1 Exercise 1
3.2 Exercise 2
3.3 Exercise 3
3.4 Exercise 4
4 Session 4
4.1 Exercise 1
4.2 Exercise 2
4.3 Exercise 3
5 Session 5
5.1 exercise 1
5.2 exercise 2
5.3 exercise 3
5.4 exercise 4
5.5 exercise 5
6 Session 6
7 Session 7
7.1 exercise 1
7.2 exercise 2
7.3 exercise 3
8 Session 8
8.1 exercise 1
9 Session 9
9.1 exercise 1

SAS Certification Examples(part 2)
Session 6
1.1 Example 1
1.2 Example 2
1.3 Example 3
1.4 Example 4
1.5 Example 4a
1.6 Example 5
1.7 Closing examples
2 Session 7
2.1 In class
2.2 Some similar functions,formats and informats
3 Session 8
4 Session 9
4.1 Example 1
4.2 Example 2
4.3 Example 3
4.4 Example 3A
4.5 Example 4
5 Session 10
5.1 Example 1
6 Example 2
7 Examples for Model Procedure
7.1 A second example for parabolic curve


Answers to SAS Certification Assignments
C
Certification Summary---Summary Reports
Class room for the workshop series
Concise Glossary for SAS

L
Lecture Notes 9/12/2006
Lecture Notes 9/5/2006

S
SAS Certification Assignments
SAS Certification Essential Reading SAS Certification Examples

S cont.
SAS Certification Examples and Exercises
SAS Certification Examples(part 2)
SAS Lesson Plan for SAS Certification

U
User:Ywang10/Work

source: http://wiki.binghamton.edu/index.php/SAS_Certification_Assignments

Wednesday, November 5, 2008

INDEX TO SAS TUTORIALS

Index to Statistics Tutorials

PROC MEANS Tutorial (Descriptive statistics)
PROC UNIVARIATE Tutorial (Distribution analysis)
New:
PROC UNIVARIATE - Advanced Tutorial
PROC CORR Tutorial (Correlation)
PROC FREQ Tutorial 1 (Frequency Tables/Goodness of Fit)
PROC FREQ Tutorial 2 (Two-way tables)
PROC TTEST Tutorial (Two sample and paired t-tests)
New:
A comparison of Paired & Independent Sample t-tests
PROC ANOVA & GLM Tutorial (One-Way ANOVA)
PROC GLM Tutorial (Repeated measures ANOVA using PROC GLM)
New:
Survival Analysis & comparison of groups using PROC LIFEREG
Bland-Altman Analysis (Comparing two measures)
Inter-Rater Reliability, Kappa, Weighted Kappa (PROC FREQ)
New:
SAS Functions (2-part tutorial)
Special SAS Topics
New:
Read and Write SAS Data Sets the EASY way
Setting the SAS Initial Folder (default directory)
Using SAS ODS Output, Styles, Graphics, Data Data files and SAS code for tutorials

FIRST. and LAST. variables: Data step processing within by groups using the SET statement

FIRST. and LAST. variables: Data step processing within by groups
If you use a by statement along with a set statement in a data step then SAS creates two automatic variables, FIRST.variable and LAST.variable, where variable is the name of the by variable. FIRST.variable has a value 1 for the first observation in the by group and 0 for all other observations in the by group. LAST.variable has a value 1 for the last observation in the by group and 0 for all other observations in the by group.
The code shown below is available here.

data temp;
input group x;
cards;
1 23
1 34
1 .
1 45
2 78
2 92
2 45
2 89
2 34
2 76
3 31
4 23
4 12
;
run;
/**************************************************
The automatic variables first.group and last.group
are not saved with the data set. Here we write them
to data set variables to show their contents.
**************************************************/
data new;
set temp;
by group;
first=first.group;
last=last.group;
run;
proc print;
title 'Raw data along with first.group and last.group';
run;
/**************************************************
A common task in data cleaning is to identify
observations with a duplicate ID number. If we set
the data set by ID, then the observations which
are not duplicated will be both the first and the
last with that ID number. We can therefore write
any observations which are not both
first.id and
last.id to a separate data set and examine them.
**************************************************/
data single dup;
set temp;
by group;
if first.group and last.group then output single;
else output dup;
run;
/**************************************************
We may also want to do data set processing within
each by group. In this example we construct the
cumulative sum of the variable X within each group.
**************************************************/
data cusum(keep=group sum);
set temp;
by group;
if first.group then sum=0;
sum+x;
if last.group then output;
run;
proc print data=cusum noobs;
title 'Sum of X within each group';
run;
/**************************************************
As an aside, if you simply want the sum of X within
each group, one of the many way of obtaining this
is with PROC PRINT.
**************************************************/
proc print data=temp;
title 'All data with X summed within each group';
by group;
sum x;
sumby group;
run;

source: http://www.pauldickman.com/teaching/sas/set_by.php

Learn how to view SAS dataset labels without opening the dataset directly in a SAS session. Easy methods and examples included!

Quick Tip: See SAS Dataset Labels Without Opening the Data Quick Tip: See SAS Dataset Labels With...