BookmarkSubscribeRSS Feed
yudhishtirb
Calcite | Level 5

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

4 REPLIES 4
Kurt_Bremser
Super User

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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?

PGStats
Opal | Level 21

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
LinusH
Tourmaline | Level 20
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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 4 replies
  • 1889 views
  • 1 like
  • 5 in conversation