what’s wrong now?! an introduction to debugging sas programs for beginners martha cox cancer...

51
What’s wrong What’s wrong NOW NOW ?! ?! An introduction to An introduction to debugging SAS programs debugging SAS programs for beginners for beginners Martha Cox Martha Cox Cancer Outcomes Research Program Cancer Outcomes Research Program CDHA / Dalhousie CDHA / Dalhousie

Upload: hugo-hunt

Post on 17-Dec-2015

230 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

What’s wrong What’s wrong NOWNOW?!?!An introduction to debugging An introduction to debugging SAS programs for beginnersSAS programs for beginners

Martha CoxMartha CoxCancer Outcomes Research ProgramCancer Outcomes Research Program

CDHA / DalhousieCDHA / Dalhousie

Page 2: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

What’s wrong What’s wrong NOWNOW?!?!

Types of BugsBug DroppingsBug RepellantBug Killing – General ApproachBug Killing – Specific Bugs

Page 3: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

Types of BugsTypes of Bugs

Syntax / Semantic

Page 4: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

Types of BugsTypes of Bugs

Syntax / SemanticExecution-Time

Page 5: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

Types of BugsTypes of Bugs

Syntax / SemanticExecution-TimeData

Page 6: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

Types of BugsTypes of Bugs

Syntax / SemanticExecution-TimeDataLogic

Page 7: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

Bug DroppingsBug Droppings

ERRORWARNINGNOTEno messages

Page 8: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

Bug RepellantBug Repellant

Indent – within a step or a DO block One Semicolon per Line (or less!) Every step gets a RUN Every PROC gets a DATA= Comments, comments, comments

1. Make your program easy to read

Page 9: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

Make your program easy to readMake your program easy to read

data hyst;set hospital(keep=year pcode1-pcode10); array pcodes{*} pcode1-pcode10;length code $3 year 3;do i = 1 to 10;if pcodes{i}=:'689‘ then do;code = substr(pcodes{i},1,3);output; end; end; keep year code;title1 'Number of Selected Hysterectomy Procedures by Year';proc freq noprint; tables year*code/list out=temp;run;

Page 10: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

/* Create a data set containing hysterectomy codes. */data hyst; set hospital (keep=year pcode1-pcode10); array pcodes{*} pcode1-pcode10; length code $3 year 3 ; do i = 1 to 10; if pcodes{i} =: '689‘ then do; code = substr(pcodes{i},1,3); output; end; ** if **; end; ** do i **; keep year code;run;

title1 'Number of Selected Hysterectomy Procedures by Year';proc freq data=hyst noprint; tables year*code/list out=temp;run;

Page 11: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

Bug RepellantBug Repellant

1. Make your program easy to read

2. Don’t destroy your data

Page 12: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

Don’t Destroy Your Data:Don’t Destroy Your Data:access=readonlyaccess=readonly

libname survey 'C:\My Documents\Survey\' access=readonly;

libname indata 'C:\Thesis\Data\' access=readonly;

libname outdata 'C:\Thesis\Data\' ;

Page 13: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

Don’t Destroy Your Data:Don’t Destroy Your Data:Use OUT= on PROC SORTUse OUT= on PROC SORTproc sort data=thesis.hospital (keep=patient addate); by patient;run;

proc sort data=thesis.hospital (keep=patient addate) out=patlist ; by patient;run;

Page 14: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

Don’t Destroy Your Data:Don’t Destroy Your Data:options DATASTMTCHK=options DATASTMTCHK=7 options datastmtchk=none;8 data adults9 set survey.adult;10 if age > 65;11 run;

NOTE: Variable age is uninitialized.NOTE: The SAS System stopped processing this step

because of errors.WARNING: The data set WORK.ADULTS may be

incomplete. When this step was stopped there were 0 observations and 1 variables.

Page 15: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

Don’t Destroy Your Data:Don’t Destroy Your Data:options DATASTMTCHK=options DATASTMTCHK=12 options datastmtchk=corekeywords;13 data adultsNOTE: SCL source line.14 set survey.adult; --- 56ERROR 56-185: SET is not allowed in the DATA

statement when option DATASTMTCHK=COREKEYWORDS. Check for a missing semicolon in the DATA statement, or use DATASTMTCHK=NONE.

15 if age > 65;16 run;

Page 16: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

Don’t Destroy Your Data:Don’t Destroy Your Data:options MERGENOBY=options MERGENOBY= 13 data valid not_valid; 14 merge TEST_DATA(keep=ID in=a) 15 master.master02(in=b); 18 if a and b then output valid; 19 else if not b then output not_valid; 20 run;

NOTE: There were 899 observations read from the data set TEST_DATA.

NOTE: There were 1138309 observations read from the data set MASTER.MASTER02.

NOTE: The data set WORK.VALID has 899 observations and 9 variables.

