functioning at a higher level: to improve your codetorsas.ca/attachments/file/20180914/functions...

17
2018-09-21 1 Copyright 2018 Peter Eberhardt, Fernwood Consulting Group Inc. All rights Reserved. TASS Functioning at a Higher Level: Using SAS® Functions to Improve Your Code Peter Eberhardt Fernwood Consulting Group Inc. Lucheng Shao Ivantis, Inc Copyright 2018 Peter Eberhardt, Fernwood Consulting Group Inc. All rights Reserved. Functioning at a Higher Level: Using SAS® Functions to Improve Your Code Peter Eberhardt

Upload: others

Post on 20-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Functioning at a Higher Level: to Improve Your Codetorsas.ca/attachments/File/20180914/Functions Peter Eberhardt 201… · 2018-09-21 3 Copyright 2018 Peter Eberhardt, Fernwood Consulting

2018-09-21

1

Copyright 2018 Peter Eberhardt, Fernwood Consulting Group Inc. All rights Reserved.

TASS

Functioning at a Higher Level:Using SAS® Functionsto Improve Your Code

Peter EberhardtFernwood Consulting Group Inc.

Lucheng ShaoIvantis, Inc

Copyright 2018 Peter Eberhardt, Fernwood Consulting Group Inc. All rights Reserved.

Functioning at a Higher Level: Using SAS® Functions to Improve Your Code

Peter Eberhardt

Page 2: Functioning at a Higher Level: to Improve Your Codetorsas.ca/attachments/File/20180914/Functions Peter Eberhardt 201… · 2018-09-21 3 Copyright 2018 Peter Eberhardt, Fernwood Consulting

2018-09-21

2

Copyright 2018 Peter Eberhardt, Fernwood Consulting Group Inc. All rights Reserved.

Functioning at a Higher Level: Using SAS® Functions to Improve Your Code

Agenda

• Examples• Examples• Yet more examples

• Run out of time

Copyright 2018 Peter Eberhardt, Fernwood Consulting Group Inc. All rights Reserved.

Functioning at a Higher Level: Using SAS® Functions to Improve Your Code

What is a Function

Page 3: Functioning at a Higher Level: to Improve Your Codetorsas.ca/attachments/File/20180914/Functions Peter Eberhardt 201… · 2018-09-21 3 Copyright 2018 Peter Eberhardt, Fernwood Consulting

2018-09-21

3

Copyright 2018 Peter Eberhardt, Fernwood Consulting Group Inc. All rights Reserved.

Functioning at a Higher Level: Using SAS® Functions to Improve Your Code

FunctionMain Entry: 1func·tionPronunciation: \ˈfəŋ(k)-shən\Function: nounEtymology: Latin function-, functio performance, from fungi to

perform; probably akin to Sanskrit bhuṅkte he enjoys

1: professional or official position : occupation2: the action for which a person or thing is specially fitted or

used or for which a thing exists : purpose3: any of a group of related actions contributing to a larger action

; especially : the normal and specific contribution of a bodilypart to the economy of a living organism

Source: Mirriam-Webser Online

Copyright 2018 Peter Eberhardt, Fernwood Consulting Group Inc. All rights Reserved.

Functioning at a Higher Level: Using SAS® Functions to Improve Your Code

FunctionMain Entry: 1func·tion4: an official or formal ceremony or social gathering5a: a mathematical correspondence that assigns exactly one

element of one set to each element of the same or another set5b: a variable (as a quality, trait, or measurement) that depends on

and varies with another <height is a function of age> ; also : result<illnesses that are a function of stress>

6: characteristic behavior of a chemical compound due to aparticular reactive unit ; also : functional group

Page 4: Functioning at a Higher Level: to Improve Your Codetorsas.ca/attachments/File/20180914/Functions Peter Eberhardt 201… · 2018-09-21 3 Copyright 2018 Peter Eberhardt, Fernwood Consulting

2018-09-21

4

Copyright 2018 Peter Eberhardt, Fernwood Consulting Group Inc. All rights Reserved.

Functioning at a Higher Level: Using SAS® Functions to Improve Your Code

