Help using Base SAS procedures

proc sort

Reply
Occasional Contributor
Posts: 15

proc sort

 how to sort character/alphabetical data in logical order (without sorting uppercase letters and lowercase letters separately)?
 
I tried to use SORTSEQ = LINGUISTIC option, but I get the error in the screenshot below.
Super User
Posts: 17,829

Re: proc sort

No screenshot is included. Please include your code/log and what version of SAS you're using.

 

It seems this should work as of SAS 9.2+

http://support.sas.com/kb/31/369.html

Occasional Contributor
Posts: 15

Re: proc sort

I have a month variable and i want it in a month order like jan fab march etc

 

I tried to use SORTSEQ = LINGUISTIC option, but I get the error in the screenshot below.

sas: 9.2

Error: Translation table for sort sequence LINGUIST no found 

 

 

Super User
Posts: 17,829

Re: proc sort

Proc sort won't sort it the way you want. You'll need to create a new variable that has the month in a format that will sort according to a calendar. 

 

 

Super User
Posts: 3,106

Re: proc sort

Convert your month variable to a SAS date and sort the SAS date, then your data will be in correct date order.

Super User
Posts: 6,938

Re: proc sort

And if you need only months, not complete dates (eg because you want to work with months across years), then store the months as numbers 1-12 and apply a custom format to show month names.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super Contributor
Posts: 474

Re: proc sort

[ Edited ]

Hi.

 

You could use SQL, which is more flexible to order data the way you want it.

 

Just use an available informat or create one that will create the sequence needed, for example:

 

 proc format;
         invalue MNTH (upcase)
                     'JAN'=1  'FEB'=2  'MAR'=3
                     'APR'=4  'MAY'=5  'JUN'=6
                     'JUL'=7  'AUG'=8  'SEP'=9
                     'OCT'=10 'NOV'=11 'DEC'=12
                      other=-1;
run; * create custom informat;

proc sql;
create table WANT as
select *
from HAVE
order by input(VAR,MNTH.); * order using the informat;
quit;

 

ORDER BY will accept functions and you can combine more than one variable.

 

Hope it helps.

 

Daniel Santos @ www.cgd.pt

 

Valued Guide
Posts: 797

Re: proc sort

Given the fixed and relatively limited number of values of the sort variable, this is a case where your knowledge of the data might avoid the need for proc sort, possibly saving lots of intermediate disk input/output activity:

 

 

data want;
  set have (where=(month='JAN'))
      have (where=(month='FEB'))
      have (where=(month='MAR'))
      have (where=(month='APR'))
      have (where=(month='MAY'))
      have (where=(month='JUN'))
      have (where=(month='JUL'))
      have (where=(month='AUG'))
      have (where=(month='SEP'))
      have (where=(month='OCT'))
      have (where=(month='NOV'))
      have (where=(month='DEC'))
	open=defer;
run;

 

 

 

The "open=defer" option tells SAS not to allocate memory to simultaneously creating input buffers for all 12 SET datasets.  Just use the same buffer 12 times, in sequence.   You can get away with this option if (1) all the buffers would accomodate the same variables and (2) you do not use a BY statement (which would require SAS to determine record order by comparing inputs from all sources).

 

Of course, the downside of this approach is that, even though no utility/intermediate files are created (i.e. no excess disk output), it does read dataset HAVE 12 times.   There is a tradeoff between number of sort levels and sort-utility file needs.

Ask a Question
Discussion stats
  • 7 replies
  • 260 views
  • 6 likes
  • 6 in conversation