BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
GeorgeSAS
Lapis Lazuli | Level 10

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!

 

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

6 REPLIES 6
ballardw
Super User

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.

Reeza
Super User

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!

 



 

kiranv_
Rhodochrosite | Level 12
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

 

 

PGStats
Opal | Level 21

 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
kiranv_
Rhodochrosite | Level 12

@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

 

PGStats
Opal | Level 21

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

sas-innovate-2024.png

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.

 

Register now!

How to Concatenate Values

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.

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
  • 6 replies
  • 1016 views
  • 6 likes
  • 5 in conversation