Solved
Contributor
Posts: 48

# How to create sort using proc format?

This is a dataset i'm working with. I need to sort the variable C_class. in this order: CMAR,SMAR,FMAR,SFAR,MVAR,PPAR,FVAR

data sum;

input c_class \$ city \$ mile cost;

datalines;
CMAR MD 12 34.00

FMAR AZ 22 12.00

PPAR MD 32 76.00

MVAR CA 11 21.00

SFAR VA 32 76.00

SMAR UT 34 87.00

FVAR va 32 54.00

;

run;

data sum_2; set sum;

if c_class = 'CMAR' THEN SORT = 1;

IF c_class = 'SMAR' THEN SORT = 2;

IF c_class = 'FMAR' THEN SORT =3;

IF c_class = 'SFAR' THEN SORT = 4;

IF c_class = 'MVAR' THEN SORT = 5;

IF c_class = 'PPAR' THEN SORT = 6;

IF c_class = 'FVAR' THEN SORT = 7;

run;

PROC SORT DATA=SUM_2; BY SORT ; RUN;

THIS IS THE APPROCE I TOOK.IT WORKS PERFERC. BUT, I WILL HAVE TO DO THIS MULTIPLE TIME AND I WAS WONDERING IF I WROTE PROC FORMAT OR SOME TYPES OF MACRO SO THAT I DON'T HAVE TO REPEATE THIS AGAIN AND AGAIN.

THE DATASET IS ALOT LARGER THEAN THIS EXAMPLE.

Accepted Solutions
Solution
‎04-07-2013 11:58 PM
Posts: 1,147

## Re: How to create sort using proc format?

Hi,

you can use the proc format to generate the numeric values for sort variable like below

proc format;

invalue \$sort 'CMAR'=1

'SMAR'=2

'FMAR'=3

'SFAR'=4

'MVAR'=5

'PPAR'=6

'FVAR'=7;

run;

data sum;

input c_class \$ city \$ mile cost;

sort=input(c_class,\$sort.);

datalines;

CMAR MD 12 34.00

FMAR AZ 22 12.00

PPAR MD 32 76.00

MVAR CA 11 21.00

SFAR VA 32 76.00

SMAR UT 34 87.00

FVAR va 32 54.00

;

run;

proc sort data=sum;

by sort;

run;

I used invalue for creating the informat from character values. Hope this helps.

Thanks,

Thanks,
Jag

All Replies
Contributor
Posts: 23

## Re: How to create sort using proc format?

You can create format, that will translate values from "c_class" variable to values, that you want to use for sort. Then create this "sort" field like sort=put(c_class, \$YourFormat.), and then sort your table by "sort" field.

Occasional Contributor
Posts: 8

## Re: How to create sort using proc format?

can`t you do

proc sort data=sum;by CMAR....;run;

?

Contributor
Posts: 48

## Re: How to create sort using proc format?

Sandy, I can't sort by CMAR because CMAR is not a variable it's a value in C_Class.

Contributor
Posts: 48

## Re: How to create sort using proc format?

Thank you all for your kind response. It did work.

Solution
‎04-07-2013 11:58 PM
Posts: 1,147

## Re: How to create sort using proc format?

Hi,

you can use the proc format to generate the numeric values for sort variable like below

proc format;

invalue \$sort 'CMAR'=1

'SMAR'=2

'FMAR'=3

'SFAR'=4

'MVAR'=5

'PPAR'=6

'FVAR'=7;

run;

data sum;

input c_class \$ city \$ mile cost;

sort=input(c_class,\$sort.);

datalines;

CMAR MD 12 34.00

FMAR AZ 22 12.00

PPAR MD 32 76.00

MVAR CA 11 21.00

SFAR VA 32 76.00

SMAR UT 34 87.00

FVAR va 32 54.00

;

run;

proc sort data=sum;

by sort;

run;

I used invalue for creating the informat from character values. Hope this helps.

Thanks,