- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
Jag
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
can`t you do
proc sort data=sum;by CMAR....;run;
?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Sandy, I can't sort by CMAR because CMAR is not a variable it's a value in C_Class.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you all for your kind response. It did work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
Jag