BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
zqkal
Obsidian | Level 7

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.

 

   

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

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

5 REPLIES 5
Demoxe
Calcite | Level 5

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.

SandyH
Calcite | Level 5

can`t you do

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

?

zqkal
Obsidian | Level 7

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


zqkal
Obsidian | Level 7

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

Jagadishkatam
Amethyst | Level 16

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

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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