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

I want the SRNO variable in following manner

CIF ACNO SRNO
123 14 1
123 15 2
123 16 3
159 17 1
159 18 2
159 19 3
159 20 4
159 30 5

but after running the code:

data DATA3;
set data1 ;
by cif  ;
srno + 1;
if first.cif then srno=1;

run;

I get the result:

CIF ACNO SRNO
123 14 1
123 15 2
123 16 2
159 17 1
159 18 2
159 19 2
159 20 2
159 30 2

 

Thanks in advance for your guidance. 

Regards

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Can't be.

data data1;
input cif acno;
datalines;
123 14
123 15
123 16
159 17
159 18
159 19
159 20
159 30
;
run;

data data3;
set data1;
by cif;
srno + 1;
if first.cif then srno = 1;
run;

proc print data=data3 noobs;
run;

Result:

cif    acno    srno

123     14       1 
123     15       2 
123     16       3 
159     17       1 
159     18       2 
159     19       3 
159     20       4 
159     30       5 

Could it be that variable SRNO is already in your dataset, with a constant value of 1?

View solution in original post

2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20

Do like this

 

data have;
input CIF ACNO;
datalines;
123 14
123 15
123 16
159 17
159 18
159 19
159 20
159 30
;

data want;
   set have;
   by CIF;
   if first.CIF then srno=1;
   else srno+1;
run;
Kurt_Bremser
Super User

Can't be.

data data1;
input cif acno;
datalines;
123 14
123 15
123 16
159 17
159 18
159 19
159 20
159 30
;
run;

data data3;
set data1;
by cif;
srno + 1;
if first.cif then srno = 1;
run;

proc print data=data3 noobs;
run;

Result:

cif    acno    srno

123     14       1 
123     15       2 
123     16       3 
159     17       1 
159     18       2 
159     19       3 
159     20       4 
159     30       5 

Could it be that variable SRNO is already in your dataset, with a constant value of 1?

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 10766 views
  • 1 like
  • 3 in conversation