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;

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

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