BookmarkSubscribeRSS Feed
Utabikunda
Calcite | Level 5

SAS 9.4:

 

I am trying to count enrollment orders where an individual can enroll multiple times in multiple groups. In this case, I'd like to count the enrollment order for each group separately. Using the by statement with first.id, I can count the OVERALL enrollment order. But I'd like to count an enrollment in a different group as a completely new enrollment, even if this individual had already enrolled in a different group. So, in the example below, dataline 2 where id 1001 enrolled in group 2 should be given an enrollment order of 1. (If you run the example below, it's not working that way)

 

...and without using a concatenation of id and group.

 

data test;
input id group date;
datalines;
1001 1 22100
1001 2 22200
1001 1 22300
1001 1 22400
1002 2 22500
1002 2 22600
1002 2 22700
;
run;

proc sort data = test;  
  by id group date;
run;

data test;
	set test;
	by id group;
	 	if (first.id) and (first.group) then enroll_ord = 1; 
	 		else enroll_ord + 1;
proc print; run;

 

1 REPLY 1
PGStats
Opal | Level 21

First.id is not true (=1) when this is not a new id. You only need first.group :

 

data test;
set test;
by id group;
if first.group 
	then enroll_ord = 1; 
 	else enroll_ord + 1;
run;
PG

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
  • 1 reply
  • 638 views
  • 0 likes
  • 2 in conversation