Quartz | Level 8

## quartiles for categorical variable

Is there a way to divide the categorical into 4 quartiles?

For example I have gender with values F and M and I need to divide all F in 4 quartiles and all M in 4 quartiles.

I know for numeric we can use proc rank but what is the equivalent for categorical?

Thanks

5 REPLIES 5
Super User

## Re: quartiles for categorical variable

@sfo wrote:

Is there a way to divide the categorical into 4 quartiles?

For example I have gender with values F and M and I need to divide all F in 4 quartiles and all M in 4 quartiles.

I know for numeric we can use proc rank but what is the equivalent for categorical?

Thanks

I think you need to provide some example data and what you want the output to look like given that example data. You are using "quartile" in a non-standard way so a more specific example will be helpful.

Do you mean to assign the data into 4 groups fore each sex that are roughly the same size? Does the assignment have to be random? If so perhaps:

Proc sort data=have;

by sex;

run;

Proc surveyselect data=have out=want groups=4;

strata sex;

run;

Quartz | Level 8

## Re: quartiles for categorical variable

Thanks for the solution.

Does it work on 9.2?

I am getting an error:

436 Proc surveyselect data=fdaada_popPK out=want groups=4;
------
22
ERROR 22-322: Syntax error, expecting one of the following: ;, (, CERTAIN, CERTSIZE, DATA, JTPROBS, M, MAXSIZE, METHOD,
MINSIZE, N, NMAX, NMIN, NOPRINT, OUT, OUTALL, OUTHITS, OUTSEED, OUTSIZE, OUTSORT, RATE, REPS, SAMPRATE, SAMPSIZE,
SEED, SELECTALL, SORT, SRSALG, STATS.

Super User

## Re: quartiles for categorical variable

@sfo wrote:

Thanks for the solution.

Does it work on 9.2?

I am getting an error:

436 Proc surveyselect data=fdaada_popPK out=want groups=4;
------
22
ERROR 22-322: Syntax error, expecting one of the following: ;, (, CERTAIN, CERTSIZE, DATA, JTPROBS, M, MAXSIZE, METHOD,
MINSIZE, N, NMAX, NMIN, NOPRINT, OUT, OUTALL, OUTHITS, OUTSEED, OUTSIZE, OUTSORT, RATE, REPS, SAMPRATE, SAMPSIZE,
SEED, SELECTALL, SORT, SRSALG, STATS.

No, the Groups=option was added in 9.3 (I think).

If Randomness is not an issue this is another way:

Using a SAS set you should have as a starting point:

```proc sort data=sashelp.class out=work.class;
by sex;
run;

data work.want;
set work.class;
by sex;
group= mod(_n_,4)+1;
run;```

Which will evenly distribute the group identifier within the BY variable.

Diamond | Level 26

## Re: quartiles for categorical variable

@ballardw wrote:

If Randomness is not an issue this is another way:

Using a SAS set you should have as a starting point:

```proc sort data=sashelp.class out=work.class;
by sex;
run;

data work.want;
set work.class;
by sex;
group= mod(_n_,4)+1;
run;```

Which will evenly distribute the group identifier within the BY variable.

And if randomness is an issue, then you can assign random numbers to each observation and use those to split the data into 4 groups.

--
Paige Miller
Diamond | Level 26

## Re: quartiles for categorical variable

@sfo wrote:

For example I have gender with values F and M and I need to divide all F in 4 quartiles and all M in 4 quartiles.

Using what criteria would you do this dividing into quartiles? Explain please! Give an example.

--
Paige Miller
Discussion stats
• 5 replies
• 1842 views
• 2 likes
• 3 in conversation