NOTE: The data set WORK.NOT_VALID has 0 observations and 9 variables.

Page 17: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

Don’t Destroy Your Data:Don’t Destroy Your Data:options MERGENOBY=options MERGENOBY=12 options mergenoby=error;13 data valid not_valid;14 merge TEST_DATA(keep=ID in=a)16 master.master02(in=b);17 if a and b then output valid;18 else if not b then output not_valid;19 run;

+ERROR: No BY statement was specified for a MERGE statement.

NOTE: The SAS System stopped processing this step because of errors.

Page 18: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

Bug RepellantBug Repellant

1. Make your program (& your log) easy to read

2. Don’t destroy your data

3. Know your data

Page 19: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

Know your dataKnow your data

21 data males;22 set survey.adult;23 if gender = 'M';24 run;

NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).

23:12NOTE: Invalid numeric data, 'M' , at line 23

column 12.

Page 20: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

Bug RepellantBug Repellant

1. Make your program (& your log) easy to read

2. Don’t destroy your data

3. Know your data

4. Do a syntax check

Page 21: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

Do a syntax checkDo a syntax check

options OBS=0 NOREPLACE;

Page 22: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

Bug RepellantBug Repellant

1. Make your program (& your log) easy to read

2. Don’t destroy your data

3. Know your data

4. Do a syntax check

Page 23: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

Bug KillingBug Killing

1. Read the LOG!

Page 24: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

2. Start at the top & deal with one mess at a time.

Bug KillingBug Killing

Page 25: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

• Use interactive SAS, if possible

2. Start at the top & deal with one mess at a time.

Bug KillingBug Killing

Page 26: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

• Use interactive SAS, if possible• Using ENDSAS

2. Start at the top & deal with one mess at a time.

Bug KillingBug Killing

Page 27: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

Using ENDSASUsing ENDSAS< lots of SAS statements >

proc freq data=workhrs; tables hrs / nofreq nocum norow;run;

endsas;

< lots more SAS statements >

Page 28: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

• Use interactive SAS, if possible• Using ENDSAS• Using OPTIONS

ERRORABEND

2. Start at the top & deal with one mess at a time.

Bug KillingBug Killing

Page 29: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

Using ERRORABENDUsing ERRORABEND1 options errorabend; 5 /* Number of Hours Worked Per Week */6 data workhrs(drop=b151);7 set survey.newadult8 (keep=wrk);ERROR: The variable wrk in the DROP, KEEP, or RENAME

list has never been referenced.9 length hrs 8;10 hrs = input(b151, 8.);11 run;

ERROR: SAS ended due to errors. You specified: OPTIONS ERRORABEND;.

Page 30: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

3. Look for horses, not zebras.

Bug KillingBug Killing

Page 31: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

1. Read the LOG!

2. Start at the top & deal with one mess at a time.

3. Look for horses, not zebras.

Bug KillingBug Killing

Page 32: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

Some Specific Bugs

Bug KillingBug Killing

Page 33: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

WARNING or ERROR:WARNING or ERROR:misspelled keywordsmisspelled keywords

33 data adult;NOTE: SCL source line.34 st aborig.newadult; -- 1WARNING 1-322: Assuming the symbol SET was

misspelled as st.

35 run;

Page 34: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

ERROR: Invalid option name, ERROR: Invalid option name, parameter, or statement.parameter, or statement.

Possible Causes:missing semicolonmisspelled keywordunmatched quotation markunclosed commenta DATA step statement in a PROC stepa valid option used in the wrong place

Page 35: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

ERROR: Invalid option name, ERROR: Invalid option name, parameter, or statement.parameter, or statement.

3 data males(rop=gender); --- 22ERROR 22-7: Invalid option name ROP.

4 set survey.adult;5 if gender = 2;6 run;

NOTE: The SAS System stopped processing this step because of errors.

Page 36: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

ERROR: Invalid option name, ERROR: Invalid option name, parameter, or statement.parameter, or statement.

8 proc print data=survey.adult;NOTE: SCL source line.9 set gender; --- 180ERROR 180-322: Statement is not valid or it is

used out of proper order.10 run;

NOTE: The SAS System stopped processing this step because of errors.

Page 37: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

ERROR: Invalid option name, ERROR: Invalid option name, parameter, or statement.parameter, or statement.11 proc print data=survey.adultNOTE: SCL source line.12 var gender; --- -- 22 202ERROR 22-322: Syntax error, expecting one of the

