Вы находитесь на странице: 1из 285

What is SAS?

Statistical Analysis Software


developed by SAS Institute in Cary, NC

Main Uses of SAS

data entry, retrieval, and management report writing and graphics statistical an
d mathematical analysis business planning, forecasting, and decision support ope
rations research and project management quality improvement applications develop
ment.
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 prog
ram

Output Where result appear after submitting programs

SAS Training
2
Program Editor Window
Explore r and Results Window
Write your code in this window
SAS Training
3
Log Window
Log Window View the Log Created by the Program Execution
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
ID
1 2 53 54 55 56 57 58 Mary
Variables N HT AME 42 Lucy
Tom Dan Tim 46 43 45 42 48
WT
41 54 . 56 48 43
Observation s
3 4 5 6
•ID, HT and WT are Numeric Variables •NAME is a Character Variable •Character Variable
s if blank are represented by a space •Numeric Variables if blank are represented
by a ‘.’ SAS Training 7
Building Blocks of a SAS Program
A SAS program is constructed from two building blocks 2) DATA Step 3) PROC Step
A typical SAS program starts with a DATA step to create a SAS dataset and then p
asses on to the PROC step to do the analysis.
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 & g
raphs
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; PROC PRINT DATA
= distance; RUN;
PROC Step

Any combination of Data Step and/or Procedures may be used Run statement should
be used throughout the program
SAS Training 10
Difference Between a DATA step and a PROC step
DATA steps steps

PROC
Begin with PROC
Begin with DATA statements statements
Read and modify data Perform specific analysis or function Note: The table is no
t meant to imply that PROC can never create Produce results or report SAS Create
a (some do), setthat DATA step can never create data sets SAS data or

reports (they can) But it is much easier to write SAS programs if one can unders
tand 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 fir
st observation and runs it all the way through the data step (line by line) befo
re looping back to pick up the second observation SAS sees one observation at a
time.
DATA step Line 1 Line 2 Line 3 Line 4 Line 5 SAS Training Output data set Observ
ation 1 Observation 2 Observation 3
Input data set Observation 1 Observation 2 Observation 3
12
Rules for SAS programs

Every SAS statement end with a semicolon. e.g. Data test; Names must be 8 charac
ters or fewer in length. e.g. valid names:- distance invalid names is distance_a
Names must be start with letter or an underscore (_). Valid Names are e.g. dist
ance, _abc are valid. Names can contain only letters, numerals and the underscor
e (_). No %$!*&#@ please.
SAS Training
13
Rules for SAS program Statements
SAS program statements can Be in upper or lower case Continue on the next line B
e on the same line as other statements Start in any column
SAS Training
14
READING DATA FROM EXTERNAL FILES
SAS can read data and create a data set from external files like txt, csv, etc.
Basic Code Structure DATA dataset-name; INFILE file-specification <options> <hos
t-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 usi
ng DATALINES;
Basic Code Structure
DATA dataset-name; INPUT <specification(s)><@|@@>; DATALINES; ………….. …………..’ ; Run;
SAS Training 16
INPUT
Describes the arrangement of values in the input data record and assigns input v
alues to the corresponding SAS variables
INPUT <specification(s)><@|@@>;
SAS Training
17
INPUT (SPECIFICATIONS)
variable - names a variable that is assigned input values $ - indicates to store
the variable value as a character value rather than as a numeric value. pointer
-control - moves the input pointer to a specified line or column in the input bu
ffer. informat. - specifies an informat to use to read the variable value
SAS Training 18
EXAMPLE 1 (EXTERNAL FILE)
Dat.txt in C:/ Mary 24 Suzan 34
Data newdata; Infile ‘C:/dat.txt’; Input name $ age; Run;
Resulting SAS data set (newdata)
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 th
e corresponding SAS variables Syntax: INPUT variable <$> start-column <-end-colu
mn> <.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 com
ma5. +5 mar comma5.; run;
It can read these input data records: ----+----1----+----2----+----3----+----4 t
rucks 1,382 2,789 3,556 vans 1,265 2,543 3,987 sedans 2,391 3,011 3,658
SAS Training 24
INPUT TYPES
INPUT, List : Scans the input data record for input values and assigns them to t
he 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 r
ed 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-toone correspon
dence between the observations in each of the data sets, list the data sets to b
e joined on a merge statement.
SAS Training
28
Match Merge

When there is not an exact one-toone correspondence between data sets to be merg
ed, the variables to use to identify matching observations can be specied on a b
y statement. The data sets being merged must be sorted by the variables specifie
d 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 * IN option indicates whic
h data set record came from;
DATA new; MERGE old1 (IN=INOLD1) old2 (IN=IN0LD2); BY NAME IF INOLD1 and INOLD2;
RUN;
* If statement keeps only those records that are in both data sets;
SAS Training
33
SAS Functions

Definition of Functions
A SAS function performs a computation or system manipulation on arguments and re
turns a value. Most functions use arguments supplied by the user, but a few obta
in their arguments from the operating environment. In base SAS software, you can
use SAS functions in DATA step programming statements, in a WHERE expression, i
n macro language statements, in PROC REPORT, and in Structured Query Language (S
QL).
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. argumen
t can be a variable name, constant, or any SAS expression, including another fun
ction. The number and kind of arguments allowed are described with individual fu
nctions. 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-u
nit is numeric and nonnegative.
Details : The ROUND function returns a value rounded to the nearest round-off un
it. 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 Statement var1=223.456; x=round(var1,1); put x 9.5
; var2=223.456; x=round(var2,.01); put x 9.5; x=round(223.456,100); put x 9.5; x
=round(223.456); put x 9.5; x=round(223.456,.3); put x 9.5; Results 223.00000 22
3.46000 200.00000 223.00000 223.33333
SAS Training
37
Selected SAS Functions

SUM : Returns the sum of the nonmissing arguments.


Syntax SUM(argument,argument, ...) Arguments argument is numeric. The argument l
ist can consist of a variable list, which is preceded by OF.
SAS Training
38
Selected SAS Functions
Function SUM Examples : SAS Statement x1=sum(4,9,3,8); x2=sum(4,9,3,8,.); x3=sum
(of x1-x2); x4=sum(of x1-x3, 5); y1=20; y2=30; x5=sum(of y:); Results 24 24 48 1
01 50
SAS Training
39
Selected SAS Functions

MEAN : Returns the arithmetic mean (average)


Syntax
MEAN(argument,argument, . . .) Arguments argument is numeric. At least one argum
ent is required. The argument list may consist of a variable list, which is prec
eded by OF. Examples : SAS Statement Results x1=mean(2,.,.,6); x2=mean(1,2,3,2);
x3=mean(of x1-x2); 4 2 3
40
SAS Training
Selected SAS Functions

INT : Returns the integer value.


Syntax INT(argument) Arguments argument is numeric. Details The INT function ret
urns 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(argum
ent). 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 : SAS Statement var1=2.1; x=int(var1); put x=; var2=-2.4;
y=int(var2); put y=; a=int(3); put a=; b=int(-1.6); put b=; Results 2
-2
3 -1
SAS Training
42
Selected SAS Functions

LAG : Returns values from a queue.


Syntax LAG<n>(argument) Arguments n specifies the number of lagged values. argum
ent is numeric or character. Details The LAG functions, LAG1, LAG2, . . . , LAG1
00 return values from a queue. LAG1 can also be written as LAG. A LAGn 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 n
odate pageno=1; data one; input X @@; Y=lag1(x); Z=lag2(x); datalines; 123456; p
roc 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 Obs 1 2 3 4 5 6 X 1 2 3 4 5 6 Y . 1 2 3 4 5 Z . .
1 2 3 4 1
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. Deta
ils : The UPCASE function copies a character argument, converts all lowercase le
tters to uppercase letters, and returns the altered value as a result.
SAS Training
46
Selected SAS Functions
Function UPCASE Examples : SAS Statement name=upcase( John B. Smith ); put name;
Results JOHN B. SMITH
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. Det
ails : 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 : SAS Statement x= INTRODUCTION ; y=lowcase(x); put y;
Results introduction
SAS Training
49
Selected SAS Functions

