06-01-2017 04:07 AM
I have the below code which is in a data step but i wanted to know if this was possible to do in a Proc SQL statement?
DATA SCORE; SET WEEKS.TEST; BY SBRID; COUNTER+1; IF FIRST.SBRID THEN COUNTER=1; RUN;
06-01-2017 04:17 AM
Well, it is possible. What you have to remember is that SQL is different from SAS, SAS treats each obsevation step by by step, and if sorted then you can do this calculation. SQL treats observations as a bunch of observations - order is only given by logical sort, not by logical order in a data table. So you can use the monotonic() function like this:
proc sql; create table SCORE as select SBRID, monotonic() as COUNTER from WEEKS.TEST; quit;
However, if you have the data:
There is no logical way of telling SQL that the order is:
Or any other combination. Is there a reason why you need to do this in SQL, generally speaking for SQL an observation count in itself is not really useful, aggregate functions are of more use (sum, count etc.).