BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hello everyone,

I have a variable Region (which is a number from 1 to 6). I have added a format like so:
[pre]PROC FORMAT library=PROJET.FORMATS;
value $Region
1='Pacific' 2='Americas' 3='Oceania' 4='Middle East' 5='Asia' 6='Africa';
RUN;[/pre]

I want to sort them alphabetically (Africa, Americas, ... , Pacific) and not numerically by ID (Pacific, Americas, ... Africa). I have typed this:

[pre]proc sort data=TEMP; format Region $Region.; by Region; run;
proc sort data=TEMP; by Region; run;[/pre]

...and looked here:
http://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/a000146878.htm

...but no luck. Any ideas? Apparently there's simply an option in SAS to do that.

Thank-you in advance
5 REPLIES 5
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
You must create a new SAS variable having the formatted value assigned, using the PUT function in an assignment statement typically.

Scott Barry
SBBWorks, Inc.
deleted_user
Not applicable
I'm afraid I simply don't understand what you mean. Do you mean that I should place another DATA STEP before the PROC SORT?
deleted_user
Not applicable
Ok to be more precise I actually simply need to output them sorted using the PROC MEANS method, so there is a statement/option in PROC MEANS that will allow me to sort it by formatted values (whilst keeping the dataset unchanged) then that will do fine.
deleted_user
Not applicable
Hi

you can do this without another data step. Here is an example. Maybe your code does not work because you defined character format and try to apply the format to numeric variable.

DATA station;
INPUT c fc;
CARDS;
1 1
2 2
3 3
4 4
5 5
6 6
;
RUN;

PROC FORMAT;
value Region
1='Pacific' 2='Americas' 3='Oceania' 4='Middle East' 5='Asia' 6='Africa';
RUN;
proc print data=station;
run;

/*
Obs c fc

1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6

*/
proc sort data=station out=station_s;
by descending fc;
format fc region.;
run;
proc print data=station_s;
run;
/*

Obs c fc

1 6 Africa
2 5 Asia
3 4 Middle East
4 3 Oceania
5 2 Americas
6 1 Pacific
*/
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Explore using the ORDER= parameter for PROC MEANS. Otherwise, to follow-on my reply and your subsequent question about a DATA step -- yes, exactly. If you are not interested in re-sequencing your data-values (another reply's suggestion), then you can use either a DATA step or a PROC SQL, along with the PUT function to create a new variable with the formatted value rather than the internal value of sort-ordering purpose.

Scott Barry
SBBWorks, Inc.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 1155 views
  • 0 likes
  • 2 in conversation