Previous Page | Next Page

Working with Numeric Variables

Comparing Numeric Variables

Often in a program you need to know if variables are equal to each other, or if 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 you can use for variable comparisons.

Logical Operators
Symbol Mnemonic Equivalent Logical Operation
= eq equal
¬=, ^=, ~= ne not equal to ( the ¬=, ^=, or ~= symbol, depending on your keyboard)
> gt greater than
>= ge greater than or equal to
< lt less than
<= le less than or equal to

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.

options pagesize=60 linesize=80 pageno=1 nodate;
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 shows the tours that are less than $2000 in total cost:

Comparing Numeric Variables

                              Tours $2000 or Less                              1

                                        Air    Land    Total
       Obs    Country        Nights    Cost    Cost     Cost     Vendor

        1     Greece           12         .     748        .    Express 
        2     Ireland           7       787     628     1415    Express 
        3     Venezuela         9       426     505      931    Mundial 
        4     Italy             8       852     598     1450    Express 
        5     Switzerland       9       816     834     1650    Tour2000
        6     Brazil            8       682     610     1292    Almeida 

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:

options pagesize=60 linesize=80 pageno=1 nodate;
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:

Multiple Comparisons in a Condition

                              Tours $2000 or Less                              1

                                               Total
               Obs    Country        Nights     Cost     Vendor

                1     Ireland           7       1415    Express 
                2     Venezuela         9        931    Mundial 
                3     Italy             8       1450    Express 
                4     Switzerland       9       1650    Tour2000
                5     Brazil            8       1292    Almeida 

Notice that Greece is no longer included in the tours for under $2000.

Previous Page | Next Page | Top of Page