Examples from SAS Functions by Example Ron Cody

Slides:



Advertisements
Similar presentations
Dates and Times SAS Date, Time and Date- Time Formats.
Advertisements

Chapter 13 Transforming Data with SAS Functions
Statistical Methods Lynne Stokes Department of Statistical Science Lecture 7: Introduction to SAS Programming Language.
Outline Proc Report Tricks Kelley Weston. Outline Examples 1.Text that spans columnsText that spans columns 2.Patient-level detail in the titlesPatient-level.
Statistics in Science  Introducing SAS ® software Acknowlegements to David Williams Caroline Brophy.
I OWA S TATE U NIVERSITY Department of Animal Science Modifying and Combing SAS Data Sets (Chapter in the 6 Little SAS Book) Animal Science 500 Lecture.
PROC_CODEBOOK: An Automated, General Purpose Codebook Generator
Chapter 18: Modifying SAS Data Sets and Tracking Changes 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Understanding SAS Data Step Processing Alan C. Elliott stattutorials.com.
Into to SAS ®. 2 List the components of a SAS program. Open an existing SAS program and run it. Objectives.
Creating SAS® Data Sets
Data Cleaning 101 Ron Cody, Ed.D Robert Wood Johnson Medical School Piscataway, NJ.
Welcome to SAS…Session..!. What is SAS..! A Complete programming language with report formatting with statistical and mathematical capabilities.
“SAS macros are just text substitution!” “ARRRRGGHHH!!!”
JavaScript Events and Event Handlers 1 An event is an action that occurs within a Web browser or Web document. An event handler is a statement that tells.
Chapter 10:Processing Macro Variables at Execution Time 1 STAT 541 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Writing Maintainable Code with ‘Style’ Allan Page Senior Marketing Analyst Canadian Tire Bank.
11 Chapter 3: Reading and Processing Data 3.1 Processing SAS Data Sets 3.2 Processing External Files.
1 Experimental Statistics - week 4 Chapter 8: 1-factor ANOVA models Using SAS.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina Chapter 17 supplement: Review of Formatting Data STAT 541.
Prepared by: Luigi Muro – Consultant
1 Experimental Statistics - week 2 Review: 2-sample t-tests paired t-tests Thursday: Meet in 15 Clements!! Bring Cody and Smith book.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
SAS Macro: Some Tips for Debugging Stat St. Paul’s Hospital April 2, 2007.
SAS Efficiency Techniques and Methods By Kelley Weston Sr. Statistical Programmer Quintiles.
EPIB 698C Lecture 2 Notes Instructor: Raul Cruz 2/14/11 1.
Macro Overview Mihaela Simion. Macro Facility Overview Definition : The SAS Macro Facility is a tool within base SAS software that contains the essential.
Lesson 2 Topic - Reading in data Chapter 2 (Little SAS Book)
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Access: Queries Ad-hoc Reporting Chapter T. Access Queries Queries Access Properties Sorting Selection Criteria Calculations.
1 SAS 1-liners SAS Coding Efficiencies. 2 Overview Less is more Always aim for robust, reusable and efficient code Coding efficiency versus processing.
SQL Chapter Two. Overview Basic Structure Verifying Statements Specifying Columns Specifying Rows.
Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004.
Creating and Using Custom Formats for Data Manipulation and Summarization Presented by John Schmitz, Ph.D. Schmitz Analytic Solutions, LLC Certified Advanced.
Chapter 4 concerns various SAS procedures (PROCs). Every PROC operates on: –the most recently created dataset –all the observations –all the appropriate.
Chapter 17: Formatting Data 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
YET ANOTHER TIPS, TRICKS, TRAPS, TECHNIQUES PRESENTATION: A Random Selection of What I Learned From 15+ Years of SAS Programming John Pirnat Kaiser Permanente.
Chapter 1: Overview of SAS System Basic Concepts of SAS System.
TASS Meeting Setting GuessingRows when Importing Excel Files September 19th, 2008 Setting GuessingRows when importing Excel Files Dr. Arthur Tabachneck,
Chapter 7: Macros in SAS  Macros provide for more flexible programming in SAS  Macros make SAS more “object-oriented”, like R 1 © Fall 2011 John Grego.
Time Series Data Processes by Tai Yu April 15, 2013.
SAS for Data Management and Analysis
Computing with SAS Software A SAS program consists of SAS statements. 1. The DATA step consists of SAS statements that define your data and create a SAS.
Chapter 6 Concatenating SAS Data Sets and Creating Summary Reports Xiaogang Su Department of Statistics University of Central Florida.
Chapter 17 Supplement: Alternatives to IF-THEN/ELSE Processing STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South.
LISA SHORT COURSE SERIES: INTRODUCTION TO SAS UNIVERSITY William DeShong Fall 2015.
Chapter 21: Controlling Data Storage Space 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.
Lesson 2 Topic - Reading in data Programs 1 and 2 in course notes –Chapter 2 (Little SAS Book)
Chapter 14: Combining Data Vertically 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
SAS Programming Training Instructor:Greg Grandits TA: Textbooks:The Little SAS Book, 5th Edition Applied Statistics and the SAS Programming Language, 5.
Based on Learning SAS by Example: A Programmer’s Guide Chapters 1 & 2
Online Programming| Online Training| Real Time Projects | Certifications |Online Classes| Corporate Training |Jobs| CONTACT US: STANSYS SOFTWARE SOLUTIONS.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapter 25 By Tasha Chapman, Oregon Health Authority.
Basic SAS Functions in Version 8.2 Kim Michalski Office of the Actuary Rick Andrews Office of Research, Development, and Information.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapters 5 & 6 By Ravi Mandal.
Loops BIS1523 – Lecture 10.
Using SAS® Dates and Times
Two “identical” programs
Chapter 3: Working With Your Data
Chapter 7: Macros in SAS Macros provide for more flexible programming in SAS Macros make SAS more “object-oriented”, like R Not a strong suit of text ©
Topics Introduction to File Input and Output
SAS Essentials How SAS Thinks
Creating Macro Variables in the DATA Step
Retrieving Macro Variables in the DATA Step
3 Iterative Processing.
Topics Introduction to File Input and Output
Presentation transcript:

