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

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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