You are on page 1of 5

SAS programmers are longing for row number function used in Proc SQL, like ROW_NUMBER() in Oracle SQL

and it will act


like data step system variable _N_. When you google this question, most likely you will get MONOTONIC() function, which
might be one of the most famous undocumented features shipped by SAS. You can of course use it, but at your own risk!
In SAS Usage Note 15138, its said:

The MONOTONIC() function is not supported in PROC SQL. Using the MONOTONIC() function in PROC SQL can cause
missing or non-sequential values to be returned.

Here question is, why stick to the function? Actually there is a long-existing Proc SQL option ( at least since SAS 9.1.3
which was my first SAS version), NUMBER to return the row numbers:

proc

sql

number

outobs=5;

select
from

Species
sashelp.iris

;
quit;

See a new column Row was created in response to this NUMBER option.

[update 2014/03/19] A reader asked how to capture such row number in a data set since the NUMBER
option only affects in output. Well, if it shows up in output, we can always use ODS to retrieve it.
Submit the follow codes to check which output table will be generated (which is SQL_Results):

ods trace on;

proc

sql

number

outobs=5;

select

Species

from

sashelp.iris

;
quit;

ods trace off;

Then use the standard ODS OUTPUT statement to get it:

ods output SQL_Results=rownumber;

proc

sql

number

outobs=5;

select
from

Species
sashelp.iris

;
quit;

Email this Digg This! Subscribe to this feed Share on Facebook View CC license Add to
del.icio.us

This entry was posted on Friday, January 11th, 2013 and is filed under SAS. You can follow any responses to
this entry through the RSS 2.0 feed. You can leave a response orTrackback from your own site.

Get Started with SAS Tagsets.ExcelXP

How to Jump into SAS Data Integration Studio

6 Responses to How to Get Row Numbers in SAS Proc SQL (and DO


NOT Use the Undocumented MONOTONIC Function)
1.

Massimiliano Ignaccolo
26. July 2013 um 11:08

Dear Jiangtang,
The
number
option
proc
sql
to
create
a
row
variable
(unless I am wrong about it)
I
wonder
why
implemented in proc sql.

such

does
not
work
table:
e.g.
you
in
the
newly

simple

if
you
do
not
create

function

Best
Massimiliano

2.

is

use
have
table.

not

Regards,

Thasleem
4. January 2014 um 16:43

It worked exactly for me. Thanks a lot


I used outobs=max for numbering all the observations

3.

Bhavin Patel
17. July 2014 um 08:59

you can use monotonic() function to get row numbers.


Example:
proc
select
quit;

*,

monotonic()

as

obs_count

from

sql;
tableA;

Regards
Bhavin

4.

RM
3. October 2014 um 03:00

Dear Jiangtang,
Is there a query supported by SAS like ROW_NUMBER() OVER(PARTITION BY PRODUCT ?
Then I can get 2 rows of each product with ROW_NUMBER < 2
ROW_NUMBER
1
2
3
1
2
3
1
2
14

PRODUCT
1
1
1
2
2
2
3
3

Thanks in advance!

5.

Oleg
28. January 2015 um 23:40

You can use this query to get table whith monotonic values:
select
from
(select
where
group
;

6.

count(*)
(select
distinct
fmtname
distinct
fmtname
t1.fmtname
by
t1.fmtname

Ersin
25. February 2015 um 04:42

as
rownum
from
dictionary.formats)
t1,
from
dictionary.formats)
t2
>=
t2.fmtname
order
by
1

Hi RM,
It is not possible with ROW_NUMBER() OVER(PARTITION BY PRODUCT with this structure
under PROC statement on SAS.Yet, you can use data step for this purpose.
With a previously ordered dataset (proc sort data; order by a1 b1 desc c1 etc) you can use
BY statement with built-in keywords like FIRST and LAST.
DATA;
SET;
BY a1 b1;
if LAST.a1 and FIRST.b1 then output;
RUN;
etc