DATA Step, Macro, Functions and more

How to create sort using proc format?

Accepted Solution Solved
Reply
Contributor
Posts: 43
Accepted Solution

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.

THANKS IN ADVANCE FOR YOUR RESPONCE AND TIME.

 

   


Accepted Solutions
Solution
‎04-07-2013 11:58 PM
Trusted Advisor
Posts: 1,129

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,

Jagadish

Thanks,
Jag

View solution in original post


All Replies
Contributor
Posts: 22

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: 43

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: 43

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
Trusted Advisor
Posts: 1,129

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,

Jagadish

Thanks,
Jag
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 239 views
  • 4 likes
  • 4 in conversation