Syntax, WHERE clause:
PROC SQL <options>;
SELECT column-1 <,...column-n>
FROM input-tables
WHERE expression;
QUIT;
expression
can be either character
or numeric values. Character values are case sensitive and must be
enclosed in single or double quotation marks. Double quotation marks
are a SAS enhancement and typically are not allowed in database systems.
Numeric values are not enclosed in quotation marks and must be standard
numeric values. You cannot include special symbols such as commas
or dollar signs when referencing numeric values.
To reference date and
time values, use one of the following forms:
|
proc sql;
select empid, jobcode, salary, salary*0.06 as bonus
from certadv.payrollmaster
where salary<32000
order by jobcode;
quit;
proc sql outobs=10;
select flightnumber, date, destination,
boarded + transferred + nonrevenue as Total
from certadv.marchflights;
quit;
proc sql outobs=10;
select flightnumber, date, destination,
boarded + transferred + nonrevenue as Total
from certadv.marchflights
where total < 100;
quit;
ERROR: The following columns were not found in the contributing tables: total.
proc sql outobs=10;
select flightnumber, date, destination,
boarded + transferred + nonrevenue as Total
from certadv.marchflights
where calculated total < 100;
quit;
where boarded + transferred + nonrevenue <100;
proc sql outobs=10;
select flightnumber, date, destination,
boarded + transferred + nonrevenue as Total,
calculated total/2 as Half
from certadv.marchflights;
quit;
Type of Operator
|
Example
|
---|---|
comparison
|
where membertype='GOLD'
|
logical
|
where visits<=3 or status='new'
|
concatenation
|
where name=trim(last) ||', '||first |
Conditional Operator
|
Looks for These Values
|
Example
|
---|---|---|
BETWEEN-AND
|
values that occur within
an inclusive range
|
where salary between 70000 and 80000 |
CONTAINS or ?
|
values that contain
a specified string
|
where name contains 'ER' where name ? 'ER' |
IN
|
values that match one
of a list of values
|
where code in ('PT' , 'NA', 'FA')
|
IS MISSING or IS NULL
|
missing values
|
where dateofbirth is missing where dateofbirth is null |
LIKE (with %, )
|
values that match a
specified pattern
|
where address like '% P%PLACE'
|
=*
|
values that sound like
a specified value
|
where lastname=* 'Smith'
|
Syntax, BETWEEN-AND
operator:
BETWEEN value-1 AND value-2
value-1
is the value at one
end of the range.
value-2
is the value at the
other end of the range.
|
Example
|
Returns Rows That Contain
These Values
|
---|---|
where date between '01mar2018'd and '07mar2018'd In this example, the
values are specified as date constants.
|
the value of Date is
01mar2018 , 07mar2018 ,
or any date value in between |
where salary between 70000 and 80000 |
the value of Salary
is
70000 , 80000 ,
or any numeric value in between |
where salary not between 70000 and 80000 |
the value of Salary
is not between or equal to
70000 and 80000 |
Syntax, CONTAINS operator:
SQL-expression CONTAINS SQL-expression
SQL-expression ? SQL-expression
SQL-expression
is a character column,
string (character constant), or expression. A string is a sequence
of characters to be matched that must be enclosed in quotation marks.
Note: PROC SQL retrieves a row
for output no matter where the string (or second SQL-expression) occurs
within the column's (or first SQL-expression's) values. Matching is
case sensitive when making comparisons.
|
ER
. As the output shows, all rows
that contain ER
anywhere within the Name
column are displayed. proc sql; select name from certadv.frequentflyers where name contains 'ER'; quit;
Syntax, IN operator:
column IN (constant-1<,...constant-n>)
column
specifies the selected
column name.
constant-1 and constant-n
represent a list that
contains one or more specific values. The list of values must be enclosed
in parentheses and separated by either commas or spaces. Values can
be either numeric or character. Character values must be enclosed
in quotation marks.
|
Example
|
Returns Rows That Contain
These Values
|
---|---|
where jobcategory in ('PT','NA','FA')
|
the value of JobCategory
is
PT , NA ,
or FA . |
where dayofweek in (2,4,6)
|
the value of DayOfWeek
is
2 , 4 ,
or 6 . |
where chesspiece not in
('pawn','king','queen') |
the value of Chesspiece
is anything but
pawn , king ,
or queen . |
Syntax, IS MISSING or
IS NULL operator:
column IS
MISSING
column IS
NULL
column
specifies the selected
column name.
|
proc sql;
select boarded, transferred, nonrevenue, deplaned
from certadv.marchflights
where boarded is missing;
quit;
where boarded = . where flight = ' 'However, the advantage of using the IS MISSING or IS NULL operator is that you do not have to specify the data type (character or numeric) of the column.
Syntax, LIKE operator:
column LIKE 'pattern'
column
specifies the column
name.
pattern
specifies the pattern
to be matched and contains one or both of the special characters underscore
( _ ) and percent sign (%). The entire pattern must be enclosed
in quotation marks and matching is case sensitive.
|
Special Character
|
Represents This Pattern
|
---|---|
underscore ( _ )
|
any single character
|
percent sign (%)
|
any sequence of zero
or more characters
|
proc sql; select ffid, name, address from certadv.frequentflyers where address like '%P%PLACE'; quit;
'%P%PLACE'
specifies
the following sequence:
Syntax, sounds-like
(=*) operator:
SQL-expression =* SQL-expression
SQL-expression
is a character column,
string (character constant), or expression. A string is a sequence
of characters to be matched that must be enclosed in quotation marks.
|
where lastname =* 'Smith';
3.134.110.97