Often in a program, you need to know whether variables
are equal to each other, or whether they are greater than or less
than each other. To compare two numeric variables, you can write an
IF-THEN/ELSE statement using logical operators.
The following table
lists some of the logical operators that you can use for variable
comparisons.
Table 8.3 Logical Operators
|
|
|
|
|
|
|
|
not equal to ( the ¬=,
^=, or ~= symbol, depending on your keyboard)
|
|
|
|
|
|
|
|
|
|
|
|
|
In this example, the
total cost of each tour in the POPULARTOURS data set is compared to
2000 using the greater-than logical operator (gt). If the total cost
of the tour is greater than 2000, the tour is excluded from the data
set. The resulting data set TOURSUNDER2K contains tours that are
$2000 or less.
data toursunder2K;
set mylib.populartours;
TotalCost = AirCost + LandCost;
if TotalCost gt 2000 then delete;
run;
proc print data=toursunder2K;
var Country Nights AirCost Landcost TotalCost Vendor;
title 'Tours $2000 or Less';
run;
The following output
displays the tours that are less than $2000 in total cost:
Display 8.4 Comparing Numeric Variables
The TotalCost value
for Greece is a missing value because any calculation that includes
a missing value results in a missing value. In a comparison, missing
numeric values are lower than any other numeric value.
If you need to compare
a variable to more than one value, you can include multiple comparisons
in a condition. To eliminate tours with missing values, a second comparison
is added:
data toursunder2K2;
set mylib.populartours;
TotalCost = AirCost + LandCost;
if TotalCost gt 2000 or Totalcost = . then delete;
run;
proc print data=toursunder2K2;
var Country Nights TotalCost Vendor;
title 'Tours $2000 or Less';
run;
The following output
displays the results:
Display 8.5 Multiple Comparisons in a Condition
Notice that Greece is
no longer included in the tours for under $2000.