Desktop productivity for business analysts and programmers

nodupkey and SQL

Reply
Contributor
Posts: 25

nodupkey and SQL

Hello Team,

 

I am looking to convert below statatement of SAS in SQL 

 

proc sort data=strat.master_query nodupkey out=strat.master_query_unq; by wclientcode szaccountnumber raisedmth; run;

 

Is it like DISTINCT in SQL ?

 

I tried with 

 

SELECT DISTINCT * FROM master_query SORT BY wclientcode szaccountnumber raisedmth

 

But it is not giving correct reocrds with SQL statement.

 

Can someone please help me ?

 

Thanks In advance

Super User
Posts: 7,371

Re: nodupkey and SQL

If you do sort with nodupkey, only one record per by values is kept; if additional variables in the dataset have different values between rows, only one of those values is therefore kept, the others are discarded.

Select distinct, OTOH, looks at all variables in the select list (which happen to be all variables in the dataset when using *), and keeps multiple records per "order by" group if there are non-unique values in the other columns.

 

IMO, if a SQL distinct creates something different than a sort nodupkey, this is a sign for sloppy programming, as values may be lost with the sort.

Look if all columns from your input dataset are needed; a proper keep in the sort step (that shows what columns are significant and makes for better understanding of the code) and a proper select with only the needed columns might be the right thing to do.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 7,668

Re: nodupkey and SQL

No, they are not the same.

by wclientcode szaccountnumber raisedmth;

This orders the SAS data by the variables given in the by statement, then by _n_ if there are more than one.  Nodupkey will take the first record per that by grouping to output.

 

Distinct will select a distinct value based on the sort order defined internally, which will consider all variables as * means all variables.  Note that in SQL the syntax is:

order by <variable>{, <variable};

 

The question is, why do you need to change SAS code which you have working for SQL code which you don't know?

Respected Advisor
Posts: 4,802

Re: nodupkey and SQL

If you want to see the same number of records as produced by sort nodupkey, try

 

SELECT DISTINCT wclientcode, szaccountnumber, raisedmth
FROM master_query;

 

If you want to see the same number of records as your query, try

 

proc sort data=master_query nodupkey; by _all_; run;

PG
Super User
Posts: 5,375

Re: nodupkey and SQL

If you got working code, why change it?
If you are doing a migration from SAS then you are better off asking this question in forum appropriate for your target environment.
Data never sleeps
Ask a Question
Discussion stats
  • 4 replies
  • 318 views
  • 1 like
  • 5 in conversation