FunctionMain Entry: 1func·tion7: a computer subroutine ; specifically : one that performs a

calculation with variables provided by a program and supplies theprogram with a single result

Copyright 2018 Peter Eberhardt, Fernwood Consulting Group Inc. All rights Reserved.

Functioning at a Higher Level: Using SAS® Functions to Improve Your Code

What is a function

• The SAS online help has this definition :A SAS function performs a computation

or system manipulation on arguments,and returns a value that can be used inan assignment statement or elsewherein expressions

Page 5: Functioning at a Higher Level: to Improve Your Codetorsas.ca/attachments/File/20180914/Functions Peter Eberhardt 201… · 2018-09-21 3 Copyright 2018 Peter Eberhardt, Fernwood Consulting

2018-09-21

5

Copyright 2018 Peter Eberhardt, Fernwood Consulting Group Inc. All rights Reserved.

Functioning at a Higher Level: Using SAS® Functions to Improve Your Code

Functions that deal with missing values

• Missing() + Sum()• MISSING(numeric-expression | character-expression)

• NMISS()– NMISS(argument-1<,...argument-n>)

• CMISS()– CMISS(argument-1 <, argument-2,...>)

• Call Missing()– CALL MISSING(varname1<, varname2, ...>)

Copyright 2018 Peter Eberhardt, Fernwood Consulting Group Inc. All rights Reserved.

Functioning at a Higher Level: Using SAS® Functions to Improve Your Code

Missing Values

• Missing Characters: ‘ ’ or length is 0• Missing Numeric Values:• (a) regular: .• (b) special characters: .A to .Z and ._• (c) ._ < . < .A < .Z < negative values < 0

< positive values

Page 6: Functioning at a Higher Level: to Improve Your Codetorsas.ca/attachments/File/20180914/Functions Peter Eberhardt 201… · 2018-09-21 3 Copyright 2018 Peter Eberhardt, Fernwood Consulting

2018-09-21

6

Copyright 2018 Peter Eberhardt, Fernwood Consulting Group Inc. All rights Reserved.

Functioning at a Higher Level: Using SAS® Functions to Improve Your Code

Example – How to count missing values

Obs Subject Eye_P1 Eye_P2 Eye_P3 Eye_P4 Eye_P5

1 111 19.6 21.3 18.2 .L .L

2 112 26.7 34.6 24.3 21.8 .N

3 113 18.8 20.3 20.8 18.1 17.9

4 114 14.7 15.9 .L .L .L

Copyright 2018 Peter Eberhardt, Fernwood Consulting Group Inc. All rights Reserved.

Functioning at a Higher Level: Using SAS® Functions to Improve Your Code

Example – How to count missing valuesUsing Missing() + Sum()

Obs Subject Eye_P1 Eye_P2 Eye_P3 Eye_P4 Eye_P5

1 111 19.6 21.3 18.2 .L .L

2 112 26.7 34.6 24.3 21.8 .N

3 113 18.8 20.3 20.8 18.1 17.9

4 114 14.7 15.9 .L .L .L

N_of_miss =sum( Missing(Eye_P1) , missing(Eye_P2),missing(Eye_P3), missing(Eye_P4),missing(Eye_P5),missing(BP));

BP

High

High

Low

Page 7: Functioning at a Higher Level: to Improve Your Codetorsas.ca/attachments/File/20180914/Functions Peter Eberhardt 201… · 2018-09-21 3 Copyright 2018 Peter Eberhardt, Fernwood Consulting

2018-09-21

7

Copyright 2018 Peter Eberhardt, Fernwood Consulting Group Inc. All rights Reserved.

Functioning at a Higher Level: Using SAS® Functions to Improve Your Code

Example – How to count missing valuesUsing Nmiss() and Cmiss()

Obs Subject Eye_P1 Eye_P2 Eye_P3 Eye_P4 Eye_P5

1 111 19.6 21.3 18.2 .L .L

2 112 26.7 34.6 24.3 21.8 .N

3 113 18.8 20.3 20.8 18.1 17.9

4 114 14.7 15.9 .L .L .L

