Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to do a case-insensitive DISTINCT with SAS (PROC SQL)?

Tags:

sql

sas

proc-sql

Is there a way to get the case-insensitive distinct rows from this SAS SQL query? ...

SELECT DISTINCT country FROM companies;

The ideal solution would consist of a single query.

Results now look like:

Australia
australia
AUSTRALIA
Hong Kong
HONG KONG

... where any of the 2 distinct rows is really required

One could upper-case the data, but this unnecessarily changes values in a manner that doesn't suit the purpose of this query.

like image 750
Rog Avatar asked May 29 '09 05:05

Rog


3 Answers

If you have some primary int key (let's call it ID), you could use:

SELECT country FROM companies
WHERE id =
(
    SELECT Min(id) FROM companies
    GROUP BY Upper(country)
)
like image 152
Roee Adler Avatar answered Oct 16 '22 09:10

Roee Adler


Normalizing case does seem advisable -- if 'Australia', 'australia' and 'AUSTRALIA' all occur, which one of the three would you want as the "case-sensitively unique" answer to your query, after all? If you're keen on some specific heuristics (e.g. count how many times they occur and pick the most popular), this can surely be done but might be a huge amount of extra work -- so, how much is such persnicketiness worth to you?

like image 34
Alex Martelli Avatar answered Oct 16 '22 10:10

Alex Martelli


A non-SQL method (really only a single step as the data step just creates a view) would be:


data companies_v /view=companies_v;
  set companies (keep=country);
  _upcase_country = upcase(country);
run;

proc sort data=companies_v out=companies_distinct_countries (drop=_upcase_country) nodupkey noequals;
  by _upcase_country;
run;
like image 2
Simon Nickerson Avatar answered Oct 16 '22 10:10

Simon Nickerson