Examples from SAS Functions by Example Ron Cody Herman Lo Technical Analyst, RBC Capital Markets

Agenda Book Structure Examples from the Book The Verdict Character Functions (CATS, CATX) Date and Time Functions (INTCK, INTNX) Descriptive Stats (IQR, SMALLEST/LARGEST) Special Functions (INPUT, PUT) Macro Functions (CALL SYMPUT, CALL SYMPUTX) The Verdict

Book Structure TOC – List of Chapters List of Programs At the beginning of each chapter List of Functions Inside each chapter For each function Purpose Syntax Examples/Anticipated Outputs A Sample Program At the back Index (Alphabetical)

Character Functions CATS(string-1, string-2 …) Joins strings, stripping both leading and trailing blanks CATX(string-1, string-2 …) Joins strings, stripping both leading and trailing blanks, and add a space in between them Example A = “Star” B = “Wars” CATS(A, B) = “StarWars” CATX(A, B) = “Star Wars” (See Pg. 57-58)

Date and Time Functions INTCK(‘interval<Multiple><.shift>’, date1,date2) Returns number of Intervals between date1 and date2 Date1 and Date2 can be date, time, datetime values date: ’01JUN2000’d time: ’9:15:09’T datetime:’01JUN2000:9:15:09’DT Interval = The unit of the interval Interval(date) = DAY, WEEK, WEEKDAY, … Interval(time) = SECOND, MINUTE, HOUR Interval(datetime) = DTDAY, DTWEEK, DTWEEKDAY, …

Date and Time Functions INTCK(‘interval<Multiple><.shift>’, date1,date2) Multiple (optional) = Multiple of interval unit DAY50 = 50-DAY intervals Shift (optional) =starting point of interval Meaning of Shift depends on the Interval Interval=YEAR,SEMIYEAR,QTR,MONTH  Shift = MONTH YEAR4.11 = 4-YEAR intervals starting on November Interval=SEMIMONTH,HOUR,MINUTE,SECOND  Shift=Interval but only Multi-intervals can be shifted (Multiple must be specified) HOUR8.6=8-HOUR intervals starting at 6AM (6AM,2PM,10PM) See Pg. 186

Date and Time Functions INTCK(‘interval<Multiple><.shift>’, date1,date2) Examples INTCK(‘YEAR.7’,’05MAY2002’d, ’15JUL2002’d)=1 INTCK(‘WEEK’,’01JAN1960’d, ’04JAN1960’d)=1 01JAN1960 is a Sunday, so the week counter is triggered because default WEEK starting point is Sunday. CAREFUL: Results may surprise you (off-by-one problems). Watch where the starting point is.

Date and Time Functions INTNX(‘interval’,start-date,increment<,’alignment’>) Interval = same as INTCK Start-date = starting date increment = # of intervals between start date and output date alignment (optional) = BEGINNING, MIDDLE, END of Interval default is BEGINNING Example INTNX(‘WEEK’,’01JAN1960’d,1,’MIDDLE’)=‘06JAN,1960’d

Descriptive Stats IQR(<of> numeric-values) Computes the interquartile range (25th percentile and 75th percentile) in a list of values Use of to define a list of values Examples X1=1,X2=2,X3=3,X4=. IQR(of X1-X4)=2 IQR(X1,X2,X3,X4)=2