N_of_miss = Nmiss(Eye_P1, Eye_P2, Eye_P3,Eye_P4, Eye_P5) + missing(BP);Or: N_of_miss = Cmiss(Eye_P1, Eye_P2, Eye_P3,Eye_P4, Eye_P5, BP);

BP

High

High

Low

Copyright 2018 Peter Eberhardt, Fernwood Consulting Group Inc. All rights Reserved.

Functioning at a Higher Level: Using SAS® Functions to Improve Your Code

Example – How to count missing valuesUsing CALL MISSING()

Obs Subject Eye_P1 Eye_P2 Eye_P3 Eye_P4 Eye_P5

1 111 19.6 21.3 18.2 .L .L

2 112 26.7 34.6 24.3 21.8 .N

3 113 18.8 20.3 20.8 18.1 17.9

4 114 14.7 15.9 .L .L .L

BP

High

High

Low

data Pressure_2;set Pressure;call missing(Eye_P4, Eye_P5, BP);/* code to set Eye_P4, Eye_P4, BP */run;

Page 8: Functioning at a Higher Level: to Improve Your Codetorsas.ca/attachments/File/20180914/Functions Peter Eberhardt 201… · 2018-09-21 3 Copyright 2018 Peter Eberhardt, Fernwood Consulting

2018-09-21

8

Copyright 2018 Peter Eberhardt, Fernwood Consulting Group Inc. All rights Reserved.

Functioning at a Higher Level: Using SAS® Functions to Improve Your Code

Dates

• Descriptive functions– date()

• day()• month()• year()• weekday()

– time()• hour()• minute()

Copyright 2018 Peter Eberhardt, Fernwood Consulting Group Inc. All rights Reserved.

Functioning at a Higher Level: Using SAS® Functions to Improve Your Code

Dates%macro runBatch;%let saturday = 7;%let currDate = %sysfunc(date());%let dispDate = %sysfunc(date(), yymmdd10.);%let currTime = %sysfunc(time());%let dispTime = %sysfunc(time(), hhmm5.);%let currDay = %sysfunc(weekday(&currDate));%let currHour = %sysfunc(hour(&currTime));

Page 9: Functioning at a Higher Level: to Improve Your Codetorsas.ca/attachments/File/20180914/Functions Peter Eberhardt 201… · 2018-09-21 3 Copyright 2018 Peter Eberhardt, Fernwood Consulting

2018-09-21

9

Copyright 2018 Peter Eberhardt, Fernwood Consulting Group Inc. All rights Reserved.

Functioning at a Higher Level: Using SAS® Functions to Improve Your Code

Dates%macro runBatch;%let saturday = 7;%let currDate = %sysfunc(date());%let dispDate = %sysfunc(putn(&currDate, yymmdd10.));%let currTime = %sysfunc(time());%let dispTime = %sysfunc(putn(&currTime , hhmm5.));%let currDay = %sysfunc(weekday(&currDate));%let currHour = %sysfunc(hour(&currTime));

Copyright 2018 Peter Eberhardt, Fernwood Consulting Group Inc. All rights Reserved.

Functioning at a Higher Level: Using SAS® Functions to Improve Your Code

Dates

%* only run on Saturday after 8:00 pm;%if &currDay = &saturday AND &currHour >= 20%then%do;

%let footer = "Saturday Night Results from processingon &dispDate @ &dispTime &currHour";

%* more processing;%end;

%mend;

Page 10: Functioning at a Higher Level: to Improve Your Codetorsas.ca/attachments/File/20180914/Functions Peter Eberhardt 201… · 2018-09-21 3 Copyright 2018 Peter Eberhardt, Fernwood Consulting

2018-09-21

10

Copyright 2018 Peter Eberhardt, Fernwood Consulting Group Inc. All rights Reserved.

Functioning at a Higher Level: Using SAS® Functions to Improve Your Code

Dates

• Computional– datepart()

• Converts a datetime variable to a date variable%let currDate = %sysfunc(date());data todayTrans prevTrans;

set SQLSRV.dailyTrans;if transDate = &currDate

