Download presentation
Presentation is loading. Please wait.
Published byMarjorie Owens Modified over 9 years ago
1
Introduction to SAS Essentials Mastering SAS for Data Analytics Alan Elliott and Wayne Woodward SAS ESSENTIALS -- Elliott & Woodward1
2
Chapter 5 Preparing to Use SAS Procedures SAS ESSENTIALS -- Elliott & Woodward2
3
LEARNING OBJECTIVES SAS ESSENTIALS -- Elliott & Woodward3 To be able to use SAS® Support Statements To be able to use TITLE and FOOTNOTE To be able to include comments in your code To be able to use RUN and QUIT correctly To understand SAS PROC statement syntax To be able to use VAR statements To be able to use BY statements To be able to use ID statements To be able to use LABEL statements in a SAS procedure To be able to use WHERE statements To be able to use PROC PRINT Going Deeper: To be able to use common System Options Going Deeper: To be able to split column titles
4
5.1 UNDERSTANDING SAS SUPPORT STATEMENTS Using TITLE and FOOTNOTES Statements Specify up to 10 titles or footnotes TITLE ‘title text’; FOOTNOTE ‘footnote text’; or TITLEn ‘title text’; FOOTNOTEn ‘footnote text SAS ESSENTIALS -- Elliott & Woodward4 First line of either Title or Footnote Define line 2 to 9 of Titles or Footnotes
5
TITLE and FOOTNOTES Examples SAS ESSENTIALS -- Elliott & Woodward5 TITLE 'The first line of the title'; TITLE2 'The second line of the title'; TITLES 'Several lines skipped, then this title on the fifth line'; FOOTNOTE 'This is a footnote'; FOOTNOTE3 'This is a footnote, line 3'; Cancel all TITLE and FOOTNOTE lines with the statement TITLE; FOOTNOTE; Do Hands On Exercise P 114
6
Customizing Titles and Footnotes SAS ESSENTIALS -- Elliott & Woodward6 There are a number of options that can be used with the TITLE or FOOTNOTE statements to customize the look of your title. Some of these options include specifying color with a C= or COLOR= option. For example: TITLE C=BLUE H=5 "This is a title"; This title appears in the color (C= or COLOR=) blue with a height (H= or HEIGHT= ) larger than normal. Do Hands on Example p116.
7
SAS ESSENTIALS -- Elliott & Woodward7
8
Including Comments in Your SAS Code It is a good programming practice to include explanatory comments in your code. There are two options for putting comments in your code Method 1 - Begin with an asterisk (*), and end with a semi-colon (;). *This is a message It can be several lines long But it always ends with an ; ************************************************************* * Boxed messages stand out more, still end in a semicolon * *************************************************************; DATA MYDATA; * You can put a comment on a line of code; SAS ESSENTIALS -- Elliott & Woodward8
9
Comments Method 2 – Begin with /* and end with */ /*This is a SAS comment*/ /* Use this comment technique to comment out lines of code PROC PRINT; PROC MEANS; End of comment – the PROCS were ignored*/ The code from /* to */ Is ignored by SAS SAS ESSENTIALS -- Elliott & Woodward9
10
Using RUN and QUIT Statements SAS ESSENTIALS -- Elliott & Woodward10 The RUN statement causes previously entered SAS statements to be executed. It is called a boundary statement. For example: PROC PRINT; PROC MEANS; RUN; Another boundary statement is the QUIT statement. It is sometimes used in conjunction with a RUN statement to cease an active procedure. For example: PROC REG; RUN; QUIT;
11
5.2 UNDERSTANDING PROC STATEMENT SYNTAX Although there are scores of SAS PROCs (procedures), the syntax is consistent across all of them. The general syntax of the SAS PROC statement is: PROC name options; Statements/statementoptions;...etc... Statements/statementoptions; RUN ; SAS ESSENTIALS -- Elliott & Woodward11
12
Four parts of a PROC Statement PROC name options; statements/statementoptions ; The name of the SAS procedure such as MEANS or PRINT. OPTIONS appear BEFORE the semicolon. Typical options are DATA=, NOPRINT, and others – typically deal with the data set or output. STATEMENTS appear as a separate “phrase” (with its on semicolon.) These usually specify options within the procedure. There may be multiple Statements. Statements may have their own options following a slash (/). SAS ESSENTIALS -- Elliott & Woodward12
13
Example PROC Syntax (Options) SAS ESSENTIALS -- Elliott & Woodward13 The most commonly used option within the PROC statement is the DATA= option. For example: PROC PRINT DATA=MYDATA; RUN; The DATA= option tells SAS which data set to use in the analysis. Note that options appear in the PROC statement BEFORE the semicolon.
14
Example PROC Syntax (Statements) SAS ESSENTIALS -- Elliott & Woodward14 Procedure statements are often required to indicate information about how an analysis is to be performed. For example: PROC PRINT DATA=MYDATA; VAR ID GROUP TIMEl TIME2; RUN; STATEMENTS appear AFTER the first PROC semicolon
15
Example PROC Syntax (Statement Options) SAS ESSENTIALS -- Elliott & Woodward15 Statements can themselves have options. For example: PROC FREQ DATA=MYDATA; TABLES GROUP*SOCIO/CHISQ; RUN; This is a statement option – note that it follows a slach (/) within the Statement
16
Summary of typical PROC Syntax SAS ESSENTIALS -- Elliott & Woodward16 PROC FREQ DATA=MYDATA; TABLES GROUP*SOCIO/CHISQ; RUN; PROC name PROC option PROC Statement PROC Statement Option
17
SAS ESSENTIALS -- Elliott & Woodward17
18
Common PROC options Some typical options that are COMMON to most PROCS include: DATA=Specify data set to use in the analysis NOPRINT Do not display certain output OUT=Send results to an output data set SAS ESSENTIALS -- Elliott & Woodward18
19
Common PROC Statements VAR variable(s);Instructs SAS to use only the variables in the list for the analysis. BY variable(s); Repeats the procedure for each different value of the named variable(s). (The data set must first be sorted by the variables listed in the BY statement.) ID variable(s); Instructs SAS to use the specified variable as an observation identifier in a listing of the data. LABEL var='label'; Assigns a descriptive label to a variable. WHERE (expression); Instructs SAS to select only those observations for which the expression is true. SAS ESSENTIALS -- Elliott & Woodward19
20
OPTIONS Specific to a PROC In PROC MEANS, for example, the MAXDEC option specified how many decimal places to report. The options specific for PROC will be covered as the PROCS are introduced PROC MEANS DATA=“C:\SASDATA\SOMEDATA” MAXDEC=2; RUN; SAS ESSENTIALS -- Elliott & Woodward20 Notice that BOTH the DATA= and the MAXDEC= options are within the first semicolon.
21
Using the VAR Statement in a SAS Procedure The VAR Statement is often used to specify a list of variables to use in an analysis VAR varlist; An example is as follows: PROC MEANS; VAR HEIGHT WEIGHT AGE; RUN; SAS ESSENTIALS -- Elliott & Woodward21
22
Listing a range of Variables List a range of variables with consecutive numeric suffixes such as Q1, Q2, Q3, etc. to Q50 using a single dash between the first and last: Q1-Q50: VAR Q1-Q50; List a range of variables without consecutive suffixes with two dashes. Example: VAR ID - - TIME4; USE two dashes to indicate all variables between the indicated names SAS ESSENTIALS -- Elliott & Woodward22
23
Using the BY Statement in a SAS Procedure The BY statement allows you to quickly analyze subsets of your data. Repeat an analysis for each value in BY (data must be in sorted order by BY variable.) Example: PROC SORT DATA="C:\SASDATA\SOMEDATA" OUT=SORTED; BY GP;RUN; PROC MEANS DATA=SORTED MAXDEC=2; BY GP; RUN; Sort Note OUT= in OPTIONS BY is used first to SORT, then to request analysis by group. SAS ESSENTIALS -- Elliott & Woodward23 BY is used first to SORT, then to request analysis by group.
24
Do Hands-On Exercise p 121 Sort data sets Use BY to perform PROC by the indexed value Multiple results displayed SAS ESSENTIALS -- Elliott & Woodward24 GP is the BY variable… thus multiple analyses BY GP
25
5.3 USING THE ID STATEMENT IN A SAS PROCEDURE The ID statement provides you with a way to increase the readability of your output. It instructs SAS to use the specified variable as an observation identifier in a listing of the data (Instead of the OBS column.) * FIRST VERSION; PROC PRINT DATA=MYSASLIB.SOMEDATA;RUN ; * SECOND VERSION; PROC PRINT DATA=MYSASLIB.SOMEDATA; ID SUBJID; RUN; SAS ESSENTIALS -- Elliott & Woodward25 SUBJID is the ID variable
26
Do Hands On Exercise p 122 SAS ESSENTIALS -- Elliott & Woodward26 Observe Results, first without ID Statement Second time with ID Statement Notice how the Obs statement was replaced with the RAT_ID column
27
5.4 USING THE LABEL STATEMENT IN A SAS PROCEDURE Aversion of the LABEL statement allows you to create labels for variable names within a procedure. LABEL var='label'; Assigns a descriptive label to a variable. Example: PROC PRINT LABEL; ID RAT_ID; LABEL TRT='Treatment'; RUN; NOTE: This assignment of a LABEL only works during this PROC, unlike the LABEL statement in a DATA Step that is saved within the data set. SAS ESSENTIALS -- Elliott & Woodward27
28
Hands-On Exercise p 124 Example of the LABEL Statement in a PROC This code: PROC MEANS DATA=WEIGHT; LABEL WT_GRAMS="Treatment" MDATE="MEDOBS Date"; RUN; Produces this output SAS ESSENTIALS -- Elliott & Woodward28
29
5.5 USING THE WHERE STATEMENT IN A SAS PROCEDURE The WHERE statement allows you to specify a conditional criterion for which output will be included in an analysis. Example WHERE TRT="A"; within a PROC statement causes the procedure to only use records in the dataset that match the criteria TRT=“A”. SAS ESSENTIALS -- Elliott & Woodward29
30
Do Hands On Example p 125 Example of the WHERE statement PROC PRINT LABEL DATA=WEIGHT; ID RAT_ID; LABEL TRT='Treatment ' ; WHERE TRT="A"; RUN; SAS ESSENTIALS -- Elliott & Woodward30 This code produces this output… for only TRT=“A”
31
5.6 USING PROC PRINT Although several previous examples have used a simple version of the PROC PRINT procedure, a number of options for this procedure have not been discussed. Here are common options: SAS ESSENTIALS -- Elliott & Woodward31
32
Common Statements for PROC PRINT SAS ESSENTIALS -- Elliott & Woodward32 For example, the SUM statement specifies that a sum of the values for the variables listed is to be reported. SUM COST ;
33
Do Hands On Example p 127 Using APRINT1.SAS PROC PRINT DATA="C:\SASDATA\SOMEDATA" N = 'Number of Subjects is: ' Obs='Subjects'; SUM TIME1 TIME2 TIME3 TIME4; TITLE 'PROC PRINT Example'; RUN; SAS ESSENTIALS -- Elliott & Woodward33
34
Output from example showing PROC PRINT options and statement results SAS ESSENTIALS -- Elliott & Woodward34
35
5.7 GOING DEEPER: SPLITTING COLUMN TITLES IN PROC PRINT SAS ESSENTIALS -- Elliott & Woodward35 Normally, SAS splits titles at blanks when needed to conserve space in a report. If you want a different look, you can tell SAS where you want the labels to be split using the SPLIT= option. For example: PROC PRINT DATA=SOMEDATASET; SPLIT='*‘ LABEL INC_KEY='Subject*ID*============' AGE='Age in*2014*============' GENDER='Gender* *============‘; In this code, SAS splits the labels where it sees an asterisk. Do the Hands On Example p 129 (APRINT3.SAS).
36
Results of using the Split Option SAS ESSENTIALS -- Elliott & Woodward36 Note how the splits for labels occur – according to where the asterisks were in the code.
37
5.8 GOING DEEPER: COMMON SYSTEM OPTIONS SAS ESSENTIALS -- Elliott & Woodward37 Although not a part of a PROC statement, System Options can be used to customize the way output is displayed or how data in a data set is used. This section introduces some commonly used options. System Options are specified using the OPTIONS statement. The syntax for the OPTIONS statement is OPTIONS option1 option2... ; For example OPTIONS ORIENTATION=LANDSCAPE;
38
Common System Options (See Table 5.14) SAS ESSENTIALS -- Elliott & Woodward38 Common System OptionsMeaning FIRSTOBS=n and OBS=n;Specifies the first observation to be used in a data set (FIRSTOBS=) and the last observation to be used (OBS= ). For example OPTIONS FIRSTOBS=2 OBS=21; causes SAS to use data records 2 through 21 in any subsequent analysis. When this option is set, it is usually a good idea to reset the values to OPTIONS FIRSTOBS=1; OBS=MAX; at the end of the program so subsequent analyses are not limited by the same options. YEAR CUTOFF= yearSpecifies the cutoff year for two digit dates in a 100 year span starting with the specified date. For example if YEARCUTOFF=1920 then the data 01115119 would be considered 2019 while 01/15/21 would be seen as 1921. The default YEARCUTOFF is 1926. (For SAS versions 9 through 9.3, the cutoff year was 1920.)
39
Common System Options (continued) SAS ESSENTIALS -- Elliott & Woodward39 System OptionsMeaning PROBSIG=nSpecifies the number of decimals used when reporting p-values. For example PROBSIG=3 would cause p-values to be reported to three decimal places. LINESIZE= n and PAGESIZE= nControls number of characters in an output line (LINESIZE) or number of lines on a page (PAGESIZE) for RTF and PDF output. NONUMBERSpecifies no page numbers will be included in RTF or PDF output. NODATESpecifies no date will be included in RTF or PDF output. ORIENTATION=optionSpecified paper orientation. Options are PORTRAIT or LANDSCAPE for RTF or PDF output. NOCENTERLeft justifies output (default is centered)
40
Do Hands On Exercise p 132 SAS ESSENTIALS -- Elliott & Woodward40 (SYSOBS.SAS) OPTIONS FIRSTOBS=11 OBS=20; PROC PRINT LABEL DATA="C:\SASDATA\SOMEDATA"; RUN; OPTIONS FIRSTOBS=1 OBS=MAX ; Sets system option so only records 11 to 20 are used in any future data sets. It is important to reset the options to the defaults to avoid an error in future data sets
41
5.9 SUMMARY SAS ESSENTIALS -- Elliott & Woodward41 This chapter introduced you to the syntax of SAS procedures in preparation for using specific PROCs discussed in the remainder of the book. It also introduced PROC PRINT and illustrated some of the common options used for this procedure. Continue to Chapter 6: SAS® ADVANCED PROGRAMMING TOPICS PART 1
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.