BookmarkSubscribeRSS Feed
R_Win
Calcite | Level 5
Hi

Iam having acc_no with same number now i want a new variable as seq_no it should be same for all the accouns that are same:


data new;
input acc_no amt;
cards;
1012 23
1013 45
1012 56
1012 56
1013 80
1023 42
run;

output:

acc_no amt seq_no
1012 23 1
1013 45 2
1012 56 1
1012 56 1
1013 80 2
1023 42 3

For this the acc no 1012 is having 1 as seq_no ,and 1013 has 2 as seq_no like that.
2 REPLIES 2
SPR
Quartz | Level 8 SPR
Quartz | Level 8
Hello R_Win,

This is a solution:
[pre]
data i;
input acc_no amt;
i+1;
cards;
1012 23
1013 45
1012 56
1012 56
1013 80
1023 42
run;
proc sort data=i;
by acc_no;
run;
data r;
set i;
if First.acc_no then seq_no+1;
by acc_no;
run;
proc sort data=r out=r(drop=i);
by i;
run;
[/pre]
If it is not necessary to keep the same order of observations in the input and output datasets then variable i can be deleted along side with the last sorting.
Sincerely,
SPR
Ksharp
Super User
If you care the origin order of the dataset. Then try to use Hash Table.
[pre]
data new;
input acc_no amt;
cards;
1012 23
1013 45
1012 56
1012 56
1013 80
1023 42
;
run;
data new(drop=rc);
declare hash ha(hashexp:10);
ha.definekey('acc_no');
ha.definedata('seq_no');
ha.definedone();

do until(last);
set new end=last;
rc=ha.find();
if rc ne 0 then seq_no+1;
rc=ha.add();
output;
end;
stop;
run;
[/pre]


Ksharp

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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