## Sorting categorical variables with PROC SORT

Solved
Frequent Contributor
Posts: 107

# Sorting categorical variables with PROC SORT

[ Edited ]

I'm trying to do the following with PROC SORT:

```DATA FOO;
INPUT name \$;
DATALINES;
nassau
suffolk
westchester
putnam
;
RUN;

PROC SQL;
SELECT *
FROM FOO
ORDER BY CASE name
WHEN 'westchester' THEN 1
WHEN 'putnam' THEN 2
WHEN 'suffolk' THEN 3
WHEN 'nassau' THEN 4
END;
QUIT;

```

Accepted Solutions
Solution
‎01-12-2018 10:56 AM
Super User
Posts: 24,001

## Re: Sorting categorical variables with PROC SORT

You'd still need to create those new variables in a prior step and then sort accordingly.

AFAIK there's no way to incorporate that into the PROC SORT directly, which is what I assume you're asking.

tomcmacdonald wrote:

I'm trying to do the following with PROC SORT:

```DATA FOO;
INPUT name \$;
DATALINES;
nassau
suffolk
westchester
putnam
;
RUN;

PROC SQL;
SELECT *
FROM FOO
ORDER BY CASE name
WHEN 'westchester' THEN 1
WHEN 'putnam' THEN 2
WHEN 'suffolk' THEN 3
WHEN 'nassau' THEN 4
END;
QUIT;

```

All Replies
Solution
‎01-12-2018 10:56 AM
Super User
Posts: 24,001

## Re: Sorting categorical variables with PROC SORT

You'd still need to create those new variables in a prior step and then sort accordingly.

AFAIK there's no way to incorporate that into the PROC SORT directly, which is what I assume you're asking.

tomcmacdonald wrote:

I'm trying to do the following with PROC SORT:

```DATA FOO;
INPUT name \$;
DATALINES;
nassau
suffolk
westchester
putnam
;
RUN;

PROC SQL;
SELECT *
FROM FOO
ORDER BY CASE name
WHEN 'westchester' THEN 1
WHEN 'putnam' THEN 2
WHEN 'suffolk' THEN 3
WHEN 'nassau' THEN 4
END;
QUIT;

```

Super User
Posts: 13,939

## Re: Sorting categorical variables with PROC SORT

And the question is?

"I'm trying to do the following the PROC SORT" doesn't make much sense.

You need to check your example data. The way you read your data you do not have a value of "Westchester" only "westches" . \$ without an informat or similar to tell SAS the variable is longer than 8 characters results in truncated data.

```DATA foo;
INPUT name \$12.;
DATALINES;
nassau
suffolk
westchester
putnam
;
RUN;```

If you were not getting expected results you should show the results you were getting and what you expected

Are you attempting to use the proc sql instead of proc sort?

Frequent Contributor
Posts: 107

## Re: Sorting categorical variables with PROC SORT

Sorry, I didn't test the code above. I'm trying to sort based on an assigned priority of a categorical variable solely using PROC SORT without having to create a temporary variable.
☑ This topic is solved.