get a the row number in a data step using sas

sas
user2467660 picture user2467660 · Jun 9, 2013 · Viewed 44.7k times · Source

Is there a way to get to do a over partition to get the row number on sas? In sql I would go like:

  Select region,company, ROW_NUMBER()  OVER(PARTITION BY region ORDER BY Name)
    From companyData;

I want to do this in a data set preferably

Answer

isJustMe picture isJustMe · Jun 9, 2013

You can do that in a data step easily by using the by statement, it and the do a running sum :

proc sort data=myData; by region name; run;

Data myData;
Set myData;
By company;
if first. company then n=1;
   else n+1;
run;

Also to ennmuarete all the obs you can use the built in features:

DATA COMPANYDATA;
SET COMPANYDATA;
ROW_NUM=_N_;    
RUN;

As Joe mentioned you might want to set the format for your row_num depending on how much obs you would get within that grouping.