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.
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
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.
can`t you do
proc sort data=sum;by CMAR....;run;
?
Sandy, I can't sort by CMAR because CMAR is not a variable it's a value in C_Class.
Thank you all for your kind response. It did work.
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.