SAS Enterprise Guide

Desktop productivity for business analysts and programmers
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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2650 views
  • 1 like
  • 5 in conversation