To duplicate the SQL ROW_NUMBER Windowing function, you must sort the dataset using the PARTITION and ORDER BY clauses. In the next data step, the SET sorted data set using only the partition variable (s) and uses the automatic control of the FIRST. variable FIRST. , assigns a new line number variable.
Here is the solution for your claimed example:
proc sort data=companyData; by region name; run; data want; set companyData; by region; if first.region then row_number = 1; else row_number + 1; run;
Note that the BY in the data step corresponds to the PARTITION BY in ANSI SQL, but the PROC SORT step is also sorted by column in the ORDER BY . Also note that if the query you are doing the βtransferβ contains an ORDER BY SQL ORDER BY , you must do this with another PROC SORT step.
source share