BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Nasser_DRMCP
Lapis Lazuli | Level 10

hello,

 

I get a team with 26 employees (it could be change)

I need to split the employees into group for each days of the Week. but sometimes the Week has 5 working days and sometimes 4

in case of 5 working days, I need to get 5 values 5+5+5+5+6 =26

in case of 4 working days, I need to get 4 values 6+6+7+7 = 26

 

After that, the aim is to identify employees with their number. I can do someting like this :"from employee n°1 to n°value1, from n°value1 ti n°value2...

thanks in advance

Nasser

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hello @Nasser_DRMCP,

 

Do you want to assign the employees randomly to the five or four groups? If so, you can use the GROUPS= option of PROC SURVEYSELECT:

%let n_emp=26; /* number of employees */
%let n_wd=4;   /* number of working days */

/* Create test data for demonstration */

data have;
do empno=1 to &n_emp;
  output;
end;
run;

/* Assign employees to &n_wd groups */

proc surveyselect data=have groups=&n_wd
seed=2718 out=want;
run;

/* Check group sizes */

proc freq data=want;
tables GroupID;
run;

View solution in original post

5 REPLIES 5
FreelanceReinh
Jade | Level 19

Hello @Nasser_DRMCP,

 

Do you want to assign the employees randomly to the five or four groups? If so, you can use the GROUPS= option of PROC SURVEYSELECT:

%let n_emp=26; /* number of employees */
%let n_wd=4;   /* number of working days */

/* Create test data for demonstration */

data have;
do empno=1 to &n_emp;
  output;
end;
run;

/* Assign employees to &n_wd groups */

proc surveyselect data=have groups=&n_wd
seed=2718 out=want;
run;

/* Check group sizes */

proc freq data=want;
tables GroupID;
run;
Nasser_DRMCP
Lapis Lazuli | Level 10

Thanks !

is it possible to get the n°group sorted ?

I mean from employee 1 to employee 5 are grp 1, from employee 6 to employee 10 are grp 2...

or is it random ?

thanks, 

FreelanceReinh
Jade | Level 19

I haven't found an option in PROC SURVEYSELECT to obtain an output dataset sorted by GroupID, but of course you can apply PROC SORT to dataset WANT:

proc sort data=want;
by GroupID;
run;

Or do you want to assign the employees systematically (rather than randomly) to the groups?

FreelanceReinh
Jade | Level 19

Addendum: Formula for direct calculation of GroupID in a systematic group assignment (leading to the same group numbers and sizes as with PROC SURVEYSELECT, but without random selection).

%let c=%sysfunc( ceil(&n_emp/&n_wd));
%let f=%sysfunc(floor(&n_emp/&n_wd));
%let r=%sysfunc(  mod(&n_emp,&n_wd));

data want;
set have nobs=n;
GroupID=ifn(_n_<=(&n_wd-&r)*&f, ceil(_n_/&f), &n_wd-floor((n-_n_)/&c));
run;
jimbarbour
Meteorite | Level 14

You can assign systematically, but my approach is more complicated that @FreelanceReinh's very nice, compact approach.  See below for code.

 

Results are put into the log, and a SAS dataset is produced as well.

NOTE:  Day 1  Employees needed today=5
       Employees for today: 1 through 5
NOTE:  Day 2  Employees needed today=5
       Employees for today: 6 through 10
NOTE:  Day 3  Employees needed today=5
       Employees for today: 11 through 15
NOTE:  Day 4  Employees needed today=5
       Employees for today: 16 through 20
NOTE:  Day 5  Employees needed today=6
       Employees for today: 21 through 26

Jim

 

%LET	Nbr_of_Employees					=	26;
%LET	Days_This_Week						=	5;

DATA	Allocated_Employees;
	DROP	_:;
	_Employees_Per_Day						=	FLOOR(&Nbr_of_Employees / &Days_This_Week);
	_Unallocated_Employees					=	MOD(&Nbr_of_Employees, &Days_This_Week);
	ARRAY	Days		{*}						Day1 		- Day&Days_This_Week;
	ARRAY	Ranges_Low	{*}						Range_Low1	- Range_Low&Days_This_Week;
	ARRAY	Ranges_High	{*}						Range_High1	- Range_High&Days_This_Week;

	DO	_Day								=	&Days_This_Week	TO	1	BY	-1;
		Days{_Day}							=	_Employees_Per_Day;
		IF	_Unallocated_Employees			>	0	THEN
			DO;
				_Unallocated_Employees		+	-1;
				Days{_Day}					+	1;
			END;
	END;

	_Unallocated_Employees					=	&Nbr_of_Employees;

	DO	_Day								=	&Days_This_Week	TO	1	BY	-1;
		Ranges_High{_Day}					=	_Unallocated_Employees;
		_Unallocated_Employees				=	_Unallocated_Employees	-	Days{_Day};
		Ranges_Low{_Day}					=	_Unallocated_Employees	+	1;
	END;

	DO	_Day								=	1	TO	&Days_This_Week;
		PUTLOG	"NOTE:  Day "  _Day  " Employees needed today=" Days{_Day};
		PUTLOG	"NOTE-  Employees for today: "  Ranges_Low{_Day}  "through " Ranges_High{_Day};
	END;

	PUTLOG	"NOTE-  ";

	STOP;
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
  • 5 replies
  • 1569 views
  • 1 like
  • 3 in conversation