BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Jay_210
Calcite | Level 5

I have a data set that has a list of departments and an employee (approximately 100 departments, and 80-160 employees in each department). I want to easily select the first X amount of employees in each department, but X is specific to the department. For instance, Department 1 I may want 17, Dept 2 19, Dept 3 24, and so on.  I have a second dataset that has a list of each department and the number of observations I want to select from that department. I do not want them picked randomly, they need to be the first X listed in that department. 

 

DepartmentEmployee
dept_1jim
dept_1jon
dept_1nancy
dept_1eric
dept_1tom
dept_2debbie
dept_2susan
dept_2karen
dept_2don
dept_2nate
dept_2chris
dept_2brian
dept_3valerie
dept_3bob
dept_3mike
dept_3tim
dept_3ryan
dept_3stephanie
dept_3tyler

 

DepartmentObs_need
dept_12
dept_24
dept_35

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Just merge the two datasets.  Keep track of how many are in each department.

data want;
  merge big little;
  by department;
  if first.department then count=0;
  count+1;
  if count <= obs_need;
run;

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

Just merge the two datasets.  Keep track of how many are in each department.

data want;
  merge big little;
  by department;
  if first.department then count=0;
  count+1;
  if count <= obs_need;
run;
ballardw
Super User

Maybe:

data want;
   merge set1 set2;
   by department;
   retain depcounter;
   if first.department then depcounter=1;
   else depcounter+1;
   if obs_need le depcounter then output;
end;

Set1 is your long set, set2 is the one with obs needed.

This does require both sets to be sorted by the department variable AND that department only occur one time in Set2 (or whatever your name is).

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 349 views
  • 6 likes
  • 3 in conversation