BookmarkSubscribeRSS Feed
trekvana
Calcite | Level 5
Hello all-

suppose in my data set i have an id variable:

id
3
3
3
4
4
4
4
6
7
7

and i want to create a new id variable (in the same data set) say id2 such that now we have in the data set:

id id2
3 1
3 1
3 1
4 2
4 2
4 2
4 2
6 3
7 4
7 4

and so on. i need to be able to generalize this regardless of the numbers in the id column. in short i need to recode the id column starting from 1 on up.

Any way to do this?

Cheers
Geo
3 REPLIES 3
Reeza
Super User
Look up BY Group processing and retain.

The following will work
data have;
input id;
cards;
3
3
3
4
4
4
4
6
7
7
;
run;

proc sort data=have;
by id;
run;

data want;
set have;
by id;
retain id2 0;
if first.id then id2+1;
run;
trekvana
Calcite | Level 5
Thanks!!
chang_y_chung_hotmail_com
Obsidian | Level 7
Just a minor reminder that the "accumulator" variable in the sum statement is automatically initialized to zero and retained. Thus the retain statement is not necessary. The fine manual even metions that a sum statement is equivalent to:
[pre]
retain variable 0;
varibale = sum(varible, expression);
[/pre]

Thus your data want step can be as simple as:
[pre]
data want;
set have;
by id;
id2 + first.id;
run;
[/pre]

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 3 replies
  • 656 views
  • 0 likes
  • 3 in conversation