BookmarkSubscribeRSS Feed
RohitSehgal
Calcite | Level 5

Hi,

 

I need to populate a count_var variable in my dataset based on the value of Order Id, Dataset format is as follows:

 

Order ID   Count_var

000123     1

000123     2

000123     3

000125     1

000125     2

000130     1

 

Here Count_var variable is something that I want to auto-populate in SAS, which should start the counting from 1 when the value of Order ID changes. Please help me with the code.

 

Regards,

Rohit

2 REPLIES 2
novinosrin
Tourmaline | Level 20

data want;

set have;

by order_id;

if first.order_id then count_var=1;

else count_var+1;

run;

 

r_behata
Barite | Level 11
Data have;
input Order_ID;
datalines;
000123
000123
000123
000125
000125
000130
;
run;

proc sort;
	by Order_ID;
run;

data want;
	set have;
	by Order_ID;

	if first.Order_ID then Count_var=1;
		else Count_var+1;
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 2 replies
  • 796 views
  • 0 likes
  • 3 in conversation