200 likes | 250 Views
BMTRY 789 Lecture 7: SAS Functions. Readings – Chapters 17 & 18 Lab Problems – 17-1, 17-11, 18-5, 18-7, 18-8 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….
E N D
BMTRY 789 Lecture 7: SAS Functions Readings – Chapters 17 & 18 Lab Problems – 17-1, 17-11, 18-5, 18-7, 18-8 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… SIMON’S IQ DWINDLES THE CLOSER HE GETS TO HIS COMPUTER BMTRY 789 Introduction to SAS Programming
Just don’t turn into these guys.. BMTRY 789 Introduction to SAS Programming
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. BMTRY 789 Introduction to SAS Programming
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); BMTRY 789 Introduction to SAS Programming
Log ( ); Log10 ( ); Sin ( ); Cos ( ); Tan ( ); Int ( ); SQRT ( ); Weekday ( ); MDY ( , , ); Round (x, 1); Mean ( ); RANUNI ( ); Put ( ); Input ( ); Lag ( ); Dif ( ); N ( ); NMISS ( ); Common Functions BMTRY 789 Introduction to SAS Programming
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? BMTRY 789 Introduction to SAS Programming
The Mean Function Mean_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) BMTRY 789 Introduction to SAS Programming
Time and Date Functions AdmitDate = 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. BMTRY 789 Introduction to SAS Programming
Converting Numeric to Character and Character to Numeric • Put Function is used to convert a numeric variable to a character variable Syntax: NewVar = Put (OldVar, Format); DayName = Put (Date, Weekdate3.); • Input Function is used to convert a character variable to a numeric variable Syntax: 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. BMTRY 789 Introduction to SAS Programming
Other SAS Functions Index (source, string); Finds the position of the string in the source school= “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 BMTRY 789 Introduction to SAS Programming
Other SAS Functions (cont.) Left(string); -returns a left-justified character variable Length(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 string Example of use: field = ‘smith, joe’; first = scan(field,2,” ,”); Results in first equal to joe BMTRY 789 Introduction to SAS Programming
Other SAS Functions (cont.) New_char_var=Translate(char_var,to_string,from_string) –changes the from_string to the to_string Example 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 BMTRY 789 Introduction to SAS Programming
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! BMTRY 789 Introduction to SAS Programming
Substr Example If 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 Q2 AST2337 12/18/65 A D JLM4875 02/03/33 C A TLP1147 07/23/18 A B BMTRY 789 Introduction to SAS Programming
My Answer PaNum = Input(Substr(PTID,4,4),4.); BMTRY 789 Introduction to SAS Programming
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 Good 001 ABCdjADbc 5 002 cdD BCAcc 4 003 BCbADDaab 0 BMTRY 789 Introduction to SAS Programming
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; BMTRY 789 Introduction to SAS Programming
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. BMTRY 789 Introduction to SAS Programming
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; BMTRY 789 Introduction to SAS Programming