Help using Base SAS procedures

Problem with PROC SORT

Reply
N/A
Posts: 0

Problem with PROC SORT

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
Super Contributor
Super Contributor
Posts: 3,174

Re: Problem with PROC SORT

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.
N/A
Posts: 0

Re: Problem with PROC SORT

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?
N/A
Posts: 0

Re: Problem with PROC SORT

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.
N/A
Posts: 0

Re: Problem with PROC SORT

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
*/
Super Contributor
Super Contributor
Posts: 3,174

Re: Problem with PROC SORT

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.
Ask a Question
Discussion stats
  • 5 replies
  • 162 views
  • 0 likes
  • 2 in conversation