Download presentation
Presentation is loading. Please wait.
1
SAS Basics: Statement and Data Set
SAS Workshop SAS Basics: Statement and Data Set Hun Myoung Park, Ph.D. University Information Technology Services Center for Statistical and Mathematical Computing Saturday, December 29, 2018 © The Trustees of Indiana University (812) , (317)
2
Outline SAS Statement SAS Basic Rules Naming Rules
SAS Basics December 29, 2018 Outline SAS Statement SAS Basic Rules Naming Rules Data Structure & Data Type SAS Library Operators Functions OPTIONS TITLE & FOOTNOTE University Information Technology Services Center for Statistical and Mathematical Computing
3
SAS Basics December 29, 2018 SAS STATEMENT 1 A SAS script is a collection of SAS statements (equivalent to commands). A statement tell what SAS should do. A SAS statement consists of keywords, various names, special characters, functions, operators, and etc. Some SAS statements may be used in a DATA step or a PROC step. Other statements (e.g., OPTIONS) can be used other place in a SAS script. University Information Technology Services Center for Statistical and Mathematical Computing
4
SAS Basics December 29, 2018 SAS STATEMENT 2 DATA steps create and modify variables and data sets; select observations; select variables; append, merge, and splits data sets. Can write programs in a DATA step. PROC steps (e.g., PROC REG and PROC GLM) conduct analyses. RUN executes statements that are listed before the command. University Information Technology Services Center for Statistical and Mathematical Computing
5
SAS BASIC RULES A statement ends with semi-colon (;).
SAS Basics December 29, 2018 SAS BASIC RULES A statement ends with semi-colon (;). A statement begins and ends at any place. A line can have more than one statement that is separated by ; SAS is not case-sensitive. Operators do not compute missing values, while functions ignore missing and computes with non-missing values. A comment is enclosed by /* and */ or follows * and ends with ; University Information Technology Services Center for Statistical and Mathematical Computing
6
SAMPLE SAS SCRIPT /* This is a sample SAS script */
SAS Basics December 29, 2018 SAMPLE SAS SCRIPT /* This is a sample SAS script */ OPTIONS LS=80 PS=55 NOLABEL NODATE; TITLE ‘Sample SAS Script’; LIBNAME sm ‘c:\temp\sas’; * DATA Step; /* This Is a Comment */ DATA sm.grade; … PROC GLM DATA=sm.cancer; RUN; University Information Technology Services Center for Statistical and Mathematical Computing
7
SAS Basics December 29, 2018 NAMING RULES Names used for data sets, variables, formats, libraries, and others in SAS. Characters, number, and _, but NO space. Begins with a character (e.g., q1, q2 > 1q, 2q) Avoid reserved words (e.g., REG, ANOVA) The shorter, the better (< 8 characters) Meaningful names (e.g., q1, q2 > xxx, yyy) Consistent and systematic names (e.g., q1-q9) Use lowercases (e.g., trust > TRUST) Use a value of a binary variable (e.g., male) University Information Technology Services Center for Statistical and Mathematical Computing
8
DATA STRUCTURE Relational database, a set of tables.
SAS Basics December 29, 2018 DATA STRUCTURE Relational database, a set of tables. A table (data set) consists of entities or observations in row and variables, attributes of an entity, in column. University Information Technology Services Center for Statistical and Mathematical Computing
9
DATA TYPE 1. Character (string)
SAS Basics December 29, 2018 DATA TYPE 1. Character (string) 2. Numeric (integer, real number, double precision) 3. Date and time: computation issue For general users, numeric type is preferred to character if character type is not really needed. Character is preferred to date and time and convert data type when computation is needed. University Information Technology Services Center for Statistical and Mathematical Computing
10
SAS Basics December 29, 2018 SAS LIBRARY An alias of the collection of data sets that tells where data sets of interest exist. Every data set should be referred using a library in SAS. Default library is “work” where data sets are temporarily stored and will be removed when SAS terminates. Define your own library in order to store data sets permanently. A physical directory or folder can be referred by more than one library. Make it easy to use SAS script in other computers with different settings. University Information Technology Services Center for Statistical and Mathematical Computing
11
SAS Basics December 29, 2018 SAS LIBRARY EXAMPLE LIBNAME statement associates a SAS data library with a library reference (specific directory or folder). LIBNAME statement should be executed before DATA and PROC steps. LIBNAME sm ‘c:\temp\sas’; When a SAS data set is called in a DATA or PROC step, its name follows a library name and period. DATA sm.cancer; PROC REG DATA=sm.cancer; University Information Technology Services Center for Statistical and Mathematical Computing
12
SAS OPERATOR 1 Arithmetic Operators Concatenation: || +, - *, /
SAS Basics December 29, 2018 SAS OPERATOR 1 Arithmetic Operators +, - *, / ** (not ^) e.g., powers=2**3; >< (minimum), <>(maximum) e.g., mins=5><6; Concatenation: || e.g., iu=‘Indiana’ || ‘ ‘ || ‘University’; University Information Technology Services Center for Statistical and Mathematical Computing
13
SAS OPERATOR 2 Relational Operators Logical Operators
SAS Basics December 29, 2018 SAS OPERATOR 2 Relational Operators < or LT, > or GT <= or LE, >= or GE = or EQ, ^= or NE Logical Operators & or AND | or OR ^ or NOT IN(a,b,c) /* a or b or c */ University Information Technology Services Center for Statistical and Mathematical Computing
14
SAS FUNCTION 1 Mathematical Functions Statistical Functions
SAS Basics December 29, 2018 SAS FUNCTION 1 Mathematical Functions ABS(), SQRT(), EXP(), LOG(), LOG10() FACT(), DIF(), DIF2(), LAG(), LAG3() SIN(), COS(), TAN(), SIGN() CEIL(), FLOOR(), INT(), ROUND() Statistical Functions N(), SUM(), MEAN(), MEDIAN() MEAN(OF a1-a10) MAX(), MIN(), RANGE(), IQR() KURTOSIS(), SKEWNESS(), STD() STDERR(), VAR() University Information Technology Services Center for Statistical and Mathematical Computing
15
SAS FUNCTION 2 Probability Distribution Functions
SAS Basics December 29, 2018 SAS FUNCTION 2 Probability Distribution Functions PDF(), CDF() PROBNORM(), PROBT() PROBCHI(), PROBF() POISSON() PROBNML(), PROBNEGB() Random Number Functions RAND() RANNOR(), RANUNI() RANPOI(), RANBIN() University Information Technology Services Center for Statistical and Mathematical Computing
16
SAS FUNCTION 3 String Functions Date Functions
SAS Basics December 29, 2018 SAS FUNCTION 3 String Functions TRIM(), LTRIM(), RTRIM(), LEFT(), RIGHT() UPCASE(), LENGTH(), REVERSE() SUBSTR(str, start, length) REPEAT(str, repetition) COUNT(str, key), FIND(str, key) Date Functions YEAR(), QTR(date), MONTH(date) WEEK(date), WEEKDAY(date) DATETIME(), TIME() DATE(), DAY(date), MDY(month, day, year) University Information Technology Services Center for Statistical and Mathematical Computing
17
FUNCTION VERSUS OPERATOR
SAS Basics December 29, 2018 FUNCTION VERSUS OPERATOR Some examples avg1 = (a1+a2+a3)/3; avg2 = MEAN(a1, a2, a3); avg3 = MEAN(OF a1-a3); Avg1-avg3 produce the same result. If any value in a1 through a3 is missing, avg1 has missing and avg2/avg3 return average of non-missing values. If OF is omitted, it returns a1-a3. When -- is used instead of -, it gives a1+a3. avg4 = MEAN(a1-a3); /* = a1-a3 */ avg5 = MEAN(a1--a3); /* = a1+a3 */ University Information Technology Services Center for Statistical and Mathematical Computing
18
SAS Basics December 29, 2018 OPTIONS An independent statement to change values of SAS system options. Each statement can change multiple options. Can appears multiple times. OPTIONS NODATE NOCENTER; OPTIONS LINESIZE=100 PAGESIZE=50; OPTIONS NOLABEL NONUMBER; OPTIONS PAGENO=5; OPTIONS FIRSTOBS=2 OBS=100; /* begin & end */ OPTIONS MISSING=‘M’; OPTIONS LIFTMARGIN=1 RIGHTMARGIN=1; University Information Technology Services Center for Statistical and Mathematical Computing
19
SAS Basics December 29, 2018 TITLE AND FOOTNOTE Independent statements to add titles or footnotes up to 10 lines. TITLE and FOOTNOTE without a title eliminate the existing title and footnote. TITLE ‘SAS Workshop at Indiana University’; TITLE2 ‘UITS Stat/Math Center (2010)’; TITLE3 ‘Data Step Example’; DATA sm.cancer; …; TITLE3 ‘Sort a Data Set’; PROC SORT … ; TITLE3; /* remove title 3 */ University Information Technology Services Center for Statistical and Mathematical Computing
20
RESOURCES http://support.sas.com/documentation/index.html
SAS Basics December 29, 2018 RESOURCES http// University Information Technology Services Center for Statistical and Mathematical Computing
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.