Help with Proc Sort

Reply
N/A
Posts: 0

Help with Proc Sort

Hi Everyone...

How to code a proc sort on dataset which has state, district and county as primary variabes along with several numeric variables.

Thanx in advance.
SAS Super FREQ
Posts: 8,647

Re: Help with Proc Sort

Hi:
Let's say you have some data, you want to work with that data and you know that the data is currently ordered by REGION, but you need the data ordered by YEAR. So, ou would use PROC SORT to do the reordering:
[pre]
proc sort data=perm.salesdata;
by year region;
run;
[/pre]

would sort the data "in place" -- that means that by the time the sort was done, perm.salesdata would have been reorganized and would now be in order by year and then region within year.

But, if you had this:
[pre]
proc sort data=perm.salesdata out=work.sales_year;
by year region;
run;
[/pre]
then...the "original" file perm.salesdata would still be in the "original" order but the new (and temporary) file work.sales_year would be the same data, only ordered by year and then region within year.

In the above 2 instances, the BY statement is telling PROC SORT how to order the variables...so right now, I have BY YEAR REGION; which would order by YEAR first, then by REGION within YEAR.

If I had BY REGION YEAR; then I would get ordering by REGION first and then by YEAR within each region.

For more information PROC SORT, such as eliminating duplicates or sorting in descending order, I recommend the SORT documentation or you might consider contacting Tech Support for more help.

cynthia
N/A
Posts: 0

Re: Help with Proc Sort

Hi Cynthia.

Thanx for the reply but i still want to know that do we sort the dataset based on primary variables?

Regards
april
SAS Super FREQ
Posts: 8,647

Re: Help with Proc Sort

Hi,
It seems that I did not undertand your question. In your original post, you asked "how to code a proc sort". So, I answered with several examples of PROC SORT syntax using a hypothetical data example.

PROC SORT has very simple syntax and you may list as many or as few variables on the BY statement as you want or need to -- based on the particular report or process that you are going to produce. For example, if you have data and you were going to use PROC SQL with the data, then I would not bother to use PROC SORT first, because PROC SQL can do an "ORDER BY". On the other hand, if you were going to use PROC PRINT with a PAGEBY and/or BY statement, you would need to use PROC SORT first to get the data in the right order for PROC PRINT.

For help with this question, your best bet is to contact Tech Support.

cynthia
Post a Question
Discussion Stats
  • 3 replies
  • 322 views
  • 0 likes
  • 2 in conversation