SUBSTR (left of=): Replaces character value contents


Syntax SUBSTR(argument,position<,n>)=characters-to-replace Arguments argument sp
ecifies a character variable. position specifies a numeric expression that is th
e beginning character position. n specifies a numeric expression that is the len
gth of the substring that will be replaced. characters-to-replace specifies a ch
aracter 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 state
ment, SAS places the value of argument with the expression on right side. SUBSTR
replaces n characters starting at the character you specify in position. Functi
on SUBSTR(left of =) Examples : SAS Statement a= KIDNAP ; substr(a,1,3)= CAT ; p
ut a; b=a; substr(b,4)= TY ; put b; Results CATNAP
CATTY
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 v
alid SAS variable name. argument specifies any SAS character expression. positio
n specifies a numeric expression that is the beginning character position. n spe
cifies 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 ob
tains its length from the length of argument. Function SUBSTR(right of =) Exampl
es : SAS Statement Results ----+----1----+----2 date= 06MAY98 ; month=substr(dat
e,3,3); year=substr(date,6,2); put @1 month @5 year; MAY 98
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 wo
rd in the character string you want SCAN to select. delimiters specifies a chara
cter expression that produces characters that you want SCAN to use as word separ
ators 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. Function SCAN Examples : SAS Statement arg= ABC.DEF(X=Y) ; word=scan(arg,3
); put word; word=scan(arg,-3); put word; Results X=Y
ABC
SAS Training
55
Selected SAS Functions

TRIM: Removes trailing blanks from character expressions and returns one blank i
f 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 re
turns the trimmed argument as a result. If the argument is blank, TRIM returns o
ne blank. TRIM is useful for concatenating because concatenation does not remove
trailing blanks. Assigning the results of TRIM to a variable does not affect th
e length of the receiving variable. If the trimmed value is shorter than the len
gth 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 : 2. Removing Trailing Blanks These statements and this d
ata line produce these results: data test; input part1 $ 1-10 part2 $ 11-20; has
blank=part1||part2; noblank=trim(part1)||part2; put hasblank; put noblank; datal
ines; 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 ch
aracters 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 acce
pts depends on the operating environment you use to run SAS. There is no functio
nal difference between using several pairs of short arguments, or fewer pairs of
longer arguments. Function TRANSLATE Examples : SAS Statement x=translate( XYZW
, AB , VW ); put x; Results XYZB
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 ri
ght-most nonblank character in the argument. If the value of the argument is mis
sing, 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 i
n the SAS log that the numeric values have been converted to character values.
SAS Training
60
Selected SAS Functions
Function LENGTH Examples : SAS Statements len=length( ABCDEF ); put len; Results
6
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 char
acter expression. Details : The INDEX function searches source, from left to rig
ht, 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 Statements a= ABC.DEF (X=Y) ; b= X=Y ; x=index(a,b
); put x; Results 10
SAS Training
63
SAS Statements

Definition of Statements
A SAS statement is a series of items that may include keywords, SAS names, speci
al characters, and operators. All SAS statements end with a semicolon. A SAS sta
tement either requests SAS to perform an operation or gives information to the s
ystem. There are two kinds of SAS statements:

those used in DATA step programming those that are global in scope and can be us
ed 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 i
s not written to a data set, and SAS returns immediately to the beginning of the
DATA step for the next iteration. Details : The DELETE statement is often used
in a THEN clause of an IF-THEN statement or as part of a conditionally executed
DO group.
SAS Training
65
Selected SAS Statements
Statement DELETE Examples : Example 1: Using the DELETE Statement as Part of an
IF-THEN Statement When the value of LEAFWT is missing, the current observation i
s deleted: if leafwt=. then delete; Example 2: Using the DELETE Statement to Sub
set Raw Data data topsales; infile file-specification; input region office produ
ct yrsales; if yrsales<100000 then delete; run;
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. Details : The DO statement is the simplest form
of DO group processing. The statements between the DO and END statements are ca
lled a DO group. You can nest DO statements within DO groups.
SAS Training 67
Selected SAS Statements
Statement DO Examples : In this simple DO group, the statements between DO and E
ND are performed only when YEARS is greater than 5. If YEARS is less than or equ
al to 5, statements in the DO group do not execute, and the program continues wi
th 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 t
he SAS data sets that are created within the same DATA step and can appear anywh
ere in the step. The variables in the DROP statement are available for processin
g 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 s
tatements 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 state
ment: drop time shift batchnum; drop grade1-grade20; In this example, the variab
les PURCHASE and REPAIR are used in processing but are not written to the output
data set INVENTRY: data inventry; drop purchase repair; infile file-specificati
on; 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 va
riables to write to the output data set. Details : The KEEP statement causes a D
ATA step to write only the variables that you specify to one or more SAS data se
ts. 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 stateme
nt: keep name address city state zip phone; keep rep1-rep5; This example uses th
e KEEP statement to include only the variables NAME and AVG in the output data s
et. 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-specif
ication; 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 >; LABEL variable-1
= . . . <variable-n= >; Arguments variable names the variable that you want
to label label specifies a label of up to 256 characters, including blanks.
removes a label from a variable. Enclose a single blank space in quotation mar
ks to remove an existing label.
SAS Training
73
Selected SAS Statements
Details Using a LABEL statement in a DATA step permanently associates labels wit
h variables by affecting the descriptor information of the SAS data set that con
tains the variables. You can associate any number of variables with labels in a
single LABEL statement.
Statement LABEL Examples :

• • • •
Specifying Labels Here are several LABEL statements: label compound= Type of Dru
g ; label date="Today s Date "; label n= Mark s Experiment Number ; label score
1="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-specific
ation is a required argument and has the form where variable names one or more v
ariables that are to be assigned a length. This includes any variables in the DA
TA 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 SAS Training 76 bytes used for storing variable values.
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 vari
able depends on whether the variable is numeric or character how the variable wa
s 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=newname-n>; Arguments old-
name specifies the name of a variable or variable list as it appears in the inpu
t data set, or in the current DATA step for newly created variables. new-name sp
ecifies the name or list to use in the output data set. Details The RENAME state
ment allows you to change the names of one or more variables, variables in a lis
t, 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 s
tatements for the current DATA step. RENAME SAS Training 78 applies to all outpu
t data sets.
Selected SAS Statements
Statement RENAME Examples :

These examples show the correct syntax for renaming variables using the RENAME s
tatement
• • •

rename street=address; rename time1=temp1 time2=temp2 time3=temp3; rename name=F


irstname score1-score3=Newscore1-Newscore3; This example uses the old name of th
e variable in program statements. The variable Olddept is named Newdept in the o
utput 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>; Argument
s where-expression is an arithmetic or logical expression that generally consist
s 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 :

Basic WHERE Statement Usage This DATA step produces a SAS data set that contains
only observations from data set CUSTOMER in which the value for NAME begins wit
h Mac and the value for CITY is Charleston or Atlanta data testmacs; set custome
r; where substr(name,1,3)= Mac and (city= Charleston or city= Atlanta ); run;
Using Operators Available Only in the WHERE Statement • Using BETWEEN-AND: where e
mpnum between 500 and 1000; • Using CONTAINS: where company ? bay ; where company
contains bay ;
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 a
ny SAS expression and is a required argument. statement can be any executable SA
S statement or DO group.
SAS Training
82
Selected SAS Statements
Details :SAS evaluates the expression in an IF-THEN statement to produce a resul
t that is either nonzero, zero, or missing. A nonzero and nonmissing result caus
es 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 optio
nal ELSE SAS Training 83
Selected SAS Statements
Statement IF-THEN\ELSE Examples : These examples show different ways of specifyi
ng the IF-THEN/ELSE statement • if x then delete;

if status= OK and type=3 then count+1; if age ne agecheck then delete; if x=0 t
hen 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 variab
les
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 l
argest 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 v
ariables are referred to as BY variables for the current DATA or PROC step. NOTS
ORTED specifies that observations with the same BY value are grouped together bu
t are not necessarily sorted in alphabetical or numeric order. Details :In a DAT
A Step the BY statement applies only to the SET, MERGE, MODIFY, or UPDATE statem
ent that precedes it in the DATA step, and only one BY statement can accompany e
ach of these statements in a DATA step In PROC step you can specify the BY state
ment 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 in ascending order of the variable DEPT: by dept;

