BookmarkSubscribeRSS Feed
BrahmanandaRao
Lapis Lazuli | Level 10
i want sort the Population from highest to lowest in datastep
proc import datafile ="C:\Users\Anand\Desktop\country_wise_population.xlsx" 
out=countries
dbms=xlsx	;
run;


data population;
set countries;
by  descending Population		;
run;
	
15 REPLIES 15
PaigeMiller
Diamond | Level 26

@BrahmanandaRao wrote:
i want sort the Population from highest to lowest in datastep
proc import datafile ="C:\Users\Anand\Desktop\country_wise_population.xlsx" 
out=countries
dbms=xlsx	;
run;
	

Use PROC SORT instead of a data step.

 

proc sort data=countries;
    by descending population;
run;
--
Paige Miller
BrahmanandaRao
Lapis Lazuli | Level 10

using datastep not in proc step

PaigeMiller
Diamond | Level 26

It's a lot more difficult (if it is even possible at all) to sort using a DATA step. 

--
Paige Miller
PeterClemmensen
Tourmaline | Level 20

It is rarely a good idea to do this directly in the Data Step. However, if it has to be in a data step, you can use a hash object like this

 

data _null_;
    if 0 then set countries;
    declare hash h(dataset:'countries', ordered:'d');
    h.definekey('Population');
    h.definedata(all:'Y');
    h.definedone();

    h.output(dataset:'want');
run;
PaigeMiller
Diamond | Level 26

@PeterClemmensen wrote:

It is rarely a good idea to do this directly in the Data Step. However, if it has to be in a data step, you can use a hash object like this

 

data _null_;
    if 0 then set countries;
    declare hash h(dataset:'countries', ordered:'d');
    h.definekey('Population');
    h.definedata(all:'Y');
    h.definedone();

    h.output(dataset:'want');
run;

@PeterClemmensen Yes, indeed it is rarely a good idea to write your own code to replicate what SAS has already programmed.

 

So to @BrahmanandaRao Why would anyone want to use an advanced technique like hash objects, when a beginner method like PROC SORT is available which does the same thing and probably does it a lot faster than hash objects?

--
Paige Miller
BrahmanandaRao
Lapis Lazuli | Level 10
NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.00 seconds 211 h.definedata(all:'Y'); 212 h.definedone(); 213 214 h.output(dataset:'want'); 215 run; 216 data _null_; 217 if 0 then set countries; 218 declare hash h(dataset:'countries', ordered:'d'); - 567 219 h.definekey('Population'); ----------- 557 ERROR: DATA STEP Component Object failure. Aborted during the COMPILATION phase. ERROR 567-185: Variable H already defined. ERROR 557-185: Variable h is not an object. NOTE: The SAS System stopped processing this step because of errors. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.00 seconds 220 h.definedata(all:'Y'); 221 h.definedone(); 222 223 h.output(dataset:'want'); 224 run;
PeterClemmensen
Tourmaline | Level 20

0.00 sec .. That is one fast sort  😉

 

Seems like your data set has a variable named h which conflicts with the name of the hash object. Needless to say, my code is untested as I can not see your data.

 

Also, I second @Tom and @PaigeMiller.. Just use PROC SORT 🙂

Tom
Super User Tom
Super User

@BrahmanandaRao wrote:

using datastep not in proc step


Why?  There is no good reason I can think of that would require you to implement a sorting algorithm in a data step.

If you want to process the data in a specific order then sort it first and then run your data step.

If you really do not have the space to save a sorted copy of the data you might want to create a PROC SQL view that sorts the data and read from that view.  But watch out for the space used in the UTIL folders.

proc sql;
create view country_pop_view as
 select *
 from countries
 order by desc population
;
quit;

data want;
  set country_pop_view ;
  by population descending;
  ...
run;
BrahmanandaRao
Lapis Lazuli | Level 10

Error this code

Tom
Super User Tom
Super User

@BrahmanandaRao wrote:

Error this code


?? 

There are probably errors since I just typed sample code into the forum instead of running it in SAS.

 

If you want fully worked example code you need to provide sample data in the form of a data step that can be run to create the dataset.

ballardw
Super User

@BrahmanandaRao wrote:

using datastep not in proc step


 

Provide a valid reason.

 

Saying that data step must be used is like saying you have to use a teaspoon to dig a ditch instead of backhoe or trencher or other machine.

 

Proper tool for the task.

 

Or since you starting with a spreadsheet use the spreadsheet tools to sort the data before import. I notice that the data is sorted by decreasing population already. So why do you even think you need to sort the data????

 

 

PaigeMiller
Diamond | Level 26

@ballardw wrote:

@BrahmanandaRao wrote:

using datastep not in proc step


 

Provide a valid reason.


By example, @BrahmanandaRao seems to have provided a valid reason for doing this in PROC SORT. Had he done that, there would be no errors in his code, it would work the first time, and he'd be done and moved on to whatever is next over 2 hours ago.

--
Paige Miller
BrahmanandaRao
Lapis Lazuli | Level 10

Maximum 14 means

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 15 replies
  • 2014 views
  • 5 likes
  • 6 in conversation