BookmarkSubscribeRSS Feed
molla
Fluorite | Level 6

Hi,

I have the following dataset

student_id  

1

2

3

4

1

2

3

1

2

I need the output as

student_id      seq_num

1                        1

2                        2

3                        3

4                        4

1                        1

2                        2

3                        3

1                        1

2                        2

3                        3

1                        1

2                         2

   

5 REPLIES 5
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Not sure what your question here is.  You have one variable and need to create another variable which is exactly the same as the variable you have?  Maybe your required output is not correct, and you need the sequence within each by group?  If so:

data want;
  set have;
  do seq_num=1 to 4;
    output;
  end;
run;
mkeintz
PROC Star

I think what you really want is to track the order in which id's first appear, yes? I.e.if you have

 

id

AAA

A21

BBB

CCC

A21

BBB

AAA

...

 

then do you wnat   id=AAA seq=1,  id=A21 seq=2, id=BBB seq=3, id=CCC seq=4.

 

If so then this would work:

 

data want (drop=rc);
  set have;
  if _n_=1 then do;
    declare hash id_lookup();
      id_lookup.definekey('id');
      id_lookup.definedata('seq');
      id_lookup.definedone();
  end;
  rc=id_lookup.find();
  if rc^=0 then do;
    seq=id_lookup.num_items+1;
    rc=id_lookup.add();
  end;
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Astounding
PROC Star

Here's an idea:

 

data want;

set have;

seq_num + 1;

if student_id < lag(student_id) then seq_num=1;

run;

 

It's not 100% clear when to start the SEQ_NUM values over again, but this might be sufficient.

mkeintz
PROC Star

I think the OP wants to set up a lookup table, in which each ID is assigned a unique sequence number, with this additional propert: the sequence number will rank the order of first appearance of the ID.

 

MK

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

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
  • 2698 views
  • 0 likes
  • 5 in conversation