Specifying Sort Order


Observations are in ascending order of SALESREP and, within each SALESREP value,
in descending order of the values of JANSALES: by salesrep descending jansales;
BY-Processing with Nonsorted Data Observations are ordered by the name of the m
onth 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, t
he RETAIN statement causes the values of all variables that are created with INP
UT or assignment statements to be retained from one iteration of the DATA step t
o 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, numeri
c 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 a
ssigned values by an INPUT or assignment statement to missing before each iterat
ion of the DATA step. Use a RETAIN statement to specify initial values for indiv
idual 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 t
hat value initially. (If you assign different initial values to the same variabl
e by naming it more than once in a RETAIN statement, SAS uses the last value.) Y
ou can also use RETAIN to assign an initial value other than the default value o
f 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 variable
s 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 initia
l values: retain month1-month5 1 year 0 a b c XYZ ; The values of MONTH1 throug
h MONTH5 are set initially to 1; YEAR is set to 0; variables A, B, and C are eac
h set to the character value XYZ. This RETAIN statement assigns the initial valu
e 1 to the variable MONTH1 only: retain month1-month5 (1); Variables MONTH2 thro
ugh MONTH5 are set to missing initially.
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 argument
s causes the current observation to be written to all data sets that are named i
n the DATA statement. Arguments data-set-name specifies the name of a data set t
o which SAS writes the observation. Details: The OUTPUT statement tells SAS to w
rite the current observation to a SAS data set immediately, not at the end of th
e DATA step. If no data set name is specified in the OUTPUT statement, the obser
vation is written to the data set or data sets that are listed in the DATA state
ment.
SAS Training 92
Selected SAS Statements
Statement OUTPUT Examples : These examples show how you can use an OUTPUT statem
ent:

/* writes the current observation */ /* to a SAS data set */ output; /* writes t


he current observation */ /* when a condition is true */
if deptcode gt 2000 then output;

/* writes an observation to data */ /* set MARKUP when the PHONE */ /* value is


missing */ if phone=. then output markup;
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 rece
nt FILE statement
Syntax PUT <specification(s)><_ODS_><@|@@>; Without Arguments The PUT statement
without arguments is called a null PUT statement. The null PUT statement writes
the current output line to the current file, even if the current output line is
blank. releases an output line that is being held by a previous PUT statement wi
th a trailing @.
SAS Training
94
Selected SAS Statements

Global Statements

LIBNAME : Associates or disassociates a SAS data library with a libref (a shortc


ut name); clears one or all librefs; lists the characteristics of a SAS data lib
rary; concatenates SAS data libraries; implicitly concatenates SAS catalogs.
Syntax 1. LIBNAME libref <engine> SAS-data-library < options > <engine/host-op
tions>;
SAS Training
95
Selected SAS Statements
Arguments libref is a shortcut name or a "nickname" for the aggregate storage lo
cation where your SAS files are stored. It is any SAS name when you are assignin
g 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-dat
a-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 physic
al name in single or double quotation marks. library-specification is two or mor
e 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 da
ta 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 LIBN
AME statement specifies only a libref and the physical name of a SAS data librar
y:
SAS Training
97
Selected SAS Statements

Statement LIBNAME Examples : Assigning and Using a Libref This example assigns t
he 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, SALE
S.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.qu
arter1; run;
SAS Training 98
Selected SAS Statements

TITLE : Specifies title lines for SAS output


Syntax TITLE <n> < text | "text">; Without Arguments Using TITLE without argume
nts cancels all existing titles. Arguments n specifies the relative line that co
ntains the title line. text | "text" specifies text that is enclosed in single
or double quotation marks. Details: A TITLE statement takes effect when the ste
p or RUN group with which it is associated executes. Once you specify a title fo
r a line, it is used for all subsequent output until you cancel the title or def
ine another title for that line. A TITLE statement for a given line cancels the
previous TITLE statement for that line and for all lines SAS Training 99 with la
rger n numbers.
Selected SAS Statements

Statement TITLE Examples : This statement suppresses a title on line n and all l
ines after it: titlen;

These are examples of TITLE statements:


• • •
title First Draft ; title2 "Year s End Report"; title2 Year s End Report ;

These statements illustrate #BYVAL, #BYVAR, and #BYLINE.


• • •
title Quarterly Sales for #byval(site) ; title Annual Costs for #byvar2 ; titl
e Data Group #byline ;
SAS Training 100
Selected SAS Statements

FOOTNOTE : Prints up to ten lines of text at the


bottom of the procedure or DATA step output
Syntax FOOTNOTE<n > < text | "text" >; Without Arguments Using FOOTNOTE without
arguments cancels all existing footnotes. Arguments n specifies the relative li
ne to be occupied by the footnote. text | "text" specifies the text of the foo
tnote in single or double quotation marks.
SAS Training
101
Selected SAS Statements
Details: A FOOTNOTE statement takes effect when the step or RUN group with which
it is associated executes. Once you specify a footnote for a line, SAS repeats
the same footnote on all pages until you cancel or redefine the footnote for tha
t line. When a FOOTNOTE statement is specified for a given line, it cancels the
previous FOOTNOTE statement for that line and for all footnote lines with higher
numbers. Statement FOOTNOTE Examples :
These examples of a FOOTNOTE statement result in the same footnote:
• •
footnote8 "Managers Meeting"; footnote8 Managers Meeting ;
SAS Training 102
SAS Options

Definition of Options
Data set options specify actions that apply only to the SAS data set with which
they appear. They let you perform such operations as

renaming variables selecting only the first or last n observations for processin
g dropping variables from processing or from the output data set specifying a pa
ssword for a SAS data set.
SAS Training
103
Syntax of Options

Specify a data set option in parentheses after a SAS data set name. To specify s
everal data set options, separate them with spaces. (option-1=value-1<. . . opti
on-n=value-n>) These examples show data set options in SAS statements:

data scores(keep=team game1 game2 game3); proc print data=new(drop=year); set ol


d(rename=(date=Start_Date));
SAS Training
104
Selected SAS Options

DROP : Excludes variables from processing or from output SAS data sets.
Syntax DROP=variable(s) Syntax Description variable(s) lists one or more variabl
e names. You can list the variables in any form that SAS allows.
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 t
hey are available for processing.
SAS Training 105
Selected SAS Options
Option DROP Example :

Excluding Variables from Input In this example, the variables SALARY and GENDER
are not included in processing and they are not written to either output data se
t:
data plan1 plan2; set payroll(drop=salary gender); if hired< 01jan98 d then outp
ut plan1; else output plan2; run;
You cannot use SALARY or GENDER in any logic in the DATA step because DROP= prev
ents the SET statement from reading them from PAYROLL.
SAS Training
106
Selected SAS Options

KEEP : Specifies variables for processing or for writing to output SAS data sets
Syntax KEEP=variable(s) Syntax Description variable(s) lists one or more variabl
e names. You can list the variables in any form that SAS allows.
Details : If the KEEP= data set option is associated with an input data set, onl
y 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 s
et, only the variables listed after the option are written to the output data se
t, but all variables are available for processing.
SAS Training 107
Selected SAS Options
Option KEEP Example :
In this example, only IDNUM and SALARY are read from PAYROLL, and they are the o
nly variables in PAYROLL that are available for processing:
data bonus; set payroll(keep=idnum salary); bonus=salary*1.1; run;
SAS Training
108
Selected SAS Options

RENAME : Changes the name of a variable.


Syntax RENAME=(old-name-1=new-name-1 < . . . old-namen=newname-n>) Syntax Descri
ption old-name the variable you want to rename. new-name the new name of the var
iable. It must be a valid SAS name.
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.
SAS Training 109
Selected SAS Options
Option RENAME Example :

Renaming a Variable at Time of Output This example uses RENAME= in the DATA stat
ement to show that the variable is renamed at the time it is written to the outp
ut data set. The variable keeps its original name, X, during the DATA step proce
ssing:
data two(rename=(x=keys)); set one; z=x+y; run;
SAS Training
110
Selected SAS Options

