02-13-2015 03:16 PM
I want to add a simple sequence number to a data set with no 'by' variable. Does anyone know how to identify the first observation in a data set so I can set this to zero and everything following it will just be an increment?
02-13-2015 03:30 PM
That works fine: if _n_=1 then do ... Thanks Reeza. One somewhat related follow up, when I increment using lag, it is not working in this instance. Do yo or anyone usually use lag for something like this?
if _n_=1 then do;
if _n_ ne 1 then do;
02-13-2015 03:40 PM
Lag doesn't work in conditional if/then statements. There are papers written on this topic so you can search for those if you're interested.
I would use the following myself:
02-13-2015 03:45 PM
Thanks Reeza. I think I remember that now actually. Sorry and thanks again.
And the _n_ works nicely when you don't need a 'by' variable grouping.
Need further help from the community? Please ask a new question.