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

1 / 51
About This Presentation
Title:

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

Description:

What s wrong NOW?! An introduction to debugging SAS programs for beginners Martha Cox Cancer Outcomes Research Program CDHA / Dalhousie * Let s say we want to ... – PowerPoint PPT presentation

Number of Views:156
Avg rating:3.0/5.0
Slides: 52
Provided by: shrugonli
Category:

less

Transcript and Presenter's Notes

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


1
Whats wrong NOW?!An introduction to debugging
SAS programs for beginnersMartha CoxCancer
Outcomes Research ProgramCDHA / Dalhousie
2
Whats wrong NOW?!
  • Types of Bugs
  • Bug Droppings
  • Bug Repellant
  • Bug Killing General Approach
  • Bug Killing Specific Bugs

3
Types of Bugs
  • Syntax / Semantic

4
Types of Bugs
  • Syntax / Semantic
  • Execution-Time

5
Types of Bugs
  • Syntax / Semantic
  • Execution-Time
  • Data

6
Types of Bugs
  • Syntax / Semantic
  • Execution-Time
  • Data
  • Logic

7
Bug Droppings
  • ERROR
  • WARNING
  • NOTE
  • no messages

8
Bug Repellant
  • Make your program easy to read
  • 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

9
Make your program easy to read
  • data hyst
  • set hospital(keepyear pcode1-pcode10)
  • array pcodes pcode1-pcode10
  • length code 3 year 3
  • do i 1 to 10
  • if pcodesi'689 then do
  • code substr(pcodesi,1,3)
  • output end end keep year code
  • title1 'Number of Selected Hysterectomy
    Procedures by Year'
  • proc freq noprint tables yearcode/list
    outtemp
  • run

10
  • / Create a data set containing hysterectomy
    codes. /
  • data hyst
  • set hospital
  • (keepyear pcode1-pcode10)
  • array pcodes pcode1-pcode10
  • length code 3
  • year 3
  • do i 1 to 10
  • if pcodesi '689
  • then do
  • code substr(pcodesi,1,3)
  • output
  • end if
  • end do i
  • keep year code
  • run
  • title1 'Number of Selected Hysterectomy
    Procedures by Year'

11
Bug Repellant
  • Make your program easy to read
  • Dont destroy your data

12
Dont Destroy Your Dataaccessreadonly
  • libname survey 'C\My Documents\Survey\'
    accessreadonly
  • libname indata 'C\Thesis\Data\'
    accessreadonly
  • libname outdata 'C\Thesis\Data\'

13
Dont Destroy Your DataUse OUT on PROC SORT
  • proc sort datathesis.hospital
  • (keeppatient addate)
  • by patient
  • run
  • proc sort datathesis.hospital
  • (keeppatient addate)
  • outpatlist
  • by patient
  • run

14
Dont Destroy Your Dataoptions DATASTMTCHK
  • 7 options datastmtchknone
  • 8 data adults
  • 9 set survey.adult
  • 10 if age gt 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.

15
Dont Destroy Your Dataoptions DATASTMTCHK
  • 12 options datastmtchkcorekeywords
  • 13 data adults
  • NOTE SCL source line.
  • 14 set survey.adult
  • ---
  • 56
  • ERROR 56-185 SET is not allowed in the DATA
    statement when option DATASTMTCHKCOREKEYWORDS.
    Check for a missing semicolon in the DATA
    statement, or use DATASTMTCHKNONE.
  • 15 if age gt 65
  • 16 run

16
Dont Destroy Your Dataoptions MERGENOBY
  • 13 data valid not_valid
  • 14 merge TEST_DATA(keepID ina)
  • 15 master.master02(inb)
  • 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.

17
Dont Destroy Your Dataoptions MERGENOBY
  • 12 options mergenobyerror
  • 13 data valid not_valid
  • 14 merge TEST_DATA(keepID ina)
  • 16 master.master02(inb)
  • 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.

18
Bug Repellant
  • Make your program ( your log) easy to read
  • Dont destroy your data
  • Know your data

19
Know 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).
  • 2312
  • NOTE Invalid numeric data, 'M' , at line 23
    column 12.

20
Bug Repellant
  • Make your program ( your log) easy to read
  • Dont destroy your data
  • Know your data
  • Do a syntax check

21
Do a syntax check
  • options OBS0 NOREPLACE

22
Bug Repellant
  • Make your program ( your log) easy to read
  • Dont destroy your data
  • Know your data
  • Do a syntax check

23
Bug Killing
  • Read the LOG!

24
Bug Killing
  • Start at the top deal with one mess at a time.