FIRSTOBS : Causes processing to begin at a specified observation.


Syntax FIRSTOBS=n Syntax Description n is a positive integer that is less than o
r equal to the number of observations in the data set.
Details : The FIRSTOBS= data set option is valid when an existing SAS data set i
s read. You cannot use this option when a WHERE statement or WHERE= data set opt
ion is specified in the same DATA or PROC step.
SAS Training 111
Selected SAS Options
Option FIRSTOBS Example :

This PROC step prints the data set STUDY beginning with observation 20:
proc print data=study(firstobs=20); run;

This SET statement uses both FIRSTOBS= and OBS= to read only observations 5 thro
ugh 10 from the data set STUDY. Data set NEW contains six observations.
data new; set study(firstobs=5 obs=10); run;
SAS Training
112
Selected SAS Options

OBS : Causes processing to end with the nth observation.


Syntax OBS=n|MAX Syntax Description n specifies a positive integer that is less
than or equal to the number of observations in the data set or zero. MAX represe
nts the total number of observations in the data set. Details : This option spec
ifies the number of the last observation to process, not how many observations s
hould be processed. It is valid only when an existing SAS data set is read. Use
OBS=0 to create an empty data set that has the structure, but SAS Training 113 n
ot the attributes, of another data set.
Selected SAS Options
Option OBS Example :

In this example, the OBS= data set option in the SET statement reads in the firs
t ten observations from data set OLD:
data new; set old(obs=10); run;

This statement prints only observations 5 through 10 in data set STUDY:


proc print data=study(firstobs=5 obs=10);
SAS Training
114
Selected SAS Options

WHERE : Selects observations that meet the specified condition


Syntax WHERE=(where-expression) Syntax Description where-expression is an arithm
etic or logical expression that consists of a sequence of operators, operands, a
nd SAS functions. The expression must be enclosed in parentheses. Details : Use
the WHERE= data set option with an input data set to select observations that me
et the condition specified in the WHERE expression before SAS brings them into t
he DATA or PROC step for processing. Selecting observations that meet the condit
ions of the WHERE expression is the first operation SAS performs in each iterati
on of the DATA step.
SAS Training 115
Selected SAS Options
Option WHERE Example :

Selecting Observations from an Input Data Set This example uses the WHERE= data
set option to subset the SALES data set as it is read into another data set:
data whizmo; set sales(where=(product= whizmo )); run;

Selecting Observations from an Output Data Set This example uses the WHERE= data
set option to subset the SALES output data set:
data whizmo(where=(product= whizmo )); set sales; run;
SAS Training
116
SAS Format

Definition of Format
A format is an instruction that SAS uses to write data values. You use formats t
o control the written appearance of data values, or, in some cases, to group dat
a values together for analysis. For example, the WORDS22. format, which converts
numeric values to their equivalent in words, writes the numeric value 692 as si
x hundred ninety-two
SAS Training
117
Syntax of Format

SAS formats have the following form: <$>format<w>.<d> where $ indicates a charac
ter format; its absence indicates a numeric format. format names the format. w s
pecifies the format width, which for most formats is the number of columns in th
e output data. d specifies an optional decimal scaling factor in the numeric for
mats.
SAS Training 118
Selected SAS Formats

$CHARw: Writes standard character data.


Syntax $CHARw. Syntax Description w specifies the width of the output field.
SAS Training
119
Selected SAS Formats
Format $CHARw Example : put @7 name $char4.;
Values Results ----+----1 XYZ XYZ
SAS Training
120
Selected SAS Formats

$w: Writes standard character data.


Syntax $w. Syntax Description w specifies the width of the output field. You can
specify a number or a column range.
SAS Training
121
Selected SAS Formats
Format $w Example : put @10 name $5.; put name $ 10-15;
Values #Cary Tokyo
Results ----+----1---+----2 Cary Tokyo
*The character # represents a blank space.
SAS Training
122
Selected SAS Formats

DOLLARw.d: Writes numeric values with dollar signs, commas, and decimal points
Syntax DOLLARw.d Syntax Description w specifies the width of the output field. d
optionally specifies the number of digits to the right of the decimal point in
the numeric value. Details:The DOLLARw.d format writes numeric values with a lea
ding dollar sign, with a comma that separates every three digits, and a period t
hat separates the decimal fraction
SAS Training
123
Selected SAS Formats
Format DOLLARw.d Example : put @3 netpay dollar10.2;
Values 1254.71 Results ----+----1----+ $1,254.71
SAS Training
124
SAS Informat

Definition of Informat
An informat is an instruction that SAS uses to read data values into a variable.
Unless you explicitly define a variable first, SAS uses the informat to determi
ne whether the variable is numeric or character. SAS also uses the informat to d
etermine the length of character variables.
SAS Training
125
Syntax of Informat

SAS Informats have the following form: <$>informat<w>.<d> where $ indicates a ch


aracter format; its absence indicates a numeric format. informat names the infor
mat. w specifies the informat width, which for most informats is the number of c
olumns 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.
SAS Training 126
Selected SAS Informats

$CHARw: Reads character data with blanks.


Syntax $CHARw.
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 i
n the input data field to a blank before storing values. If you use $CHARw. in a
n INFORMAT or ATTRIB statement within a DATA step to read list input, then by de
fault SAS interprets any blank embedded within data as a field delimiter, includ
ing leading blanks.
SAS Training 127
Selected SAS Formats
Informat $CHARw Example : put @1 name $char5.;
Data lines ----+----1 XYZ XYZ . X YZ XYZ## #XYZ# ##.## #X#YZ Results
*The character # represents a blank space
SAS Training 128
Selected SAS Informats

$w: Reads standard character data.


Syntax $w. Syntax Description w specifies the width of the input field. You must
specify w because SAS does not supply a default value. Details: The $w. informa
t trims leading blanks and left aligns the values before storing the text. In ad
dition, if a field contains only blanks and a single period, $w. converts the pe
riod to a blank because it interprets the period as a missing value. The $w. inf
ormat treats two or more periods in a field as character data.
SAS Training 129
Selected SAS Informats
Format $w Example : input @1 name $5.;
Data lines ----+----1 XYZ XYZ . X YZ X#YZ# XYZ## XYZ## Results
*The character # represents a blank space
SAS Training 130
SAS Procedures

Print Format Sort Append Contents Transpose SQL


SAS Training 131
Proc Print
The PRINT procedure prints the observations in a SAS data set, using all or some
of the variables.
SAS Training
132
Syntax

PROC PRINT <option(s)BY <DESCENDING> variable-1 <...<DESCENDING> variable; VAR v


ariable(s);
Options: Obs=n
SAS Training 133
Hands-On Exercise
Create a List Report from the Cake Dataset displaying the first 10 Observations
Expected Output:
SAS Training
134
Solution
proc print data = cake (obs=10); var lastname flavor pscore; run;
SAS Training
135
User Defined Format

Proc Format The FORMAT procedure enables you to define your own informats and fo
rmats for variables.
SAS Training
136
Syntax

PROC FORMAT <option(s)>; VALUE <$>name <(formatoption(s))> value-range-set(s);


SAS Training
137
Example

Proc Format; value $genders ‘m’=“Male” ‘f’=“Female” Run; Proc Print data=customer.demo; for
ex $gender.; run;
SAS Training 138
Output
Cust_id Name Address Sex 2335 Jimmy Birmingham,UK Male 5889 Chen, Len Birmingham
,UK Female 3878 Davis, Brad Plymouth,UK Male 4553 Maria Miami USA
SAS Training 139
Hands-On Exercise
Display the values of the variable SEX in Class dataset as 1 and 2 , instead of
the default values.
SAS Training
140
Hands-On Exercise
Expected Output
SAS Training
141
Solution
proc format; value $sex M = 1 F = 2 ; run; proc print data = train.class; fo
rmat sex $sex.; run;
SAS Training 142
Proc Sort
The SORT procedure sorts observations in a SAS data set by one or more character
or numeric variables, either replacing the original data set or creating a new,
sorted data set.
SAS Training 143
Syntax
PROC SORT <option(s)> <collating-sequence-option> BY <DESCENDING> variable-1 <..
.<DESCENDING> variable-n>; Options(s) :nodupkey