following: ;, (, DATA, DOUBLE, HEADING, LABEL, N, NOOBS, OBS, ROUND, ROWS, SPLIT, STYLE, UNIFORM, WIDTH.

ERROR 202-322: The option or parameter is not recognized and will be ignored.

13 run;

NOTE: The SAS System stopped processing this step because of errors.

Page 38: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

ERROR: Invalid option name, ERROR: Invalid option name, parameter, or statement.parameter, or statement.

14 * Print a list of Adult ages.15 proc print data=survey.newadult;NOTE: SCL source line.16 var b4; --- 180

ERROR 180-322: Statement is not valid or it is used out of proper order.

17 run;

Page 39: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

WARNING: unbalanced quotesWARNING: unbalanced quotes

90 proc print data=hospital(obs=10); 91 title1 "Sample of Hospital Data; 92 run; 93 < more SAS statements > 91 title1 "Sample of Hospital Data;+ _____________________+ _____________________+ _____________________ 32+ 32+ 32 WARNING 32-169: The quoted string currently being

processed has become more than 262 characters long. You may have unbalanced quotation marks.

Page 40: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

NOTE: Missing values generated.NOTE: Missing values generated.48 data test;49 a = 2; b = 4; c = .;50 x = a + b + c;51 y = sum(a,b,c);52 a = .; b = .;53 z = sum(a,b,c);54 z2 = sum(a,b,c,0);55 put x= y= z= z2= ;56 run;

x=. y=6 z=. z2=0NOTE: Missing values were generated as a result of

performing an operation on missing values. Each place is given by: (Number of times) at (Line):(Column).

1 at 50:13 1 at 53:7

Page 41: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

NOTE: Numeric to character NOTE: Numeric to character conversion (or vice versa)conversion (or vice versa)109 data adult2;110 length sexage $5 household 8 ;111 set adult;112 sexage = sex || age ;113 household = sum(kids, adults, 1);114 run;

NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column). 112:19NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column). 113:19

Page 42: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

NOTE: Numeric to character NOTE: Numeric to character conversion (or vice versa)conversion (or vice versa)122 data adult3;123 length sexage $5 household 8 ;124 set adult;125 sexage = sex || put(age, 3. -L);126 kidsnum = input(kids, 8.);127 household = sum(kidsnum, adults, 1);128 run;

NOTE: There were 493 observations read from the data set WORK.ADULT.

NOTE: The data set WORK.ADULT3 has 493 observations and 8 variables.

Page 43: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

NOTE: Variable is uninitialized.NOTE: Variable is uninitialized.

Possible Causes:misspelling the variable nameusing a variable that was dropped in a

previous stepusing the wrong data setusing a variable before it is created

Page 44: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

NOTE: Variable is uninitialized.NOTE: Variable is uninitialized.

140 data males;141 set survey.adult142 (keep=id community);143 if gender=2;144 run;

NOTE: Variable gender is uninitialized.NOTE: There were 493 observations read from the

data set SURVEY.ADULT.NOTE: The data set WORK.MALES has 0 observations

and 3 variables.

Page 45: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

No Messages: Character values are No Messages: Character values are being truncated.being truncated.

data groups; set survey.adult; if age < 20 then AgeGroup = 'Teen'; else if age > 65 then AgeGroup = 'Senior'; else AgeGroup = 'Adult';run;

proc freq data=groups; tables agegroup / nocum;run;

Page 46: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

No Messages: Character values are No Messages: Character values are being truncated.being truncated.

The FREQ Procedure

AgeGroup Frequency Percent------------------------------Adul 388 78.70Seni 17 3.45Teen 88 17.85

Page 47: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

No Messages: Character values are No Messages: Character values are being truncated.being truncated.

data groups;

set survey.adult;

attrib AgeGroup length=$6 label='Age Group';

if age < 20 then AgeGroup = 'Teen';

else if age > 65 then AgeGroup = 'Senior';

else AgeGroup = 'Adult';

run;

proc freq data=groups;

tables agegroup / nocum;

run;

Page 48: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

No Messages: Character values are No Messages: Character values are being truncated.being truncated.

The FREQ Procedure

Age

Group Frequency Percent

-------------------------------

Adult 388 78.70

Senior 17 3.45

Teen 88 17.85

Page 49: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

Questions?Questions?

Page 50: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

ReferencesReferences Delwiche, Lora D., and Susan J. Slaughter, The Little

SAS Book: a primer, Third Edition, SAS Institute, Cary, NC, 2003

Staum, Roger, To Err is Human; to Debug Divine, NESUG 15 Conference Proceedings

Howard, Neil, How SAS Thinks, or Why the DATA Step Does What It Does, SUGI 29 Conference Proceedings

Knapp, Peter, Debugging 101, NESUG 15 Conference Proceedings

Rhodes, Dianne Louise, So You Want to Write a SUGI Paper? That Paper About Writing a Paper, SUGI 29 Conference Proceedings

Page 51: What’s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie

ResourcesResources

Online documentation http://v9doc.sas.com/sasdoc/

Online conference proceedings– SUGI:

http://support.sas.com/usergroups/ sugi/proceedings/index.html

– NESUG: http://www.nesug.org/