BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
heretolearn
Obsidian | Level 7

Hi all,

 

I have a dataset in which I need to assign a unique id by two variables.

 

Below is what I have:

 

id      reg_seq

1       1

1       1

1       2

1       3

2       1

2       2

2       2

 

This is what I want:

id      reg_seq       seq

1       1                  1

1       1                  1

1       2                  2

1       3                  3

2       1                  4

2       2                  5

2       2                  5

 

Any advice?

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Assuming your data set is sorted so that the BY statement works:

 

data want;

set have;

by id reg_seq;

if first.reg_seq then seq + 1;

run;

View solution in original post

3 REPLIES 3
Astounding
PROC Star

Assuming your data set is sorted so that the BY statement works:

 

data want;

set have;

by id reg_seq;

if first.reg_seq then seq + 1;

run;

heretolearn
Obsidian | Level 7

Thank you, both!

KentaMURANAKA
Pyrite | Level 9

Hi, heretolearn:

 

 

If you know First (or Last) By Variables & Retain Statement, you can get your objective submitting this code.

data test;
    input id reg_seq;
    datalines;
    1 1
    1 1
    1 2
    1 3
    2 1
    2 2
    2 2
    ;
run;
proc sort data=test out=test_1;
    by id reg_seq;
run;
data test_2;
    set test_1;
    retain seq;
    by id reg_seq;
    if first.reg_seq eq 1 then byflg=1;
    if _n_ eq 1 then seq=0;
    if byflg ne . then seq+byflg; 
run;

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 9732 views
  • 3 likes
  • 3 in conversation