SAS Training
144
Example
Account
Company Paul s Pizza Apex World Wide Electronics Garner Strickland Industries Mo
rrisville Ice Cream Delight Debt 83.00 119.95 657.22 299.98 Town Apex Apex Apex
Apex
SAS Training
145
proc sort data=account out=bytown;€ by town company; run; proc print data=bytown;
var company town debt; title Customers with Past-Due Accounts ; SAS Training ti
tle2 Listed Alphabetically
146
Output
Customers with Past-Due Accounts Listed Alphabetically within Town Obs Company T
own Debt
1 2 3 4 Apex World Wide Electronics Garner Strickland Industries Morrisville Ice
Cream Delight Paul s Pizza
SAS Training
Apex Apex Apex Apex
119.95 657.22 299.98 83.00
147
Duplicate observations
proc sort data=account out=towns nodupkey; by town; run;
€
proc print data=towns; var town company debt ; title Towns of Customers with Pa
st-Due Accounts‘; run;
SAS Training
148
Output
Towns of Customers with Past-Due Accounts
Obs Town Company Debt
1 2 3 4
Apex Garner Holly Springs Morrisville
Paul s Pizza World Wide Electronics Ice Cream Delight Strickland Industries
83.00 119.95 299.98 657.22
SAS Training
149
Hands-On Exercise
SORT DUPOBS dataset to create dataset A. sort the data by descending tourtype an
d vendor and ascending landcost Expected Output
SAS Training
150
Solution
proc sort data = dupobs ; by descending tourtype descending vendor landcost; run
;
SAS Training
151
Proc Append
The APPEND procedure adds the observations from one SAS data set to the end of a
nother SAS data set Remember: • Both data sets contained the same variables. • If Ap
pending data sets with different variables, use the FORCE option
SAS Training
152
Syntax

PROC APPEND BASE=<libref.>SAS-data-set <DATA=<libref.>SAS-data-set> <FORCE> ;


SAS Training
153
Class – 1 -2
name Andre 15 w Robert 15 Philip 14
Example
age height 67 78 70
SAS Training
Class
name ag Andrea e 16 Linda 13 heigh t 60 55 72
Stephe 17 n
154
Example

PROC APPEND BASE=class1 DATA=class2; RUN;


SAS Training
155
Output
name Andrew Robert Philip
Andrea Linda
age 15 15 14
16 13
height 67 78 70
60 55
Stephen
17
72
SAS Training
156
Hands On - Exercise
Append datasets DEPT1 and DEPT2 and create replace DEPT1 with the appended datas
et Expected Output
SAS Training
157
Solution
proc append base = dept1 data=dept2; run;
proc print data = dept1; run;
SAS Training
158
Proc Contents
The CONTENTS procedure shows the contents of a SAS data set and prints the direc
tory of the SAS data library.
SAS Training
159
Example

proc contents data=test.records; run;


SAS Training
160
Output
SAS Training
161
Hands-On Exercise
Display the Dataset structure of DUPOBS dataset
SAS Training
162
Proc Transpose
The TRANSPOSE procedure creates an output data set by restructuring the values i
n a SAS data set, transposing selected variables into observations.
Original data X Y 12 19 21 15 33 27 14 32

Z 14 19 82 99
=)
Transposed data _NAME_ COL1 COL2 COL3 COL4 X 12 21 33 14 Y 19 15 27 32 Z 14 19 8
2 99
SAS Training
163
Syntax

PROC TRANSPOSE <DATA=input-data-set> <LABEL=label> <LET> <NAME=name> <OUT=output


-data-set> <PREFIX=prefixBY <DESCENDING> variable-1 <...<DESCENDING> variable-n>
<NOTSORTED>; COPY variable(s); ID variable; IDLABEL variable; VAR variable(s);
SAS Training
164
To do this
€
Use this statement
BY COPY ID IDLABEL VAR
Transpose each BY group Copy variables directly without transposing them Specify
a variable whose values name the transposed variables Create labels for the tra
nsposed variables List the variables to transpose
SAS Training
165
Hands-On Exercise
Transpose the dataset grp_tran in the Train library to get the output as shown b
elow
SAS Training
166
Solution
proc sort data = grp_tran; by grp; run; proc transpose data = grp_tran out = res
ult (drop= _name_); by grp; run; proc print data = result; run;
SAS Training 167
Proc SQL
The SQL procedure implements Structured Query Language (SQL) for the SAS System.
SQL is a standardized, widely used language that retrieves and updates data in
tables and views based on those tables
SAS Training 168
Syntax

PROC SQL <option(s)>; ALTER TABLE table-name <constraint-clause> <,constraint-cl


ause>...>; <ADD column-definition <,column-definition>...> <MODIFY column-defini
tion <,column-definition>...> <DROP column <,column>...>;
CONT….
SAS Training 169
€CREATE TABLE table-name (column-definition <,columndefinition>...); (column-speci
fication , ...<constraint-specification > ,...) ; DROP TABLE table-name <,table-
name>...; DROP VIEW view-name <,view-name>...; INSERT INTO table-name|sas/access
-view|proc-sqlview<(column<,column>...)> VALUES (value<,value>...) <VALUES (valu
e <,value>...)>...;
Syntax
CONT….
SAS Training 170
€€
Syntax
SELECT <DISTINCT> object-item <,object-item>... FROM from-list <WHERE sql-expres
sion> <GROUP BY group-by-item <,group-by-item>...> <HAVING sql-expression> <ORDE
R BY order-by-item <,order-by-item>...>;
SAS Training
171
Example

