Help using Base SAS procedures

Please help, how to add a new id column?

Accepted Solution Solved
Reply
N/A
Posts: 0
Accepted Solution

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

Accepted Solutions
Solution
‎08-30-2017 12:13 PM
Super Contributor
Posts: 292

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

Posted in reply to deleted_user
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;

View solution in original post


All Replies
Solution
‎08-30-2017 12:13 PM
Super Contributor
Posts: 292

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

Posted in reply to deleted_user
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?

Posted in reply to deleted_user
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?

Posted in reply to deleted_user
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
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 12859 views
  • 0 likes
  • 3 in conversation