then output todayTrans;else output prevTrans;

run;

Copyright 2018 Peter Eberhardt, Fernwood Consulting Group Inc. All rights Reserved.

Functioning at a Higher Level: Using SAS® Functions to Improve Your Code

Dates

• Computional– datepart()

• Converts a datetime variable to a date variabledata todayTrans prevTrans;

set SQLSRV.dailyTrans (rename=(transDate=transDtTm));drop transDtTm;transDate = datepart(transDtTm);if transDate = &currDate

then output todayTrans;else output prevTrans;

run;

Page 11: Functioning at a Higher Level: to Improve Your Codetorsas.ca/attachments/File/20180914/Functions Peter Eberhardt 201… · 2018-09-21 3 Copyright 2018 Peter Eberhardt, Fernwood Consulting

2018-09-21

11

Copyright 2018 Peter Eberhardt, Fernwood Consulting Group Inc. All rights Reserved.

Functioning at a Higher Level: Using SAS® Functions to Improve Your Code

Dates

• Computional– intck()

• Calculate intervals between two datesdata _null_;

start = '01jan2018'd;end = '01jun2018'd;days1 = end - start;days2 = intck('day', start, end);weeks = intck('week', start, end);months = intck('month', start, end);years = intck('year', start, end);put days1= days2= weeks= months= years=;

run;

Copyright 2018 Peter Eberhardt, Fernwood Consulting Group Inc. All rights Reserved.

Functioning at a Higher Level: Using SAS® Functions to Improve Your Code

Dates

• Computional– intck()

• Calculate intervals between two dates

start = '01jan2018'd;end = '01jun2018'd;put days1= days2= weeks= months= years=;

days1=151 days2=151 weeks=21 months=5 years=0

Page 12: Functioning at a Higher Level: to Improve Your Codetorsas.ca/attachments/File/20180914/Functions Peter Eberhardt 201… · 2018-09-21 3 Copyright 2018 Peter Eberhardt, Fernwood Consulting

2018-09-21

12

Copyright 2018 Peter Eberhardt, Fernwood Consulting Group Inc. All rights Reserved.

Functioning at a Higher Level: Using SAS® Functions to Improve Your Code

Dates

• Computional– intnx()

• Add/subtract intervals from a date to get a new datedata _null_;

start = '15may2018'd;monthB = intnx('month', start, 0, 'b'); * beginning;monthE = intnx('month', start, 0, 'e'); * end;put monthB = yymmdd10. monthE= yymmdd10.;

run;

monthB=2018-05-01 monthE=2018-05-31

Copyright 2018 Peter Eberhardt, Fernwood Consulting Group Inc. All rights Reserved.

Functioning at a Higher Level: Using SAS® Functions to Improve Your Code

Character• CAT family

– Concatenate variables– Replaces the || operator with trim() and/or left()

Page 13: Functioning at a Higher Level: to Improve Your Codetorsas.ca/attachments/File/20180914/Functions Peter Eberhardt 201… · 2018-09-21 3 Copyright 2018 Peter Eberhardt, Fernwood Consulting

2018-09-21

13

Copyright 2018 Peter Eberhardt, Fernwood Consulting Group Inc. All rights Reserved.

Functioning at a Higher Level: Using SAS® Functions to Improve Your Code

Character• CAT family

– Cat()• Concatenates, does not remove blanks

– Catq()• Concatenates using delimter plus many options

– Catt()• Concatenates, removes trailing blanks

– Cats()• Concatenates and removes blanks

– Catx()• Concatenates and removes blanks inserts delimiter

Copyright 2018 Peter Eberhardt, Fernwood Consulting Group Inc. All rights Reserved.

Functioning at a Higher Level: Using SAS® Functions to Improve Your Code

Characterdata catInHat;var1 = "I do "; var2 = "not eat";var3 = "green eggs"; var4 = " and ham";line = var1 || var2 || var3 || var4;lineCat = cat( of var:);lineCats = cats( of var:);lineCatt = catt( of var:);lineCatq = catq(' ', of var:);lineCatx = catx(' ', of var:);put 'line ' line;put 'lineCat= ' lineCat;put 'lineCats= ' lineCats;put 'lineCatt= ' lineCatt;put 'lineCatq= ' lineCatq;put 'lineCatx= ' lineCatx;