Proc Sql; select Lname, Fname, City, State, IdNumber, Salary, Jobcode from staff
, payroll where idnumber=idnum ; Quit;
SAS Training
172
Hands-On Exercise
Create Table for Females from the Class dataset using Proc Sql. Expected Output
SAS Training
173
Solution
proc sql; create table females as select * from class where sex = F ; select *
from females; quit;
SAS Training
174
Proc GPLOT
The GPLOT procedure plots the values of two or more variables on a set of coordi
nate axes (X and Y). The coordinates of each point on the plot correspond to two
variable values in an observation of the input data set.
SAS Training 175
Syntax
PROC GPLOT <DATA=input-data-set> </option(s) >; PLOT plot-request(s) </option(s)
PLOT2 plotrequest(s) option(s)>;
SAS Training
176
Example
proc gplot data = dupobs; goptions reset = all; plot landcost*country; run;
SAS Training
177
Output
SAS Training
178
Example
proc gplot data = dupobs; goptions reset=all; symbol1 color = green value=triang
le ; symbol2 color=blue value=circle; symbol3 color=red value=square; plot landc
ost*country=vendor; run;
SAS Training
179
Output
SAS Training
180
PROC MEANS
The MEANS procedure provides data summarization tools to compute descriptive sta
tistics for variables across all observations and within groups of observations.
SAS Training
181
Syntax
PROC MEANS <option(s)> BY <DESCENDING> variable-1 <... <DESCENDING> variablen><N
OTSORTED>; CLASS variable(s) </ option(s)>; OUTPUT <OUT=SAS-data-set> <output-st
atistic-specification(s)>
SAS Training
182
PROC MEANS (OPTIONS)
Specify the number of MAXDEC= decimal places for the statistics all displayed ou
tput NOPRINT Suppress Order the values of the class variables according to the s
pecified order Limit the output statistics to the observations with the highest
_TYPE_ value
SAS Training
ORDER=
NWAY
183
PROC MEANS (BY)
Produces separate statistics for each BY group.
Example: Input Data set
proc sort data = temp; by cat; run; proc means data = temp; by cat; output out =
temp1; run;
SAS Training 184
PROC MEANS (CLASS)
Specifies the variables whose values define the subgroup combinations for the an
alysis.
SAS Training
185
PROC MEANS (NWAY)
proc means data = prod nway; class cat product; var sales; output out = prodsta
n=n sum=total; run;
SAS Training
186
PROC MEANS (OUTPUT)
Outputs statistics to a new SAS data set Input Data set
proc means data = sales; class prod; output out = temp n = Cost_n sal_n mean=Cos
t_m Sale_m; run;
SAS Training
187
PROC MEANS (CLASS)
proc means data = prod; class cat product; var sales; output out = prodsta n=n s
um=total; run;
SAS Training
188
PROC MEANS (VAR)
Identifies the analysis variables and their order in the output. If you omit the
VAR statement, PROC MEANS analyzes all numeric variables that are not listed in
the other statements. When all variables are character variables, PROC MEANS pr
oduces a simple count of observations.
SAS Training 189
PROC Summary
The SUMMARY procedure provides data summarization tools that compute descriptive
statistics for variables across all observations or within groups of observatio
ns SAS Training
190
Syntax
PROC SUMMARY <option(s)> <statistic-keyword(s)BY <DESCENDING> variable-1<...<DES
CENDING> variable-n> <NOTSORTED>; CLASS variable(s) </ option(s)>; FREQ variable
; OUTPUT <OUT=SAS-data-set><output-statistic-specification(s)> <id-group-specifi
cation(s)> <maximum-id-specification(s)> <minimum-id-specification(s)></ option(
s)> ; VAR variable(s)</ WEIGHT=weight-variable>;
SAS Training
191
Difference Between Proc Means & Proc Summary
Both PROC MEANS and PROC SUMMARY compute descriptive statistics for an entire SA
S data set. The Difference Between them :
. PROC MEANS produces subgroup statistics only when a BY statement is used and t
he input data has been previously sorted (use PROC SORT) by the BY variables PRO
C SUMMARY automatically produces statistics for all subgroups, giving you all th
e information in one run
SAS Training
192
Difference Between Proc Means & Proc Summary
. PROC MEANS produce information in the output window. PROC SUMMARY does not pro
duce any information in your output so you will always need to use the OUTPUT st
atement to create a new data set and use PROC PRINT to see the computed statisti
cs.
SAS Training
193
PROC REPORT
Overview:
The REPORT procedure combines features of the PRINT, MEANS, and TABULATE procedu
res with features of the DATA step in a single report-writing tool that can prod
uce a variety of reports
SAS Training
194
PROC REPORT (TYPES)
SAS Training
195
PROC REPORT (TYPES)
SAS Training
196
SAS Training
197
PROC REPORT (Concept)
The most important thing to figure out is the layout of the report. Once you und
erstand the layout of the report, use the COLUMN and DEFINE statements in PROC R
EPORT to construct the layout.
SAS Training
198
Typical Report Example
Input Data set
proc report data = rep nowd headline headskip; column product sales; define prod
uct / Product Name order; define sales / Sales Occured format=8.2; run;
SAS Training
199
PROC REPORT (Syntax)
PROC REPORT <option(s)>; BY <DESCENDING> variable-1 COLUMN column-specification(
s); COMPUTE LINE specification(s); . . . select SAS language elements . . . ENDC
OMP; DEFINE report-item ; SAS Training REBREAK location </ option(s)>; 200
PROC REPORT (OPTIONS)
Select the windowing or the nonwindowing environment Specify the default number
of characters for columns containing computed variables or numeric data set vari
ables Underline all column headers and the spaces between them Write a blank lin
e beneath all column headers Specify the split character Specify the number of b
lank characters between columns
SAS Training
WINDOWS| NOWINDOWS COLWIDTH=
HEADLINE HEADSKIP SPLIT= SPACING=
201
PROC REPORT (COLUMN)
Describes the arrangement of all columns and of headers that span more than one
column. • column-specification(s) is one or more of the following:
• •
• •

report-item(s)
 report-item-1, report-item-2 <. . . , report-itemn> ( header-1
< . . . header-n > report-item(s) ) report-item=name
SAS Training 202
PROC REPORT (COLUMN)
Examples • column sector manager N sales; • column sector sales,min; • column ( Indivi
dual Store Sales as a Percent of All Sales sector manager); • column manager depa
rtment sales sales=salesmin sales=salesmax;
SAS Training 203
PROC REPORT
Decide the usage of a variable in DEFINE statement these usages are • DISPLAY • ORDE
R • ACROSS • GROUP • ANALYSIS
SAS Training
204
PROC REPORT (DEFINE)
• •

