DATA Step, Macro, Functions and more

SQL query not works

Accepted Solution Solved
Reply
Super Contributor
Posts: 271
Accepted Solution

SQL query not works

Hello everyone,

 

This query not works:

proc sql; 
Select *,ROW_NUMBER OVER (PARTITION BY 'sex' ORDER BY height DESC) AS Rank_height
From sashelp.class;
quit;

Please advise.

 

Thanks!

 


Accepted Solutions
Solution
‎01-11-2018 01:04 PM
Esteemed Advisor
Posts: 5,625

Re: SQL query not works

Then I guess you would need:

 

proc sort data=sashelp.class out=have; by sex descending height; run;

data want;
do rank_height = 1 by 1 until(last.sex);
    set have; by sex;
    output;
    end;
run;
PG

View solution in original post


All Replies
Super User
Posts: 13,941

Re: SQL query not works

Posted in reply to GeorgeSAS

Doesn't work is awful vague.

Are there errors in the log?: Post the code and log in a code box opened with the {i} to maintain formatting of error messages.

No output? Post any log in a code box.

Unexpected output? Provide input data in the form of a dataset, the actual results and the expected results. Data should be in the form of a data step. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

Super User
Posts: 24,010

Re: SQL query not works

Posted in reply to GeorgeSAS

It's not valid SAS SQL Syntax.

 

This part in particular: 

ROW_NUMBER OVER (PARTITION BY 'sex' ORDER BY height DESC)

 

That looks like a different flavour of SQL. If you want this type of analysis there are other methods in SAS to accomplish this that are better suited.

 

The list of SQL statements that are valid are available here:

http://documentation.sas.com/?docsetId=sqlproc&docsetTarget=n1oihmdy7om5rmn1aorxui3kxizl.htm&docsetV...

 


GeorgeSAS wrote:

Hello everyone,

 

This query not works:

proc sql; 
Select *,ROW_NUMBER OVER (PARTITION BY 'sex' ORDER BY height DESC) AS Rank_height
From sashelp.class;
quit;

Please advise.

 

Thanks!

 



 

PROC Star
Posts: 549

Re: SQL query not works

[ Edited ]
Posted in reply to GeorgeSAS
ROW_NUMBER OVER (PARTITION BY 'sex' ORDER BY height DESC) AS Rank_height

Row_number is window function and is part of ansi sql 2003 and is not implement in Proc sql, but is  implemented in most of databases. not sure, I guess proc sql follows SQL 99 standard. 

 

To sum it up, all the window functions like row_number and rank over , sum over, count over, are not available in proc sql. Hence you cannot use them in proc sql. please use data step by variable and first. and last. concepts to get equivalent results. To achieve the same in proc sql will be long and tough process.

 

please check below paper which discusses how to implement window functions(available in most of databases) in SAS.

 

http://support.sas.com/resources/papers/proceedings11/019-2011.pdf

 

 

Esteemed Advisor
Posts: 5,625

Re: SQL query not works

Posted in reply to GeorgeSAS

 I guess your query would be equivalent to

 

proc sort data=sashelp.class out=have; by sex; run;

proc rank data=have out=want descending;
by sex;
var height;
ranks rank_height;
run;
PG
PROC Star
Posts: 549

Re: SQL query not works

@PGStats I like your answer very much, but row_number is slightly different than rank.  for 

by sex;
var height;

if heights are same in sex,  rank will be same. In case of row_number it assigns the next number for even the same value

 

Solution
‎01-11-2018 01:04 PM
Esteemed Advisor
Posts: 5,625

Re: SQL query not works

Then I guess you would need:

 

proc sort data=sashelp.class out=have; by sex descending height; run;

data want;
do rank_height = 1 by 1 until(last.sex);
    set have; by sex;
    output;
    end;
run;
PG
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 198 views
  • 6 likes
  • 5 in conversation