Descriptive Stats SMALLEST/LARGEST(N,<of> numeric-values) Finds the Nth smallest or largest number in the list of values Returns missing value and writes an error in log if N is larger than number of values Returns missing value and does not write an error in log if N is larger than the number of missing values Examples X1=1,X2=2,X3=3,X4=.,X5=. SMALLEST(3,X2,X3,X4)=. LARGEST(1,X1,X2,X3,X4)=3

Special Functions INPUT(value, informat) PUT(value, format) Performs character-to-numeric conversion. Value is a character variable Informat is a SAS informat, defines the input format of Value PUT(value, format) Performs numeric-to-character conversion. format is a SAS format, defines the output format Program Example from Pg. 302

Listing of Data Set TABLE Special Functions ***Primary functions: PUT, INPUT; PROC FORMAT; VALUE ITEM 1=‘APPLE’ 2=‘PEAR’ 3=‘GRAPE’ OTHER=‘UNKNOWN’; VALUE $COST ‘A’ – ‘C’ = ’44.45’ ‘D’ = ‘125.’ OTHER = ‘ ‘; RUN; DATA TABLE; INPUT ITEM_NO CODE $ @@; ITEM_NAME = PUT(ITEM_NO, ITEM.); AMOUNT = INPUT(PUT(CODE, $COST.), 9.); DATALINES; 1 B 2 D 3 X 4 C ; PROC PRINT DATA=TABLE NOOBS; TITLE “Listing of Data Set TABLE”; Listing of Data Set TABLE ITEM_NO CODE ITEM_ NAME AMOUNT 1 B APPLE 44.45 2 D PEAR 125.00 3 X GRAPE . 4 C UNKNOWN

Macro Functions CALL SYMPUT(macro-var, character-value) CALL SYMPUTX(macro-var, character-value) Assigns a value to a macro variable during execution of DATA step CALL SYMPUTX: Blanks are not removed from value before assignment to macro variable CALL SYMPUTX: Leading & trailing blanks are removed from value before assignment to macro variable Equivalent to %let statement in macro language

Macro Functions Example DATA TEST; INPUT STRING $CHAR10. ; CALL SYMPUT(“StringWithBlanks”,STRING); CALL SYMPUTX(“StringWithoutBlanks”,STRING); DATALINES; ABC ; DATA _NULL_; WITHBLANKS = “----” || “&StringWithBlanks” || “----”; WITHOUTBLANKS = “----” || “&StringWithoutBlanks” || “----”; PUT “Value of StringWithBlanks is ” WITHBLANKS; PUT “Value of StringWithBlanks is ” WITHOUTBLANKS; RUN; SAS LOG Value of WITHBLANKS is ---- ABC ---- Value of WITHOUTBLANKS is ----ABC----

File I/O Functions File I/O functions are used to obtain information about SAS data sets dsid = OPEN(‘data-set-name’<,’mode’>) Opens a SAS data set with the name data-set-name and return a data set ID dsid A Data set ID is necessary for File I/O Functions If data set cannot be opened, OPEN returns a 0 EXIST(dsid) Returns 1 if Data set exists and a 0 otherwise CLOSE(dsid) Closes SAS data set after it has been opened by the OPEN function

File I/O Functions ATTRC(dsid,’attribute’) Returns the character value of a character type attribute i.e. ATTRC(DSID, ‘sortedby’) = name of the BY variable. Empty if not sorted. ATTRN(dsid,’attribute’) Returns the numeric value of a character type attribute

Listing of Data Set TABLE File I/O Functions %MACRO NOBS(DSN); IF EXIST(“&DSN”) THEN DO; DSID = OPEN(“&DSN”); NOBS=ATTRN(DSID,”ANY”); NOBS=ATTRN(DSID,”NLOBS”); NOBS=ATTRN(DSID,”NVARS”); END; ELSE NOBS=.; RC = CLOSE(DSID); %MEND NOBS; DATA _NULL_; %NOBS(TABLE); PUT ANY=; PUT NLOBS=; PUT NVARS=; RUN; Listing of Data Set TABLE ITEM_NO CODE ITEM_ NAME AMOUNT 1 B APPLE 44.45 2 D PEAR 125.00 3 X GRAPE . 4 C UNKNOWN SAS LOG ANY=1 (indicates that dataset has both observations and variables) NLOBS=4 (dataset has 4 logical observations) NVARS=4 (dataset has 4 variables)

The Verdict The book is an excellent Handbook on SAS Functions New SAS9.1 Functions are demonstrated What I wish it has: An associated web resource/software help manual for ease of lookup