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!
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;
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.
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:
@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!
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
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;
@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
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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.