## Compare two columns first alphabetically?

Solved
Occasional Contributor
Posts: 7

# Compare two columns first alphabetically?

How can I compare two columns to get which one is first alphabetically? I want to put which is first in a 3rd column.

Example:

Column1 = Kelly

Column2 = Katie

Column3 would be Katie because it is first alphabetically.

dataset = Rentals

Accepted Solutions
Solution
‎12-03-2015 03:59 PM
Super User
Posts: 13,523

## Re: Compare two columns first alphabetically?

Case of letters is an issue as in this example:

``````data work.example;
informat column1 column2 \$10.;
input column1 column2;
if column1 < column2 then column3 = column1;
else column3 = column2;
datalines;
Kelly Katie
kelly Katie
Kelly katie
kelly katie
;
run;``````

If case should not be considered then you can use either UPCASE(column1) and UPCASE(column2) or LOWCASE in the comparison.

All Replies
Solution
‎12-03-2015 03:59 PM
Super User
Posts: 13,523

## Re: Compare two columns first alphabetically?

Case of letters is an issue as in this example:

``````data work.example;
informat column1 column2 \$10.;
input column1 column2;
if column1 < column2 then column3 = column1;
else column3 = column2;
datalines;
Kelly Katie
kelly Katie
Kelly katie
kelly katie
;
run;``````

If case should not be considered then you can use either UPCASE(column1) and UPCASE(column2) or LOWCASE in the comparison.

Contributor
Posts: 66

## Re: Compare two columns first alphabetically?

Borrowing from ballardw's example, you can also use "><" (min) and "<>" (max). It works differently than the min() and max() functions, which only support numeric variables.

``````data work.example;
informat column1 column2 \$10.;
input column1 column2;
min_of_col1_col2 = (column1 >< column2);
max_of_col1_col2 = (column1 <> column2);
datalines;
Kelly Katie
kelly Katie
Kelly katie
kelly katie
;
run;``````
🔒 This topic is solved and locked.