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

Suppose I have a list of new visitors sorted by last name descending. If I have an even number of visitors I want to send the top half to one salesperson and another to the other salesperson.

Have:

ZunigaJames
UtigaTom
BalforJane

Anderson

Jessica


Want:
Salesperson A gets:

ZunigaJames
UtigaTom


Sales person B gets:

BalforJane
AndersonJessica


However, suppose I have and odd number of visitors. Since Salesperson A has a higher priority, I want them to get the extra visitor.

Have:

ZunigaJames
UtigaTom
LemieuxLynn
BalforJane

Anderson

Jessica


Want:
Salesperson A gets:

ZunigaJames
UtigaTom
LemieuxLynn


Salesperson B gets:

BalforJane

Anderson

Jessica





1 ACCEPTED SOLUTION

Accepted Solutions
russt_sas
SAS Employee

Here is one way to accomplish your task:

 

data one;
input last $ first $;
cards;
Zuniga James
Utiga Tom
Lemieux Lynn
Balfor Jane
Anderson Jessica
;

data A B;
set one nobs=obs;
half=obs/2;
if mod(obs,2)=0 then do;
if _n_ le half then output A;
else output B;
end;
else do;
if _n_ le ceil(half) then output A;
else output B;
end;

run;

proc print;
run;

View solution in original post

4 REPLIES 4
russt_sas
SAS Employee

Here is one way to accomplish your task:

 

data one;
input last $ first $;
cards;
Zuniga James
Utiga Tom
Lemieux Lynn
Balfor Jane
Anderson Jessica
;

data A B;
set one nobs=obs;
half=obs/2;
if mod(obs,2)=0 then do;
if _n_ le half then output A;
else output B;
end;
else do;
if _n_ le ceil(half) then output A;
else output B;
end;

run;

proc print;
run;

ballardw
Super User

Similar but less code:

data have;
  input name $;
datalines;
abc
pdq
vvv
jhi
ccc
;
data want;
   set have nobs=num;
   group = (_n_ le round( (num/2),1) );
run;

Assumes that the ORDER of the names in the data is important.

Nobs= creates a temporary variable of the count of observations in the data set.

Round( (num/2,1) ) rounds the division of the number of records divided by 2 to the nearest integer, so the 2.5 you would get from 5 records gets rounded to 3.

_N_ is a counter for the number of iterations of the data step. In a simple action like this it will act as a row counter. so if the _N_ is less than or equal to that half way we will get 1 and 0 other wise as that is how SAS returns the values of logical comparisons: 1 for true, 0 for false.

 

Caution: this sort of algorithm doesn't extend well to a generic N number of groups. Proc Rank or Surveyselect might be better in a more complex selection.

mmaleta851
Fluorite | Level 6

This worked. Thank you.

PaigeMiller
Diamond | Level 26

Don't split the dataset. Assign value of 0 to the top half and 1 to the bottom half (or something like that). Keep everything in one data set.

--
Paige Miller

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
  • 4 replies
  • 407 views
  • 4 likes
  • 4 in conversation