Post on 30-Jan-2016
description
BMTRY 789 Lecture 7: SAS Functions
Readings – Chapters 17 & 18Lab Problems – 17-1, 17-11, 18-5, 18-7, 18-8Homework Due – HW 4Homework for Next Week – Book Problems 17.2, 17.4, 17.8, 18.2, 18.4
Summer 2008 BMTRY 789 Introduction to SAS Programming 2
With your new-found SAS knowledge, this will never happen to you…
SIMON’S IQ DWINDLES THE CLOSER HE GETS TO HIS COMPUTER
Summer 2008 BMTRY 789 Introduction to SAS Programming 3
Just don’t turn into these guys..
Summer 2008 BMTRY 789 Introduction to SAS Programming 4
SAS Functions
As defined by SAS:A SAS function is a keyword that you
use to perform a specific computation or system manipulation. Functions return a value, might require one or more arguments, and can be used in expressions.
Summer 2008 BMTRY 789 Introduction to SAS Programming 5
SAS Functions Previously created SAS functions are
used to simplify some complex programming problems
Usually arithmetic or mathematical calculations
Syntax of Function used in an expression:NewVar = FunctionName (VariableName);
Summer 2008 BMTRY 789 Introduction to SAS Programming 6
Common FunctionsLog ( );Log10 ( );Sin ( );Cos ( );Tan ( );Int ( );SQRT ( );Weekday ( );MDY ( , , );
Round (x, 1);Mean ( );RANUNI ( );Put ( );Input ( );Lag ( );Dif ( );N ( );NMISS ( );
Summer 2008 BMTRY 789 Introduction to SAS Programming 7
SAS Functions with lists of variables
You can use variable lists in all the statistical summary functions by preceding the list with the word ‘of’.
xm = mean(of x1-x8);vmean = mean (of thisvar - thatvar);
Without the ‘of’, the single dash is interpreted in its usual way, as a minus sign
xm = mean (of x1-x5);is the same as
xm = (x1 + x2 + x3 + x4 + x5)/5;EXCEPT UNDER WHAT CONDITIONS?
Summer 2008 BMTRY 789 Introduction to SAS Programming 8
The Mean FunctionMean_x = Mean(of x1-x5);Mean_x = (x1 + x2 + x3 + x4 + x5)/5;
An important difference between the Mean Function and an expression that calculates a mean is that the Mean Function returns the mean of the nonmissing values. The equation above would return a missing value if ANY of the X values were missing while the Mean Function would only do that if ALL of the X values were missing.
(This is also true for the Min, Max, Sum, STD, and STDERR functions)
Summer 2008 BMTRY 789 Introduction to SAS Programming 9
Time and Date FunctionsAdmitDate = MDY ( Mon,Day,Yr);
This function can be helpful if you do not have date data in any of the formats that are standard in SAS.
Num_Quarter = INTCK (‘QTR’, Admit, Today());
The INTCK function returns the number of intervals between any two dates.
Summer 2008 BMTRY 789 Introduction to SAS Programming 10
Converting Numeric to Character and Character to Numeric
Put Function is used to convert a numeric variable to a character variableSyntax: NewVar = Put (OldVar, Format);DayName = Put (Date, Weekdate3.);
Input Function is used to convert a character variable to a numeric variableSyntax: NewVar = Input (OldVar, Format);ID = Input (Compress (SS, ‘-’), 9.);
*Do not confuse the Input and Put functions with the previously learned Input and Put statements.
Summer 2008 BMTRY 789 Introduction to SAS Programming 11
Other SAS FunctionsIndex (source, string); Finds the position of the
string in the sourceschool= “university of california”;I = index (school, ‘cal’);Results in I equal to 15
Indexc (source, string); Finds the position of any character in the string within the source
place=“berkeley, ca”;I = indexc(place, ‘abc’);Results in I equal to 1, since b is in the position 1
*Both of these functions return 0 if there is no match
Summer 2008 BMTRY 789 Introduction to SAS Programming 12
Other SAS Functions (cont.)
Left(string); -returns a left-justified character variableLength(string) –returns number of characters in a string
[returns 1 if missing, 12 if uninitialized]Right(string) –returns a right-justified character variable
Example of use: both = right(one) || left(two);Scan(string,n,<delims>); -returns the nth ‘word’ in the
stringExample of use: field = ‘smith, joe’;
first = scan(field,2,” ,”);Results in first equal to joe
Summer 2008 BMTRY 789 Introduction to SAS Programming 13
Other SAS Functions (cont.)
New_char_var=Translate(char_var,to_string,from_string) –changes the from_string to the to_stringExample of use:
Ques1=Translate(ques1,’ABCDE’,’12345’);Trim(string); -returns string with trailing blanks removed
Example of use: Name = Trim (First) || ‘ ‘ || Trim(Last);Upcase(string); -converts lowercase to uppercase
Summer 2008 BMTRY 789 Introduction to SAS Programming 14
Substring Function
Substr(char_var, start, length);
A string is just another term used to describe character data. Therefore, when you use string functions, you must apply them only to character variables!
Summer 2008 BMTRY 789 Introduction to SAS Programming 15
Substr ExampleIf you wanted to get the numeric code out of
the following data, and turn the new variable into a numeric variable how would you do it?
Patient Data
PTID DOB Q1 Q2AST2337 12/18/65 A DJLM4875 02/03/33 C ATLP1147 07/23/18 A B
Summer 2008 BMTRY 789 Introduction to SAS Programming 16
My Answer
PaNum = Input(Substr(PTID,4,4),4.);
Summer 2008 BMTRY 789 Introduction to SAS Programming 17
Verify Function A great way to test for valid data. You may want to be sure that only
certain values are present in your character data.
Survey Data Good=Verify(Qans, ‘ABCDabcd’);
ID Qans Results of Good001 ABCdjADbc 5002 cdD BCAcc 4003 BCbADDaab 0
Summer 2008 BMTRY 789 Introduction to SAS Programming 18
Unpacking a string Lets use the Survey data we just had and now make
the Qans variable into nine separate variable (A1-A9).
Data Unpack;Set Survey;
Array A[9]; *Define array; Do J = 1 to 9;
/*Assign 9 new variables using substr of Qans variable*/ A[J]=Substr(Qans,J,1);
End; Drop J;
Run;
Summer 2008 BMTRY 789 Introduction to SAS Programming 19
Computing a Moving Average LAG Function Syntax: LAGn( );Performing computations between
observations in a DATA step is much tougher than within-observation processing.
Computing a mean such as:Meanx=Mean(of x1-x3);Is much easier than computing the mean
of an observation and the two previous observations of that same variable.
Summer 2008 BMTRY 789 Introduction to SAS Programming 20
Moving Average (cont.)Data Moving;
Set Old;x1=LAG(x);x2=LAG2(x);Avg=MEAN(of x x1 x2);If _n_ GE 3 then output; /*for the first two iterations at least one of the values of x1 and x2 will be missing so we just want to output the results to the new data set (Moving) after that point*/
Run;