run;

Page 14: Functioning at a Higher Level: to Improve Your Codetorsas.ca/attachments/File/20180914/Functions Peter Eberhardt 201… · 2018-09-21 3 Copyright 2018 Peter Eberhardt, Fernwood Consulting

2018-09-21

14

Copyright 2018 Peter Eberhardt, Fernwood Consulting Group Inc. All rights Reserved.

Functioning at a Higher Level: Using SAS® Functions to Improve Your Code

Characterdata catInHat;var1 = "I do "; var2 = "not eat";var3 = "green eggs"; var4 = " and ham";

line I do not eatgreen eggs and hamlineCat= I do not eatgreen eggs and hamlineCats= I donot eatgreen eggsand hamlineCatt= I donot eatgreen eggs and hamlineCatq= "I do " "not eat" "green eggs" " and ham"lineCatx= I do not eat green eggs and ham

Copyright 2018 Peter Eberhardt, Fernwood Consulting Group Inc. All rights Reserved.

Functioning at a Higher Level: Using SAS® Functions to Improve Your Code

Characterdata fullName;

length lname fname $25.;fname = "Peter"; lname = "Eberhardt";fn1 = fname || lname;fn2 = trim(fname) || trim(lname);fn3 = trim(fname) || ' ' || trim(lname);fn4 = catq('DS',' ', fname, lname);put (fn1-fn4) (=/);

run;

fn1=Peter Eberhardtfn2=PeterEberhardtfn3=Peter Eberhardtfn4=Peter Eberhardt

Page 15: Functioning at a Higher Level: to Improve Your Codetorsas.ca/attachments/File/20180914/Functions Peter Eberhardt 201… · 2018-09-21 3 Copyright 2018 Peter Eberhardt, Fernwood Consulting

2018-09-21

15

Copyright 2018 Peter Eberhardt, Fernwood Consulting Group Inc. All rights Reserved.

Functioning at a Higher Level: Using SAS® Functions to Improve Your Code

Character• CAT family

– See TASS presentation slides by Mike Zdeb in2012

• Using CAT() and FIND()

Copyright 2018 Peter Eberhardt, Fernwood Consulting Group Inc. All rights Reserved.

Functioning at a Higher Level: Using SAS® Functions to Improve Your Code

Character

• String Search– PRX family

• Ken Borowiak

Page 16: Functioning at a Higher Level: to Improve Your Codetorsas.ca/attachments/File/20180914/Functions Peter Eberhardt 201… · 2018-09-21 3 Copyright 2018 Peter Eberhardt, Fernwood Consulting

2018-09-21

16

Copyright 2018 Peter Eberhardt, Fernwood Consulting Group Inc. All rights Reserved.

Functioning at a Higher Level: Using SAS® Functions to Improve Your Code

Questions

Peter [email protected]: @rkinRobin

We Chat: peterOnDroid

Lucheng [email protected]

Copyright 2018 Peter Eberhardt, Fernwood Consulting Group Inc. All rights Reserved.

Functioning at a Higher Level: Using SAS® Functions to Improve Your Code

Questions

Peter [email protected]: @rkinRobin

We Chat: peterOnDroid

Lucheng [email protected]

Page 17: Functioning at a Higher Level: to Improve Your Codetorsas.ca/attachments/File/20180914/Functions Peter Eberhardt 201… · 2018-09-21 3 Copyright 2018 Peter Eberhardt, Fernwood Consulting

2018-09-21

17

Copyright 2018 Peter Eberhardt, Fernwood Consulting Group Inc. All rights Reserved.

Copyright 2018 Peter Eberhardt, Fernwood Consulting Group Inc. All rights Reserved.

Functioning at a Higher Level: Using SAS® Functions to Improve Your Code

Questions

Peter [email protected]: @rkinRobin

We Chat: peterOnDroid

Lucheng [email protected]