BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Mattress58
Fluorite | Level 6

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

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

 

View solution in original post

2 REPLIES 2
ballardw
Super User

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.

 

hbi
Quartz | Level 8 hbi
Quartz | Level 8

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. Robot Happy

 

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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 4026 views
  • 2 likes
  • 3 in conversation