• • •
DISPLAY – Do not affect the order of variables in a row ORDER – Used to change order
of variables (Ascending/Descending/Formatted) ACROSS - creates a column for eac
h value of an across variable. GROUP – Creates Groups Analysis – To calculate statis
tics Computed – Variables defined for the report not on input data set
SAS Training 205
PROC REPORT (DEFINE OPTIONS)
Assign a SAS or user-defined format to the item Order the values of a group, ord
er, or across variable according to the specified order Define the number of bla
nk characters to leave between the column being defined and the column immediate
ly to its left Define the width of the column in which PROC REPORT displays the
report item Reverse the order in which PROC REPORT displays rows or values of a
group, order, or across variable JUSTIFY Formatted Values
SAS Training
FORMAT= ORDER= SPACING=
WIDTH= DESCENDING CENTER, LEFT, RIGHT
206
PROC REPORT (BREAK)
Produces a default summary at the beginning or end of a report or at the beginni
ng and end of each BY group. Write a RBREAKthe last break line of option(s)>; bl
ank line for location </ a break SKIP
located at the beginning of the report Include a summary line as one of the brea
k lines SUMMARIZE
Start a new page after the last break line of a break located at the beginning o
f the report
SAS Training
PAGE
207
Example
Input Data set
proc report data = rep nowd headline headskip split= * ; column ( This is sales
report product N sales sales,min discount newprice); define product / Product
Name group; define sales / My*Sales format=8.2; define min / Min ; define ne
wprice / Discount Price computed; compute newprice; newprice = _c3_ - _c5_; en
dcomp; break after product / skip; run; SAS Training 208
SAS Date
SAS Date : is a value that represents the number of days between January 1, 1960
, and a specified date. SAS can perform calculations on dates ranging from A.D.
1582 to A.D. 19,900. Dates before January 1, 1960, are negative numbers; dates a
fter are 209 SAS Training
SAS Date
How SAS Converts Calendar Dates to SAS Date Values :
SAS Training
210
SAS Date
Working with SAS Dates :
The SAS System converts date values back and forth between calendar dates with S
AS language elements called formats and informats. Formats present a value, reco
gnized by SAS, such as a date value, as a calendar date in a variety of lengths
and notations. Informats read notations or a value, such as a calendar date, whi
ch may be in a variety of lengths, and then convert the data to a SAS date. SAS
Training 211
SAS Date
Example: Reading, Writing, and Calculating Date Values
data meeting; options nodate pageno=1 linesize=80 pagesize=60; input region $ mt
g : mmddyy8.; sendmail=mtg-45; datalines; N 11-24-99 S 12-28-99 E 12-03-99 W 10-
04-99 ;
SAS Training
212
SAS Date
proc print data=meeting; format mtg sendmail date9.; title When To Send Announc
ements ; run;
SAS Training
213
SAS Date
Date formats DATEw. Format: Writes date values in the form ddmmmyy or ddmmmyyyy.
Syntax : DATEw.
SAS Training
214
SAS Date
Examples
The example table uses the input value of 15415, which is the SAS date value tha
t corresponds to March 16, 2002.
SAS Training
215
SAS Date
DDMMYYw. Format : Writes date
values in the form ddmmyy or ddmmyyyy. Syntax DDMMYYw.
SAS Training
216
SAS Date
Examples : The example table uses the input value of 15415, which is the SAS dat
e value that corresponds to March 16, 2002.
SAS Training
217
SAS Date
Date Informats DATEw. : Reads date values in the form ddmmmyy or ddmmmyyyy. Synt
ax DATEw.
SAS Training
218
SAS Date
Example : input calendar_date date11.;
SAS Training
219
SAS Date
DDMMYYw. Informat :Reads date values in the form ddmmyy or ddmmyyyy. Syntax DDMM
YYw.
SAS Training
220
SAS Date
Example : input calendar_date ddmmyy10.;
SAS Training
221
SAS Date
Functions DATE Function : Returns the current date as a SAS date value Syntax :
DATE() Details : The DATE function produces the current date in the form of a SA
S date value, which is the number of days since January 1, 1960. SAS Training
222
SAS Date
Example : tday=date(); Put tday ddmmyy8.;
SAS Training
223
SAS Date
TODAY function : Returns the current date as a SAS date value. Syntax : TODAY()
Details : TODAY is identical to the DATE function. The TODAY function produces t
he current date in the form of a SAS date value, which is the number of days sin
ce January 1, 1960. SAS Training 224
SAS Date
YEAR function : Returns the year from a SAS date value. Syntax : YEAR(date) Deta
ils : The YEAR function produces a four-digit numeric value that represents the
year.
SAS Training 225
SAS Date
Example :
SAS Training
226
SAS Date
MONTH function : Returns the month from a SAS date value. Syntax : MONTH(date) D
etails : The MONTH function returns a numeric value that represents the month fr
om a SAS date value. Numeric values can range from 1 through 12.
SAS Training 227
SAS Date
Example :
SAS Training
228
SAS Date
DAY Function : Returns the day of the month from a SAS date value. Syntax : DAY(
date) Details : The DAY function produces an integer from 1 to 31 that represent
s the day of the month.
SAS Training 229
SAS Date
Example :
SAS Training
230
SAS Date
QTR function: Returns the quarter of the year from a SAS date value. Syntax : QT
R(date) Details : The QTR function returns a value of 1, 2, 3, or 4 from a SAS d
ate value to indicate the quarter of the year in which a date value falls.
SAS Training 231
SAS Date
Example :
SAS Training
232
SAS Date
WEEKDAY Function : Returns the day of the week from a SAS date value. Syntax : W
EEKDAY(date) Details : The WEEKDAY function produces an integer that represents
the day of the week, where 1=Sunday, 2=Monday, . . . , 7=Saturday. SAS Training
233
SAS Date
Example :
SAS Training
234
SAS Date
MDY Function :Returns a SAS date value from month, day, and year values. Syntax
: MDY(month,day,year)
SAS Training
235
SAS Date
Example :
SAS Training
236
INTRODUCTION TO MACROS
SAS Training
237
What is a Macro Facility?
The macro facility is a tool for extending and customizing the SAS System and fo
r reducing the amount of text you must enter to do common tasks
SAS Training
238
Replacing Text Strings Using Macro Variables
Macro variables are an efficient way of replacing text strings in SAS code The S
implest way to define a macro variable is to use the %LET statement to assign th
e macro variable a name and a value. Eg: %let macname = Test;
SAS Training 239
Replacing Text Strings Using Macro Variables
Resolve a Macro Variable Value using the ‘&’ symbol Eg: %let macname = Test; title “Th
is is a &macname” The macro processor resolves the reference to the macro variable
MACNAME, and the statement becomes Title “This is a Test”
SAS Training 240
Generating SAS Code Using Macros Macros Allow you to execute a SAS code multiple
times without compiling it. A macro definition is placed between a %MACRO state
ment and a %MEND (macro end) statement, as follows: %MACRO macro-name; SAS code
%MEND macro-name; Execute the macro using %macro-name
SAS Training 241
Generating SAS Code Using Macros Eg:
%macro mac1; data temp1; set temp; x= ‘Hello’; run; %mend mac1; %mac1; Execution of
this macro SAS Training the following produces
242
Generating SAS Code Using Macros
Execution of this macro produces the following program: data temp1; set temp; x=
‘Hello’; run;
SAS Training
243
Hands – On Exercise
Create a macro datcrt that creates a data set cake1 from train.cake
SAS Training
244
Solution
%macro datcrt; data cake1; set train.cake; run; %macro datcrt; %datcrt;
SAS Training
245
Passing Information into a Macro Using Parameters
A macro variable defined in parentheses in a %MACRO statement is a macro paramet
er Macro parameters allow you to pass information into a macro
SAS Training
246
Passing Information into a Macro Using Parameters Eg:
%macro mac2 (dest= , src = ); data &src; set &dest; x= ‘Hello’; run;
You invoke the macro by providing values for the parameters, as follows:
%mac2 (dest=temp1, src=temp) ; %mac2 (dest=temp2, src=temp); SAS Training
247
Hands – On Exercise
Create a Macro datprnt that prints the data set cake and dept1 when passed as a
macro parameter
SAS Training 248
Solution
%macro datprnt (dat= ); proc print data = &dat; run; %mend datprnt; %datprnt (da
t=cake); %datprnt (dat=dept1);
SAS Training
249
Conditionally Generating SAS Code Use %IF-%THEN-%ELSE macro statements to condit
ionally generate SAS code with a macro. Eg:
%macro test (info= , mydata = ); %if &info = print %then %do; proc print data =
&mydata; run; %end; %else %if &info = report %then %do; proc report data = &myda
ta; run; %end; %mend test;
SAS Training 250
Conditionally Generating SAS Code
%test(info=print,mydata = data1); /* Calling the Macro */ Result of the macro ex
ecution: Proc print data = data1; run;
SAS Training
251
Macro Variables



Macro variables are tools that enable you to dynamically modify the text in a SA
S program through symbolic substitution. You can assign large or small amounts o
f text to macro variables, and after that, you can use that text by simply refer
encing the variable that contains it. Macro variables defined by macro programme
rs are called user-defined macro variables. Eg: %let name = Henry Woodbridge is
Male; Those defined by the SAS System are called automatic macro variables Eg: S
ysdate,Sysday, SysProcessID, etc.
SAS Training 252
Methods to create User Defined Macro Variables

