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
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.
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?
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.