BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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.
3 REPLIES 3
Cynthia_sas
SAS Super FREQ
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
deleted_user
Not applicable
Hi Cynthia.

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

Regards
april
Cynthia_sas
SAS Super FREQ
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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

New Learning Events in April

 

Join us for two new fee-based courses: Administrative Healthcare Data and SAS via Live Web Monday-Thursday, April 24-27 from 1:00 to 4:30 PM ET each day. And Administrative Healthcare Data and SAS: Hands-On Programming Workshop via Live Web on Friday, April 28 from 9:00 AM to 5:00 PM ET.

LEARN MORE

Discussion stats
  • 3 replies
  • 947 views
  • 0 likes
  • 2 in conversation