%Let iterative %DO statement %GLOBAL statement %INPUT statement INTO clause of t
he SELECT statement in SQL %LOCAL statement %MACRO statement SYMPUT routine %WIN
DOW statement. SAS Training 253
Using Macro Variables
After a macro variable is created, you typically use the variable by referencing
it with an ampersand preceding its name (&variable-name), which is called a mac
ro variable reference. These references perform symbolic substitutions when they
resolve to their value. Eg: %let dsn=Newdata;
title1 "Contents of Data Set &dsn"; data temp; set &dsn; if age>=20; run;
SAS Training 254
Using Macro Variables
if macro variable JERRY is misspelled as JERY, the following produces an unexpec
ted result: %let jerry=student; data temp; x="produced by &jery"; run; This prod
uces the following message: WARNING: Apparent symbolic reference JERY not resolv
ed. Note: Display the macro variable value using %put Eg: %put &jerry;
SAS Training 255
Combining Macro Variable References with Text
When the macro variable is appended after a text, directly reference it with the
text. When a text is appended after a macro variable, use the delimiter ‘.’ between
the macro variable reference and the text DATA=PERSNL&YR.EMPLOYES, where &YR co
ntains two characters for a year
Data = &MONTH&YR
%let name=sales; data new&name; set save.&name; more SAS statements run; The SAS
system sees it as DATA NEWSALES; SET SAVE.SALES; more SAS statements RUN;
SAS Training
256
Combining Macro Variable References with Text
Eg:
DATA=PERSNL&YR.EMPLOYES, where &YR contains two characters for a year Data = &MO
NTH&YR %let name=sales; data new&name; set save.&name; more SAS statements run;
The SAS system sees it as DATA NEWSALES; SET SAVE.SALES; more SAS statements SAS
Training
257
Hands – On Exercise
Create a data set newcake from cake by passing cake as a macro parameter
SAS Training
258
Solution
%macro app(dat=); data new&dat; set cake; run; %mend app; %app(dat=cake);
SAS Training
259
Hands – On Exercise
Create a data set cakenew from cake by passing cake as a macro parameter
SAS Training
260
Solution
%macro app(dat=); data &dat.new; set cake; run; %mend app; %app(dat=cake);
SAS Training
261
Referencing Macro Variables Indirectly
It is also useful to be able to indirectly reference macro variables that belong
to a series so that the name is determined when the macro variable reference re
solves. Eg: %let City1 = Mumbai; %let City2 = Delhi; %let City3 = Chennai For ex
ample, you could use the value of macro variable N to reference a variable in th
e series of macro variables named CITY1 to CITY3. If N has the value 1, the refe
rence would be to CITY1. If the value of N is 3, the reference would be to CITY3
. %put &city&n; /* incorrect */
SAS Training 262
Referencing Macro Variables Indirectly
% put &&city&n; /* correct */ When the macro processor encounters multiple amper
sands, its basic action is to resolve two ampersands to one ampersand. For examp
le, to append the value of &N to CITY and then reference the appropriate variabl
e name Assuming that &N contains 3, when the macro processor receives this state
ment, it performs the following steps: resolves && to & passes CITY as text reso
lves &N into 3 returns to the beginning of the macro variable reference, &CITY3,
starts resolving from the beginning again, and prints 263 the value of CITY3 wh
ich SAS Training is Chennai.
Debugging a Macro Program
Use the following Options to Debug a Macro code: 1) MLOGIC : Controls whether ma
cro execution is traced for debugging Each line generated by the MLOGIC option i
s identified with the prefix MLOGIC(macro-name) 2) MPRINT : Controls whether SAS
statements generated by macro execution are traced for debugging The MPRINT opt
ion displays the text generated by macro execution. Each SAS statement begins a
new line. Each line of MPRINT output is identified with the prefix MPRINT(macron
ame): 3) SYMBOLGEN : Controls whether the results of resolving macro variable re
ferences are displayed for debugging SYMBOLGEN displays the results in this form
: SAS Training 264
Debugging a Macro Program
options mlogic mprint symbolgen; %macro test(x=); %if &x = 1 %then %do; %put "Va
lue 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);
SAS Training 265
Debugging a Macro Program
MLOGIC(TEST): Beginning execution. MLOGIC(TEST): Parameter X has value 1 SYMBOLG
EN: Macro variable X resolves to 1 MLOGIC(TEST): %IF condition &x = 1 is TRUE ML
OGIC(TEST): %PUT "Value of x is 1" "Value of x is 1" MPRINT(TEST): proc print da
ta = train.names; MPRINT(TEST): run; NOTE: There were 4 observations read from t
he data set TRAIN.NAMES. NOTE: PROCEDURE PRINT used: real time 0.01 seconds cpu
time 0.00 seconds MLOGIC(TEST): Ending execution.
SAS Training 266
Manipulating Macro Variable Values with Macro Functions
When you define macro variables, you can include macro functions in the expressi
ons to manipulate the value of the variable before the value is stored Eg: To sc
an for words in macro variable values, use the %SCAN function. For example, %let
address=123 maple avenue; %let frstword=%scan(&address,1); Result: address reso
lves to 123 maple avenue frstword resolves to 123
SAS Training
267
Scope of Macro Variables

Every macro variable has a scope. A macro variable s scope determines how it is
assigned values and how the macro processor resolves references to it. Two types
of scope exist for macro variables: global and local Global macro variables exi
st for the duration of the SAS session and can be referenced anywhere in the pro
gram--either inside or outside a macro Local macro variables exist only during t
he execution of the macro in which the variables are created and have no meaning
outside the defining macro.
SAS Training 268



Scope of Macro Variables
Eg:
%macro A; %let Loc1 = xyz; %macro B; %let Loc2 = abc; %mend B; %mend A; LOC1 is
local to both A and B. However, LOC2 is local only to B. Macro variables are sto
red 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 vari
ables are stored in a local symbol table that SAS Training 269 is created at the
beginning of the execution of a macro.
Global Macro Variables
%let county=Clark; %macro concat; data _null_; length longname $20; longname="&c
ounty"||" County"; put longname; run; %mend concat; %concat
Calling the macro CONCAT produces the following statements: data _null_; length
longname $20; longname="Clark"||" County"; put longname; run; The PUT statement
writes the following to the SAS log: Clark County SAS Training
270
Global Macro Variables Global Macro Variables
The new macro variable definition simply updates the existing global one.
SAS Training 271
Local Macro Variables



Local macro variables are defined within an individual macro Each macro you invo
ke creates its own local symbol table. Local macro variables exist only as long
as a particular macro executes; when the macro stops executing, all local macro
variables for that macro cease to exist.
SAS Training
272
Local Macro Variables
Eg: %macro holinfo(day,date); %let holiday=Christmas; %put *** Inside macro: ***
; %put *** &holiday occurs on &day, &date, 1997. ***; %mend holinfo; %holinfo(Th
ursday,12/25) %put *** Outside macro: ***; %put *** &holiday occurs on &day, &da
te, 1997. ***; SAS Training 273
Local Macro Variables
The %PUT statements write the following to the SAS log: *** Inside macro: *** **
* Christmas occurs on Thursday, 12/25, 1997. *** *** Outside macro: *** WARNING:
Apparent symbolic reference HOLIDAY not resolved. WARNING: Apparent symbolic re
ference DAY not resolved. WARNING: Apparent symbolic reference DATE not resolved
. *** &holiday occurs on &day, &date, 1997. *** As you can see from the log, the
local macro variables DAY, DATE, and HOLIDAY resolve inside the macro, 274 but
SAS Training outside the macro they do not exist and therefore do
Local Macro Variables Local Macro Variables
SAS Training
275
Data Step Interfaces
DATA step interfaces consist of tools that enable a program to interact with the
macro facility during DATA step execution. 1) CALL SYMPUT : Assigns DATA step i
nformation to a macro variable 2) SYMGET : Returns the value of a macro variable
during DATA step execution
SAS Training
276
CALL SYMPUT
Syntax: CALL SYMPUT(macro-variable,value); Eg:
1)call symput( new , testing ); Assigns the character string testing to macro va
riable NEW 2) data team1; input position : $8. player : $12.; call symput(positi
on,player); cards; shortstp Ann pitcher Tom frstbase Bill ; This DATA step creat
es the three macro variables SHORTSTP, PITCHER, and FRSTBASE and respectively as
sign them the values ANN, TOM, and SAS Training 277 BILL.
CALL SYMPUT
3)
data team2; input position $12. player $12.; call symput( POS ||left(_n_), posit
ion); cards; shortstp Ann pitcher Tom frstbase Bill
; This form is useful for creating a series of macro variables. For example, the
CALL SYMPUT statement builds a series of macro variable names by combining the
character string POS and the left-aligned value of _N_ and assigns values to SAS
Training variables POS1, POS2, the macro 278
Hands – On Exercise
Create macro varialbes ssn1, ssn2,….sssn from ssn variable in dept1 dataset using
CALL SYMPUT
SAS Training 279
Solution
Data _null_; set dept1; call symput (‘ssn’||left(_n_),ssn); run;
SAS Training
280
SYMGET
Syntax SYMGET(argument) Eg: 1) x=symget( g ); Assign the value of the macro vari
able G to the DATA step variable X. 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 SAS Training
281
Macro Functions and Expressions
The macro functions and expressions are similar to data step functions except th
at they are used with macro variables only and are preceded by % symbol Eg: %upc
ase(&macvar); %substr(&macvar,3,2); %EVAL(&num1 + &num2); %If &x = 1 %then %do;
%end; %do i=1 %to 5; data dat&i %end;;
SAS Training
282
Macro Quoting
Macro quoting functions tell the macro processor to interpret special characters
and mnemonics as text rather than as part of the macro language. If you did not
use a macro quoting function to mask the special characters, the macro processo
r or the rest of the SAS System might give the character a meaning you did not i
ntend.
Eg:
• •


Is %sign a call to the macro SIGN or a phrase "percent sign"? Is OR the mnemonic
Boolean operator or the abbreviation for Oregon? Is the quote in O Malley an un
balanced single quotation mark or just part of the name? Is Boys&Girls a referen
ce to the macro variable &GIRLS or a group of children?
SAS Training 283
Macro Quoting
The following macro quoting functions are most commonly used: • %STR and %NRSTR • %B
QUOTE and %NRBQUOTE • %SUPERQ Eg: %let print=proc print; run;; /* ERROR */ To avoi
d the ambiguity and correctly assign the value of PRINT, you must mask the semic
olons with the macro quoting function %STR, as follows: %let print=%str(proc pri
nt; run;);
SAS Training 284

Вам также может понравиться