BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jcis7
Pyrite | Level 9

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!

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

 

View solution in original post

5 REPLIES 5
art297
Opal | Level 21

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

jcis7
Pyrite | Level 9

This works!  Is there a way to get this as the first column in the output?  Right now, it is the last.

art297
Opal | Level 21

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

 

jcis7
Pyrite | Level 9

Thank you!

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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_.

sas-innovate-2024.png

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.

 

Register now!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 1375 views
  • 1 like
  • 3 in conversation