Help using Base SAS procedures

Please help, how to add a new id column?

Reply
N/A
Posts: 0

Please help, how to add a new id column?

Hi Gurus,

Please help.

I would like to add a new ID column in existing table with the ID numbers equal the row numbers. Still don't know how.

ID
1
2
3
4
5
...


I know this is a dumb question, but.... Message was edited by: WAYNE66
Super Contributor
Posts: 291

Re: Please help, how to add a new id column?

The data step carries a bunch of hidden variables in the pdv that disappear at the end of the data step. To keep them, one needs to insert them into another field. The one that you are interested in is _n_;

data new;
set old;
IDNew=_n_;
run;
N/A
Posts: 0

Re: Please help, how to add a new id column?

Hi Wayne,
I have come up with a (sort of) solution, but for sure soon will get the exact solution to your query. Well this is what I thought of:
If you can create an ID variable in ur raw data set just analogous to your observation and in ur PROC PRINT use 'NOOBS' option... in dat case , ur ouput wud have soemthg like this:
ID
1
2
3
.
.
10
Now this is gonna be fine with few rows ,,, how if the raw data has 100s of rows(I mean really huge data????)???
Will find out and let u know sooner.Even I am eager to know abt this...

Regards,
SAS_Learner
Contributor
Posts: 59

Re: Please help, how to add a new id column?

As Bill pointed out , a perfect way to do it is to use _n_. This keeps the row number at every row. Once you have a dataset, you just need to create a new variable which is = _n_ and this will work.

Thanks
Ask a Question
Discussion stats
  • 3 replies
  • 10992 views
  • 0 likes
  • 3 in conversation