Hello,
How do I create a new variable named 'Count' that starts at 1 on row 1 and increases by 1 increment through the last observation?
Is it possible to do this using proc sql? Or is it better to do it using the data stet?
Thanks!
Of course! Just add a retain statement before the set statement. E.g.,:
data want; retain count; set sashelp.class; count=_n_; run;
Art, CEO, AnalystFinder.com
There is already an automatic variable that does that. You just have to capture it in a data step. e.g.:
data want; set sashelp.class; count=_n_; run;
Art, CEO, AnalystFinder.com
This works! Is there a way to get this as the first column in the output? Right now, it is the last.
Of course! Just add a retain statement before the set statement. E.g.,:
data want; retain count; set sashelp.class; count=_n_; run;
Art, CEO, AnalystFinder.com
Thank you!
The question would be why. It sounds like your coming from SQL to SAS and there is an understanding gap between the two. SQL does not assume sorted data - in fact apart from grouping and ordering, data can go in any way - hence why there are functions to create these types of ID field either automatically or by hand. In SAS however it is different, the basic functionality expects sorted data - i.e. by group processing (there is the unsorted option - however that doesn't mean the data is not sorted, just not logically). So as your data will always be in group by order for any processing, and id field should not be necessary, and if it is needed for some logic then you can just use _n_.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.