25
Bug Killing
  • Start at the top deal with one mess at a time.
  • Use interactive SAS, if possible

26
Bug Killing
  • Start at the top deal with one mess at a time.
  • Use interactive SAS, if possible
  • Using ENDSAS

27
Using ENDSAS
  • lt lots of SAS statements gt
  • proc freq dataworkhrs
  • tables hrs / nofreq nocum norow
  • run
  • endsas
  • lt lots more SAS statements gt

28
Bug Killing
  • Start at the top deal with one mess at a time.
  • Use interactive SAS, if possible
  • Using ENDSAS
  • Using OPTIONS ERRORABEND

29
Using ERRORABEND
  • 1 options errorabend
  • 5 / Number of Hours Worked Per Week /
  • 6 data workhrs(dropb151)
  • 7 set survey.newadult
  • 8 (keepwrk)
  • 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.

30
Bug Killing
  • Look for horses, not zebras.

31
Bug Killing
  • Read the LOG!
  • Start at the top deal with one mess at a time.
  • Look for horses, not zebras.

32
Bug Killing
  • Some Specific Bugs

33
WARNING or ERRORmisspelled keywords
  • 33 data adult
  • NOTE SCL source line.
  • 34 st aborig.newadult
  • --
  • 1
  • WARNING 1-322 Assuming the symbol SET was
    misspelled as st.
  • 35 run

34
ERROR Invalid option name, parameter, or
statement.
  • Possible Causes
  • missing semicolon
  • misspelled keyword
  • unmatched quotation mark
  • unclosed comment
  • a DATA step statement in a PROC step
  • a valid option used in the wrong place

35
ERROR Invalid option name, parameter, or
statement.
  • 3 data males(ropgender)
  • ---
  • 22
  • ERROR 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.

36
ERROR Invalid option name, parameter, or
statement.
  • 8 proc print datasurvey.adult
  • NOTE SCL source line.
  • 9 set gender
  • ---
  • 180
  • ERROR 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.

37
ERROR Invalid option name, parameter, or
statement.
  • 11 proc print datasurvey.adult
  • NOTE SCL source line.
  • 12 var gender
  • --- --
  • 22 202
  • ERROR 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.

38
ERROR Invalid option name, parameter, or
statement.
  • 14 Print a list of Adult ages.
  • 15 proc print datasurvey.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

39
WARNING unbalanced quotes
  • 90 proc print datahospital(obs10)
  • 91 title1 "Sample of Hospital Data
  • 92 run
  • 93
  • lt more SAS statements gt
  • 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.

40
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. y6 z. z20
  • NOTE 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 5013 1 at 537

41
NOTE Numeric to character 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).
  • 11219
  • NOTE Character values have been converted to
    numeric
  • values at the places given by
    (Line)(Column).
  • 11319

42
NOTE Numeric to character 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.

43
NOTE Variable is uninitialized.
  • Possible Causes
  • misspelling the variable name
  • using a variable that was dropped in a previous
    step
  • using the wrong data set
  • using a variable before it is created

44
NOTE Variable is uninitialized.
  • 140 data males
  • 141 set survey.adult
  • 142 (keepid community)
  • 143 if gender2
  • 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.

45
No Messages Character values are being truncated.
  • data groups
  • set survey.adult
  • if age lt 20 then AgeGroup 'Teen'
  • else if age gt 65 then AgeGroup 'Senior'
  • else AgeGroup 'Adult'
  • run
  • proc freq datagroups
  • tables agegroup / nocum
  • run

46
No Messages Character values are being truncated.
  • The FREQ Procedure
  • Age
  • Group Frequency Percent
  • ------------------------------
  • Adul 388 78.70
  • Seni 17 3.45
  • Teen 88 17.85

47
No Messages Character values are being truncated.
  • data groups
  • set survey.adult
  • attrib AgeGroup length6 label'Age Group'
  • if age lt 20 then AgeGroup 'Teen'
  • else if age gt 65 then AgeGroup 'Senior'
  • else AgeGroup 'Adult'
  • run
  • proc freq datagroups
  • tables agegroup / nocum
  • run

48
No Messages Character values are being truncated.
  • The FREQ Procedure
  • Age
  • Group Frequency Percent
  • -------------------------------
  • Adult 388 78.70
  • Senior 17 3.45
  • Teen 88 17.85

49
Questions?
50
References
  • 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

51
Resources
  • Online documentation http//v9doc.sas.com/sa
    sdoc/
  • Online conference proceedings
  • SUGI http//support.sas.com/usergroups/
    sugi/proceedings/index.html
  • NESUG http//www.nesug.org/
Write a Comment
User Comments (0)
About PowerShow.com