Professional Documents
Culture Documents
What Is SAS?: Statistical Analysis Software Main Uses of SAS
What Is SAS?: Statistical Analysis Software Main Uses of SAS
SAS Training 1
Basics about SAS
SAS is composed of three windows
• Program Editor
where you write and submit programs
• Log
where SAS displays messages which indicates any errors that
may be in a program
• Output
Where result appear after submitting programs
SAS Training 2
Program Editor Window
Explorer
and Write your code in
Results this window
Window
SAS Training 3
Log Window
Log Window
SAS Training 4
Output Window
Output Window
View the Values of a Dataset
in this Window
SAS Training 5
SAS Dataset, Variables and
Observations
SAS expects your data to be in a special
form. This special form is called a SAS
data set
The SAS data set is a tabular form with
Variables and Observations
The rows are the Observations
The columns are the Variables
SAS Training 6
Example of a SAS Data set
Variables
ID NAME HT WT
1 53 Lucy 42 41
2 54 Tom 46 54
Observations 3 55 Dan 43 .
4 56 Tim 45 56
5 57 42 48
6 58 Mary 48 43
SAS Training 8
Basic Components of SAS, Continued
Data Step
SAS statement that read data, create new datasets or variables,
modify datasets, perform calculation.
Procedures
SAS statements that can perform statistical analyses,
create & print reports & graphs
SAS Training 9
Basic Components of SAS
Every SAS program is constructed using the Data Step and/or
Procedures
e.g. DATA distance;
miles = 23; DATA Step
kilometer = 1.61 * miles;
RUN;
SAS Training 10
Difference Between a DATA step and
a PROC step
Note: The table is not meant to imply that PROC can never create SAS data
sets (some do), or that DATA step can never create reports (they can)
But it is much easier to write SAS programs if one can understand the basic
functions of DATA and PROC steps.
SAS Training 11
Execution of SAS data set
Data Steps execute line by line and observation by observation
SAS takes the first observation and runs it all the way through
the data step (line by line) before looping back to pick up the
second observation
SAS sees one observation at a time.
Observation 1 Line 1
Observation 1
Line 2
Observation 2 Line 3 Observation 2
Observation 3 Line 4
Observation 3
Line 5
SAS Training 12
Rules for SAS programs
SAS Training 13
Rules for SAS program
Statements
SAS program statements can
Be in upper or lower case
SAS Training 14
READING DATA FROM
EXTERNAL FILES
SAS can read data and create a data
set from external files like txt, csv, etc.
DATA dataset-name;
INFILE file-specification <options> <host-options>;
INPUT <specification(s)><@|@@>;
Run;
SAS Training 15
Reading data using
DATALINES
A SAS data set can also be created bytyping values in
the SAS program editor using DATALINES;
Basic Code Structure
DATA dataset-name;
INPUT <specification(s)><@|@@>;
DATALINES;
…………..
…………..’
;
Run;
SAS Training 16
INPUT
INPUT <specification(s)><@|@@>;
SAS Training 17
INPUT (SPECIFICATIONS)
variable - names a variable that is assigned input values
SAS Training 18
EXAMPLE 1 (EXTERNAL FILE)
Mary 24
Dat.txt in C:/ Suzan 34
SAS Training 19
INPUT (DATALINES)
Data newdata;
Input name $ age;
DATALINES;
Mary 24
Suzan 32
;
Run;
SAS Training 20
INPUT TYPES
INPUT, COLUMN : Reads input values from
specified columns and assigns them to the
corresponding SAS variables
Syntax:
INPUT variable <$> start-column <-- end-
column>
<.decimals> <@ | @@>;
SAS Training 21
Example
data scores;
input name $ 1-18 score1 25-27 score2 30-32 score3 35-37;
datalines;
Joseph 11 32 76
Mitchel 13 29 82
Sue Ellen 14 27 74
;
Run;
SAS Training 22
INPUT TYPES
INPUT, Formatted : Reads input values with
specified informats and assigns them to the
corresponding SAS variables.
Syntax:
INPUT <pointer-control> variable informat.
<@ | @@>;
SAS Training 23
Example
data sales;
infile file-specification;
input item $10. +5 jan comma5. +5 feb comma5. +5 mar comma5.;
run;
SAS Training 24
INPUT TYPES
INPUT, List : Scans the input data record for
input values and assigns them to the
corresponding SAS variables.
Syntax :
INPUT <pointer-control> variable <$> <&>
<@ | @@>;
SAS Training 25
Example
data scores;
input name $ score1 score2 score3 team $;
datalines;
Joe 11 32 76 red
Mitchel 13 29 82 blue
Susan 14 27 74 green
;
SAS Training 26
Merge Statement
The MERGE statement is flexible and
has a variety of uses in SAS
programming
One-to-One
Match Merge
SAS Training 27
One-to-one matching
To combine variables from several data sets
where there is a one-to-one correspondence
between the observations in each of the data
sets, list the data sets to be joined on a
merge statement.
SAS Training 28
Match Merge
When there is not an exact one-to-one
correspondence between data sets to be
merged, the variables to use to identify
matching observations can be specied on a by
statement.
The data sets being merged must be sorted by
the variables specified on the by statement.
SAS Training 29
Example – One to One Merge
SAS Training 30
Example – Match Merge
SAS Training 31
Example – Match Merge
SAS Training 32
Manipulating result of merge statement
with IN values
SAS Training 33
SAS Functions
Definition of Functions
A SAS function performs a computation or system manipulation on
arguments and returns a value. Most functions use arguments supplied
by the user, but a few obtain their arguments from the operating
environment.
In base SAS software, you can use SAS functions in DATA step
programming statements, in a WHERE expression, in macro language
statements, in PROC REPORT, and in Structured Query Language
(SQL).
SAS Training 34
Syntax of Functions
The syntax of a function is
function-name (argument-1<. . .,argument-n>)
function-name (OF variable-list)
where
function-name
names the function.
argument
can be a variable name, constant, or any SAS expression, including
another function. The number and kind of arguments allowed are described
with individual functions. Multiple arguments are separated by a comma.
SAS Training 35
Selected SAS Functions
Numeric Functions
ROUND : Rounds to the nearest round-off unit.
Syntax
ROUND(argument,round-off-unit)
Arguments
argument
is numeric.
round-off-unit
is numeric and nonnegative.
Details : The ROUND function returns a value rounded to the nearest round-
off unit. If round-off-unit is not provided, a default value of 1 is used and
argument is rounded to the nearest integer.
SAS Training 36
Selected SAS Functions
Function ROUND Examples :
SAS Training 37
Selected SAS Functions
SUM : Returns the sum of the nonmissing arguments.
Syntax
SUM(argument,argument, ...)
Arguments
argument
is numeric. The argument list can consist of a variable list,
which is preceded by OF.
SAS Training 38
Selected SAS Functions
Function SUM Examples :
x1=sum(4,9,3,8); 24
x2=sum(4,9,3,8,.); 24
x3=sum(of x1-x2); 48
y1=20; 50
y2=30;
x5=sum(of y:);
SAS Training 39
Selected SAS Functions
MEAN : Returns the arithmetic mean (average)
Syntax
MEAN(argument,argument, . . .)
Arguments
argument
is numeric. At least one argument is required. The argument list
may consist of a variable list, which is preceded by OF.
Examples :
SAS Statement Results
x1=mean(2,.,.,6); 4
x2=mean(1,2,3,2); 2
x3=mean(of x1-x2); 3
SAS Training 40
Selected SAS Functions
INT : Returns the integer value.
Syntax
INT(argument)
Arguments
argument
is numeric.
Details
The INT function returns the integer portion of the argument
(truncates the decimal portion). If the value of argument is
positive, INT(argument) has the same result as FLOOR(argument).
If the value of argument is negative, INT(argument) has the same
result as CEIL(argument).
SAS Training 41
Selected SAS Functions
Function INT Examples :
var1=2.1; 2
x=int(var1);
put x=;
var2=-2.4; -2
y=int(var2);
put y=;
a=int(3); 3
put a=;
b=int(-1.6); -1
put b=;
SAS Training 42
Selected SAS Functions
LAG : Returns values from a queue.
Syntax
LAG<n>(argument)
Arguments
n
specifies the number of lagged values.
argument
is numeric or character.
Details
The LAG functions, LAG1, LAG2, . . . , LAG100 return values from a queue.
LAG1 can also be written as LAG. A LAG n function stores a value in a queue
and returns a value stored previously in that queue.
SAS Training 43
Selected SAS Functions
Function LAG Examples :
Creating a Data Set
The following program creates a data set that contains the values for X, Y,
and Z.
options pagesize=25 linesize=64 nodate pageno=1;
data one;
input X @@;
Y=lag1(x);
Z=lag2(x);
datalines;
123456;
proc print;
title 'Lag Output';
run;
SAS Training 44
Selected SAS Functions
Function LAG Examples :
Creating a Data Set :LAG1 returns one missing value and the values of X
(lagged once). LAG2 returns two missing values and the values of X (lagged
twice).
Lag Output 1
Obs X Y Z
1 1 . .
2 2 1 .
3 3 2 1
4 4 3 2
5 5 4 3
6 6 5 4
SAS Training 45
Selected SAS Functions
Character Functions
UPCASE: Converts all letters in an argument to uppercase.
Syntax
UPCASE(argument)
Arguments
argument
specifies any SAS character expression.
Details :
The UPCASE function copies a character argument, converts all lowercase
letters to uppercase letters, and returns the altered value as a result.
SAS Training 46
Selected SAS Functions
Function UPCASE Examples :
SAS Training 47
Selected SAS Functions
LOWCASE: Converts all letters in an argument to
lowercase.
Syntax
LOWCASE(argument)
Arguments
argument
specifies any SAS character expression.
Details :
The LOWCASE function copies a character argument, converts all uppercase
letters to lowercase letters, and returns the altered value as a result.
SAS Training 48
Selected SAS Functions
Function LOWCASE Examples :
x='INTRODUCTION'; introduction
y=lowcase(x);
put y;
SAS Training 49
Selected SAS Functions
SUBSTR (left of=): Replaces character value contents
Syntax
SUBSTR(argument,position<,n>)=characters-to-replace
Arguments
argument
specifies a character variable.
position
specifies a numeric expression that is the beginning character position.
n
specifies a numeric expression that is the length of the substring that will
be replaced.
characters-to-replace
specifies a character expression that will replace the contents of
argument.
SAS Training 50
Selected SAS Functions
Details
When you use the SUBSTR function on the left side of an assignment
statement, SAS places the value of argument with the expression on right
side. SUBSTR replaces n characters starting at the character you specify in
position.
b=a; CATTY
substr(b,4)='TY';
put b;
SAS Training 51
Selected SAS Functions
SUBSTR(right of=):Extracts a substring from an argument
Syntax
<variable=>SUBSTR(argument,position<,n>)
Arguments
variable
specifies a valid SAS variable name.
argument
specifies any SAS character expression.
position
specifies a numeric expression that is the beginning character position.
n
specifies a numeric expression that is the length of the substring to
extract.
SAS Training 52
Selected SAS Functions
Details
The SUBSTR function returns a portion of an expression that you specify in
argument. The portion begins with the character specified by position and is
the number of characters specified by n.
A variable that is created by SUBSTR obtains its length from the length of
argument.
Function SUBSTR(right of =) Examples :
----+----1----+----2
date='06MAY98'; MAY 98
month=substr(date,3,3);
year=substr(date,6,2);
put @1 month @5 year;
SAS Training 53
Selected SAS Functions
SCAN : Selects a given word from a character expression.
Syntax
SCAN(argument,n<, delimiters>)
Arguments
argument
specifies any character expression.
n
specifies a numeric expression that produces the number of the
word in the character string you want SCAN to select.
delimiters
specifies a character expression that produces characters that you want
SCAN to use as word separators in the character string.
SAS Training 54
Selected SAS Functions
Details
Leading delimiters before the first word in the character string do not effect
SCAN. If there are two or more contiguous delimiters, SCAN treats them as
one.
arg='ABC.DEF(X=Y)'; X=Y
word=scan(arg,3);
put word;
word=scan(arg,-3); ABC
put word;
SAS Training 55
Selected SAS Functions
TRIM: Removes trailing blanks from character expressions
and returns one blank if the expression is missing.
Syntax
TRIM(argument)
Arguments
argument
specifies any SAS character expression.
Details :
TRIM copies a character argument, removes all trailing blanks, and returns
the trimmed argument as a result. If the argument is blank, TRIM returns
one blank. TRIM is useful for concatenating because concatenation does not
remove trailing blanks.
Assigning the results of TRIM to a variable does not affect the length of the
receiving variable. If the trimmed value is shorter than the length of the
receiving variable, SAS pads the value with new blanks as it assigns it to the
variable.
SAS Training 56
Selected SAS Functions
Function TRIM Examples :
1. Removing Trailing Blanks
These statements and this data line produce these results:
data test;
input part1 $ 1-10 part2 $ 11-20;
hasblank=part1||part2;
noblank=trim(part1)||part2;
put hasblank;
put noblank;
datalines;
Data Line Results
apple sauce ----+----1----+----2
apple sauce
applesauce
SAS Training 57
Selected SAS Functions
TRANSLATES : Replaces specific characters in a character
expression.
Syntax
TRANSLATE(source,to-1,from-1<,...to-n,from-n>)
Arguments
source
specifies the SAS expression that contains the original character value.
to
specifies the characters that you want TRANSLATE to use as substitutes.
from
specifies the characters that you want TRANSLATE to replace.
SAS Training 58
Selected SAS Functions
Details
The maximum number of pairs of to and from arguments that TRANSLATE
accepts depends on the operating environment you use to run SAS. There is
no functional difference between using several pairs of short arguments, or
fewer pairs of longer arguments.
x=translate('XYZW','AB','VW'); XYZB
put x;
SAS Training 59
Selected SAS Functions
LENGTH: Returns the length of an argument.
Syntax
LENGTH(argument)
Arguments
argument
specifies any SAS expression.
Details :
The LENGTH function returns an integer that represents the position of the
right-most nonblank character in the argument. If the value of the
argument is missing, LENGTH returns a value of 1. If the argument is a
numeric variable (either initialized or uninitialized), LENGTH returns a value
of 12 and prints a note in the SAS log that the numeric values have been
converted to character values.
SAS Training 60
Selected SAS Functions
Function LENGTH Examples :
SAS Training 61
Selected SAS Functions
INDEX: Searches a character expression for a string of characters.
Syntax
INDEX(source,excerpt)
Arguments
source
specifies the character expression to search.
excerpt
specifies the string of characters to search for in the character expression.
Details :
The INDEX function searches source, from left to right, for the first occurrence of the
string specified in excerpt, and returns the position in source of the string's first
character. If the string is not found in source, INDEX returns a value of 0. If there are
multiple occurrences of the string, INDEX returns only the position of the first
occurrence.
SAS Training 62
Selected SAS Functions
Function INDEX Examples :
SAS Training 63
SAS Statements
Definition of Statements
A SAS statement is a series of items that may include keywords, SAS names,
special characters, and operators. All SAS statements end with a semicolon. A
SAS statement either requests SAS to perform an operation or gives information
to the system.
those that are global in scope and can be used anywhere in a SAS program.
SAS Training 64
Selected SAS Statements
DATA step Statements
DELETE : Stops processing the current observation.
Syntax
DELETE;
Without Arguments
When DELETE executes, the current observation is not written to a data set,
and SAS returns immediately to the beginning of the DATA step for the next
iteration.
SAS Training 65
Selected SAS Statements
Statement DELETE Examples :
SAS Training 66
Selected SAS Statements
DO : Designates a group of statements to be executed as a
unit.
Syntax
DO;
...more SAS statements...
END;
Without Arguments
Use the DO statement for simple DO group processing.
SAS Training 67
Selected SAS Statements
Statement DO Examples :
In this simple DO group, the statements between DO and END are performed
only when YEARS is greater than 5. If YEARS is less than or equal to 5,
statements in the DO group do not execute, and the program continues with
the assignment statement that follows the ELSE statement.
if years>5 then
do;
months=years*12;
put years= months=;
end;
else yrsleft=5-years;
SAS Training 68
Selected SAS Statements
DROP : Excludes variables from output SAS data sets
Syntax
DROP variable-list;
Arguments
variable-list
specifies the names of the variables to omit from the output
data set.
Details : The DROP statement applies to all the SAS data sets that are
created within the same DATA step and can appear anywhere in the step.
The variables in the DROP statement are available for processing in the
DATA step. If no DROP or KEEP statement appears, all data sets that are
created in the DATA step contain all variables. Do not use both DROP and
KEEP statements within the same DATA step.
SAS Training 69
Selected SAS Statements
Statement DROP Examples :
These examples show the correct syntax for listing variables with the DROP statement:
• drop time shift batchnum;
• drop grade1-grade20;
In this example, the variables PURCHASE and REPAIR are used in processing but are
not written to the output data set INVENTRY:
data inventry;
drop purchase repair;
infile file-specification;
input unit part purchase repair;
totcost=sum(purchase,repair);
run;
SAS Training 70
Selected SAS Statements
KEEP : Includes variables in output SAS data sets
Syntax
KEEP variable-list;
Arguments
variable-list
specifies the names of the variables to write to the output data
set.
Details : The KEEP statement causes a DATA step to write only the
variables that you specify to one or more SAS data sets. The KEEP
statement applies to all SAS data sets that are created within the same
DATA step and can appear anywhere in the step. If no KEEP or DROP
statement appears, all data sets that are created in the DATA step contain
all variables.
SAS Training 71
Selected SAS Statements
Statement KEEP Examples :
These examples show the correct syntax for listing variables in the KEEP statement:
• keep name address city state zip phone;
• keep rep1-rep5;
This example uses the KEEP statement to include only the variables NAME and AVG in the
output data set. The variables SCORE1 through SCORE20, from which AVG is calculated,
are not written to the data set AVERAGE.
data average;
keep name avg;
infile file-specification;
input name $ score1-score20;
avg=mean(of score1-score20);
run;
SAS Training 72
Selected SAS Statements
LABEL : Assigns descriptive labels to variables
Syntax
LABEL variable-1='label-1' . . . <variable-n='label-n'>;
SAS Training 73
Selected SAS Statements
Details
Using a LABEL statement in a DATA step permanently associates labels with variables by
affecting the descriptor information of the SAS data set that contains the variables. You can
associate any number of variables with labels in a single LABEL statement.
Specifying Labels
Here are several LABEL statements:
• label compound='Type of Drug';
• label date="Today's Date ";
• label n='Mark''s Experiment Number';
• label score1="Grade on April 1 Test" score2="Grade on May 1 Test";
SAS Training 74
Selected SAS Statements
Removing a Label
This example removes an existing label:
data rtest;
set rtest;
label x=' ';
run;
SAS Training 75
Selected SAS Statements
LENGTH : Specifies the number of bytes for storing variables
Syntax
LENGTH variable-specification(s)
<DEFAULT=n>;
Arguments
variable-specification
is a required argument and has the form where
variable
names one or more variables that are to be assigned a length. This includes any
variables in the DATA step, including those dropped from the output data set.
$
indicates that the preceding variables are character variables.
length
specifies a numeric constant that is the number of bytes used for storing variable
values.
SAS Training 76
Selected SAS Statements
DEFAULT=n
changes the default number of bytes that SAS uses to store the
values of any newly created numeric variables.
Details
In general, the length of a variable depends on
whether the variable is numeric or character
how the variable was created
whether a LENGTH statement is present.
SAS Training 77
Selected SAS Statements
RENAME : Specifies new names for variables in output SAS data sets
Syntax
RENAME old-name-1=new-name-1 . . . <old-name-n=new-name-n>;
Arguments
old-name
specifies the name of a variable or variable list as it appears in the input data set, or in the
current DATA step for newly created variables.
new-name
specifies the name or list to use in the output data set.
Details
The RENAME statement allows you to change the names of one or more variables, variables
in a list, or a combination of variables and variable lists. The new variable names are
written to the output data set only. Use the old variable names in programming
statements for the current DATA step. RENAME applies to all output data sets.
SAS Training 78
Selected SAS Statements
Statement RENAME Examples :
These examples show the correct syntax for renaming variables using the RENAME statement
• rename street=address;
• rename time1=temp1 time2=temp2 time3=temp3;
• rename name=Firstname
score1-score3=Newscore1-Newscore3;
This example uses the old name of the variable in program statements. The variable Olddept is named
Newdept in the output data set, and the variable Oldaccount is named Newaccount.
rename Olddept=Newdept Oldaccount=Newaccount;
if Oldaccount>5000;
keep Olddept Oldaccount items volume;
SAS Training 79
Selected SAS Statements
WHERE : Selects observations from SAS data sets that meet a particular condition
Syntax
WHERE where-expression-1
< logical-operator where-expression-n>;
Arguments
where-expression
is an arithmetic or logical expression that generally consists of a sequence of operands and
operators.
logical-operator
can be AND, AND NOT, OR, or OR NOT.
Details :Using the WHERE statement may improve the efficiency of your SAS programs because SAS is
not required to read all observations from the input data set.
The WHERE statement cannot be executed conditionally; that is, you cannot use it as part of an
IF-THEN statement.
WHERE statements can contain multiple WHERE expressions that are joined by logical operators.
SAS Training 80
Selected SAS Statements
Statement WHERE Examples :
SAS Training 81
Selected SAS Statements
IF-THEN/ELSE : Executes a SAS statement for observations that
meet specific conditions
Syntax
IF expression THEN statement;
<ELSE statement;>
Arguments
expression
is any SAS expression and is a required argument.
statement
can be any executable SAS statement or DO group.
SAS Training 82
Selected SAS Statements
Details :SAS evaluates the expression in an IF-THEN
statement to produce a result that is either nonzero, zero, or
missing. A nonzero and nonmissing result causes the
expression to be true; a result of zero or missing causes the
expression to be false.
If the conditions that are specified in the IF clause are met, the
IF-THEN statement executes a SAS statement for observations
that are read from a SAS data set, for records in an external
file, or for computed values. An optional ELSE statement gives
an alternative action if the THEN clause is not executed. The
ELSE statement, if used, must immediately follow the IF-THEN
statement.
SAS Training 83
Selected SAS Statements
Statement IF-THEN\ELSE Examples :
These examples show different ways of specifying the IF-THEN/ELSE statement
• if x then delete;
• if x=0 then
if y ne 0 then put 'X ZERO, Y NONZERO';
else put 'X ZERO, Y ZERO';
else put 'X NONZERO';
SAS Training 84
Selected SAS Statements
BY : Controls the operation of a SET, MERGE, MODIFY, or UPDATE
statement in the DATA step and sets up special grouping variables
Syntax
BY <DESCENDING> <GROUPFORMAT> variable-1
<. . .<DESCENDING> <GROUPFORMAT>
variable-n> <NOTSORTED>;
Arguments
DESCENDING
indicates that the data sets are sorted in descending order by the variable that is specified.
DESCENDING means largest to smallest numerically, or reverse alphabetical for character
variables.
SAS Training 85
Selected SAS Statements
variable
names each variable by which the data set is sorted or indexed. These variables are referred to as BY
variables for the current DATA or PROC step.
NOTSORTED
specifies that observations with the same BY value are grouped together but are not necessarily sorted in
alphabetical or numeric order.
Details :In a DATA Step the BY statement applies only to the SET, MERGE, MODIFY, or UPDATE statement
that precedes it in the DATA step, and only one BY statement can accompany each of these statements in
a DATA step
In PROC step you can specify the BY statement with some SAS procedures to modify their action.
SAS Training 86
Selected SAS Statements
Statement BY Examples :
Specifying One or More BY Variables
• Observations are ordered by the name of the month in which the expenses
were accrued:
by month notsorted;
SAS Training 87
Selected SAS Statements
RETAIN : Causes a variable that is created by an INPUT or assignment
statement to retain its value from one iteration of the DATA step to the next
Syntax
RETAIN <element-list(s) <initial-value(s) |
(initial-value-1) | (initial-value-list-1) >
< . . . element-list-n <initial-value-n |
(initial-value-n ) | (initial-value-list-n)>>>;
Without Arguments
If you do not specify an argument, the RETAIN statement causes the values of all variables
that are created with INPUT or assignment statements to be retained from one iteration of the
DATA step to the next.
SAS Training 88
Selected SAS Statements
Arguments
element-list
specifies variable names, variable lists, or array names whose values you want retained.
initial-value
specifies an initial value, numeric or character, for one or more of the preceding elements.
(initial-value-list)
specifies an initial value, numeric or character, for individual elements in the preceding
list. SAS matches the first value in the list with the first variable in the list of
elements, the second value with the second variable, and so on.
SAS Training 89
Selected SAS Statements
Details:
Without a RETAIN statement, SAS automatically sets variables that are assigned values
by an INPUT or assignment statement to missing before each iteration of the DATA
step.
Use a RETAIN statement to specify initial values for individual variables, a list of
variables, or members of an array. If a value appears in a RETAIN statement, variables
that appear before it in the list are set to that value initially. (If you assign different
initial values to the same variable by naming it more than once in a RETAIN statement,
SAS uses the last value.) You can also use RETAIN to assign an initial value other than
the default value of 0 to a variable whose value is assigned by a sum statement.
SAS Training 90
Selected SAS Statements
Statement RETAIN Examples :
This RETAIN statement retains the values of variables MONTH1 through MONTH5 from one iteration of the
DATA step to the next:
retain month1-month5;
This RETAIN statement retains the values of nine variables and sets their initial values:
retain month1-month5 1 year 0 a b c 'XYZ';
The values of MONTH1 through MONTH5 are set initially to 1; YEAR is set to 0; variables A, B, and C are each
set to the character value
XYZ.
This RETAIN statement assigns the initial value 1 to the variable MONTH1 only:
retain month1-month5 (1);
Variables MONTH2 through MONTH5 are set to missing initially.
• Observations are ordered by the name of the month in which the expenses were accrued:
by month notsorted;
SAS Training 91
Selected SAS Statements
OUTPUT : Writes the current observation to a SAS data set
Syntax
OUTPUT<data-set-name(s)>;
Without Arguments
Using OUTPUT without arguments causes the current observation to be written to all data
sets that are named in the DATA statement.
Arguments
data-set-name
specifies the name of a data set to which SAS writes the observation.
Details:
The OUTPUT statement tells SAS to write the current observation to a SAS data set immediately, not
at the end of the DATA step. If no data set name is specified in the OUTPUT statement, the
observation is written to the data set or data sets that are listed in the DATA statement.
SAS Training 92
Selected SAS Statements
Statement OUTPUT Examples :
These examples show how you can use an OUTPUT statement:
SAS Training 93
Selected SAS Statements
PUT : Writes lines to the SAS log, to the SAS procedure output file, or to an
external file that is specified in the most recent FILE statement
Syntax
PUT <specification(s)><_ODS_><@|@@>;
Without Arguments
The PUT statement without arguments is called a null PUT statement.
releases an output line that is being held by a previous PUT statement with a trailing @.
SAS Training 94
Selected SAS Statements
Global Statements
LIBNAME : Associates or disassociates a SAS
data library with a libref (a shortcut name); clears
one or all librefs; lists the characteristics of a SAS
data library; concatenates SAS data libraries;
implicitly concatenates SAS catalogs.
Syntax
1. LIBNAME libref <engine> 'SAS-data-library'
< options > <engine/host-options>;
SAS Training 95
Selected SAS Statements
Arguments
libref
is a shortcut name or a "nickname" for the aggregate storage location
where your SAS files are stored. It is any SAS name when you are assigning
a new libref. When you are disassociating a libref from a SAS data library or
when listing attributes, specify a libref that was previously assigned.
'SAS-data-library'
must be the physical name for the SAS data library. The physical name
is the name that is recognized by the operating environment. Enclose the
physical name in single or double quotation marks.
library-specification
is two or more SAS data libraries, specified by physical names, previously-
assigned librefs, or a combination of the two. Separate each specification with
either a blank or a comma and enclose the entire list in parentheses.
SAS Training 96
Selected SAS Statements
engine
is an engine name.
Details:
The association between a libref and a SAS data library lasts only
for the duration of the SAS session or until you change it or
discontinue it with another LIBNAME statement. The simplest form
of the LIBNAME statement specifies only a libref and the physical
name of a SAS data library:
SAS Training 97
Selected SAS Statements
Statement LIBNAME Examples :
Assigning and Using a Libref
This example assigns the libref SALES to an aggregate storage location that is
specified in quotation marks as a physical pathname. The DATA step creates
SALES.QUARTER1 and stores it in that location. The PROC PRINT step
references it by its two-level name, SALES.QUARTER1
libname sales 'SAS-data-library';
data sales.quarter1;
infile 'your-input-file;
input salesrep $20. +6 jansales febsales
marsales;
run;
proc print data=sales.quarter1;
run;
SAS Training 98
Selected SAS Statements
TITLE : Specifies title lines for SAS output
Syntax
TITLE <n> <'text' | "text">;
Without Arguments
Using TITLE without arguments cancels all existing titles.
Arguments
n
specifies the relative line that contains the title line.
'text' | "text"
specifies text that is enclosed in single or double quotation marks.
Details:
A TITLE statement takes effect when the step or RUN group with which it is associated executes. Once you
specify a title for a line, it is used for all subsequent output until you cancel the title or define another title for
that line. A TITLE statement for a given line cancels the previous TITLE statement for that line and for all lines
with larger n numbers.
SAS Training 99
Selected SAS Statements
Statement TITLE Examples :
This statement suppresses a title on line n and all lines after it:
titlen;
renaming variables
selecting only the first or last n observations for processing
dropping variables from processing or from the output data set
specifying a password for a SAS data set.
(option-1=value-1<. . . option-n=value-n>)
Details : If the option is associated with an input data set, the variables are
not available for processing. If the DROP= data set option is associated with
an output data set, SAS does not write the variables to the output data set,
but they are available for processing.
Details : If the KEEP= data set option is associated with an input data set, only
those variables that are listed after the KEEP= data set option are available
for processing. If the KEEP= data set option is associated with an output data
set, only the variables listed after the option are written to the output data
set, but all variables are available for processing.
In this example, only IDNUM and SALARY are read from PAYROLL, and they
are the only variables in PAYROLL that are available for processing:
data bonus;
set payroll(keep=idnum salary);
bonus=salary*1.1;
run;
Details : If you use the RENAME= data set option when you create a data set,
the new variable name is included in the output data set. If you use RENAME=
on an input data set, the new name is used in DATA step programming
statements.
data two(rename=(x=keys));
set one;
z=x+y;
run;
Details : The FIRSTOBS= data set option is valid when an existing SAS data
set is read. You cannot use this option when a WHERE statement or
WHERE= data set option is specified in the same DATA or PROC step.
<$>format<w>.<d>
where
$
indicates a character format; its absence indicates a numeric format.
format
names the format.
w
specifies the format width, which for most formats is the number of
columns in the output data.
d
Syntax Description
w
specifies the width of the output field.
XYZ XYZ
Syntax Description
w
specifies the width of the output field. You can specify a
number or a column range.
Values Results
----+----1----+----2
#Cary Cary
Tokyo Tokyo
Values Results
----+----1----+
1254.71 $1,254.71
<$>informat<w>.<d>
where
$
indicates a character format; its absence indicates a numeric format.
informat
names the informat.
w
specifies the informat width, which for most informats is the number of
columns in the input data.
d
specifies an optional decimal scaling factor in the numeric informats.SAS
divides the input data by 10 to the power of d.
Syntax Description
w
specifies the width of the input field.
Details:The $CHARw. informat does not trim leading and trailing blanks or
convert a single period in the input data field to a blank before storing
values. If you use $CHARw. in an INFORMAT or ATTRIB statement within a
DATA step to read list input, then by default SAS interprets any blank
embedded within data as a field delimiter, including leading blanks.
XYZ XYZ##
XYZ #XYZ#
. ##.##
X YZ #X#YZ
XYZ XYZ##
XYZ XYZ##
X YZ X#YZ#
Options: Obs=n
run;
Remember:
Robert 15 78 Linda 13 55
Stephen 17 72
Philip 14 70
Andrew 15 67
Robert 15 78
Philip 14 70
Andrea 16 60
Linda 13 55
Stephen 17 72
CONT….
SAS Training 169
Syntax
CREATE TABLE table-name (column-definition <,column-
definition>...);
(column-specification , ...<constraint-specification > ,...) ;
DROP TABLE table-name <,table-name>...;
DROP VIEW view-name <,view-name>...;
INSERT INTO table-name|sas/access-view|proc-sql-
view<(column<,column>...)>
VALUES (value<,value>...)
<VALUES (value <,value>...)>...;
CONT….
SAS Training 170
Syntax
SELECT <DISTINCT> object-item <,object-item>...
FROM from-list
<WHERE sql-expression>
<GROUP BY group-by-item
<,group-by-item>...>
<HAVING sql-expression>
<ORDER BY order-by-item
<,order-by-item>...>;
the following:
• report-item(s)
• report-item-1, report-item-2 <. . . , report-item-n>
• (`header-1 ' < . . . `header-n '> report-item(s) )
• report-item=name
sales=salesmin sales=salesmax;
Start a new page after the last break line of a break located at PAGE
the beginning of the report
SAS Training 207
Example
Input Data set
compute newprice;
newprice = _c3_ - _c5_;
endcomp;
Syntax :
DATEw.
Syntax
DDMMYYw.
Syntax
DATEw.
Syntax
DDMMYYw.
Syntax :
DATE()
tday=date();
Put tday ddmmyy8.;
Syntax :
TODAY()
Syntax :
YEAR(date)
Syntax :
MONTH(date)
Syntax :
DAY(date)
Syntax :
QTR(date)
Syntax :
WEEKDAY(date)
Syntax :
MDY(month,day,year)
Eg:
%let macname = Test;
Eg:
%macro mac1;
data temp1;
set temp;
x= ‘Hello’;
run;
%mend mac1;
%mac1;
%macro datcrt;
data cake1;
set train.cake;
run;
%macro datcrt;
%datcrt;
Eg:
%macro mac2 (dest= , src = );
data &src;
set &dest;
x= ‘Hello’;
run;
You invoke the macro by providing values for the parameters, as follows:
%Let
iterative %DO statement
%GLOBAL statement
%INPUT statement
INTO clause of the SELECT statement in SQL
%LOCAL statement
%MACRO statement
SYMPUT routine
%WINDOW statement.
SAS Training 253
Using Macro Variables
Eg:
DATA=PERSNL&YR.EMPLOYES, where &YR contains two characters for a year
Data = &MONTH&YR
%let name=sales;
data new&name;
set save.&name;
more SAS statements
run;
%macro app(dat=);
data new&dat;
set cake;
run;
%mend app;
%app(dat=cake);
%macro app(dat=);
data &dat.new;
set cake;
run;
%mend app;
%app(dat=cake);
Eg: %let City1 = Mumbai; %let City2 = Delhi; %let City3 = Chennai
For example, you could use the value of macro variable N to
reference a variable in the series of macro variables named CITY1
to CITY3. If N has the value 1, the reference would be to CITY1. If
the value of N is 3, the reference would be to CITY3.
%macro test(x=);
%if &x = 1 %then %do;
%put "Value of x is 1";
proc print data = train.names;
run;
%end;
%else %do;
%put "Value of x is not 1";
%end;
%mend test;
%test(x=1);
NOTE: There were 4 observations read from the data set TRAIN.NAMES.
NOTE: PROCEDURE PRINT used:
real time 0.01 seconds
cpu time 0.00 seconds
Eg: To scan for words in macro variable values, use the %SCAN
function. For example,
%let address=123 maple avenue;
%let frstword=%scan(&address,1);
Result:
address resolves to 123 maple avenue
frstword resolves to 123
• Two types of scope exist for macro variables: global and local
• Global macro variables exist for the duration of the SAS session
and can be referenced anywhere in the program--either inside or
outside a macro
• Local macro variables exist only during the execution of the macro
in which the variables are created and have no meaning outside
the defining macro.
Macro variables are stored in symbol tables, which list the macro variable
name and its value
There is a global symbol table, which stores all global macro variables.
Local macro variables are stored in a local symbol table that is created at the
beginning of the execution of a macro.
%let county=Clark;
%macro concat;
data _null_;
length longname $20;
longname="&county"||" County";
put longname;
run;
%mend concat;
%concat
The new
macro variable definition simply updates
the existing global one.
Eg:
%macro holinfo(day,date);
%let holiday=Christmas;
%put *** Inside macro: ***;
%put *** &holiday occurs on &day, &date, 1997. ***;
%mend holinfo;
%holinfo(Thursday,12/25)
As you can see from the log, the local macro variables DAY, DATE, and
HOLIDAY resolve inside the macro, but outside the macro they do not exist and
therefore do not resolve.
Data _null_;
set dept1;
call symput (‘ssn’||left(_n_),ssn);
run;
2) length key $ 8;
input code $;
key=symget(code);
Assigns the value stored in the DATA step variable CODE, which
contains a macro variable name, to the DATA step variable KEY
3) score=symget('s'||left(_n_));
Assigns the letter s and the number of the current iteration
%substr(&macvar,3,2);
%EVAL(&num1 + &num2);
Eg:
To avoid the ambiguity and correctly assign the value of PRINT, you must
mask the semicolons with the macro quoting function %STR, as follows:
%let print=%str(proc print; run;);