XixiHaha
XixiHaha

Reputation: 173

How to derive a new colnum according to a given colnum in SAS?

I have two columns like this:

subject regnum
106001 2
106001 2
106001 2
106001 1
106001 1
106001 4
106001 4
106001 6
106001 6
106001 3
106001 3
106001 5
106001 5
106001 7
106001 7
106001 8

and I want a new column like this:

subject regnum regnum_new
106001 2 1
106001 2 1
106001 2 1
106001 1 2
106001 1 2
106001 4 3
106001 4 3
106001 6 4
106001 6 4
106001 3 5
106001 3 5
106001 5 6
106001 5 6
106001 7 7
106001 7 7
106001 8 8

How can I do this?

Upvotes: 0

Views: 42

Answers (2)

Kermit
Kermit

Reputation: 3117

Please edit your question to format the table. In your example, it seems there is only one Subject.

In that particular case, the following should do the trick:

data want;
    set have;
    by subject regnum notsorted;

    if first.regnum then
        regnum_new+1;
run;

Upvotes: 1

PeterClemmensen
PeterClemmensen

Reputation: 4937

data have;
input subject regnum;
datalines;
106001 2
106001 2
106001 2
106001 1
106001 1
106001 4
106001 4
106001 6
106001 6
106001 3
106001 3
106001 5
106001 5
106001 7
106001 7
106001 8
;

data want;
   set have;
   by subject regnum notsorted;
   if first.regnum then regnum_new + 1;
   if first.subject then regnum_new = 1;
run;

Upvotes: 2

Related Questions