bmtry 789 lecture 7: sas functions

20
BMTRY 789 Lecture 7: SAS Functions Readings – Chapters 17 & 18 Lab Problems – 17-1, 17-11, 18-5, 18-7, 18-8 Homework Due – HW 4 Homework for Next Week – Book Problems 17.2, 17.4, 17.8, 18.2, 18.4

Upload: lot

Post on 30-Jan-2016

38 views

Category:

Documents


0 download

DESCRIPTION

BMTRY 789 Lecture 7: SAS Functions. Readings – Chapters 17 & 18 Lab Problems – 17-1, 17-11, 18-5, 18-7, 18-8 Homework Due – HW 4 Homework for Next Week – Book Problems 17.2, 17.4, 17.8, 18.2, 18.4. With your new-found SAS knowledge, this will never happen to you…. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: BMTRY 789 Lecture 7:  SAS Functions

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

Page 2: BMTRY 789 Lecture 7:  SAS Functions

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

Page 3: BMTRY 789 Lecture 7:  SAS Functions

Summer 2008 BMTRY 789 Introduction to SAS Programming 3

Just don’t turn into these guys..

Page 4: BMTRY 789 Lecture 7:  SAS Functions

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.

Page 5: BMTRY 789 Lecture 7:  SAS Functions

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);

Page 6: BMTRY 789 Lecture 7:  SAS Functions

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 ( );

Page 7: BMTRY 789 Lecture 7:  SAS Functions

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?

Page 8: BMTRY 789 Lecture 7:  SAS Functions

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)

Page 9: BMTRY 789 Lecture 7:  SAS 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.

Page 10: BMTRY 789 Lecture 7:  SAS Functions

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.

Page 11: BMTRY 789 Lecture 7:  SAS Functions

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

Page 12: BMTRY 789 Lecture 7:  SAS Functions

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

Page 13: BMTRY 789 Lecture 7:  SAS Functions

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

Page 14: BMTRY 789 Lecture 7:  SAS Functions

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!

Page 15: BMTRY 789 Lecture 7:  SAS Functions

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

Page 16: BMTRY 789 Lecture 7:  SAS Functions

Summer 2008 BMTRY 789 Introduction to SAS Programming 16

My Answer

PaNum = Input(Substr(PTID,4,4),4.);

Page 17: BMTRY 789 Lecture 7:  SAS Functions

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

Page 18: BMTRY 789 Lecture 7:  SAS Functions

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;

Page 19: BMTRY 789 Lecture 7:  SAS Functions

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.

Page 20: BMTRY 789 Lecture 7:  SAS Functions

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;