BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SasStatistics
Pyrite | Level 9

I often find myself creating a data-set and then sorting it since for instance I want to do an visual inspection of my data. 

 

For example, with pseudo code it looks like: 

 

EDIT: When creating the data set, I do some other things such as keeping some columns and create a new one. 

* Creating my data-set; 

data MyData;
set SomeDataSet;
keep colA colB;
NewCol = ColA - ColB;
run;
* Sorting my data-set;

proc sort data = MyData; by ColumnA; run;

Now, this quickly becomes alot of code, can this be done more compactly? For instance sort the data already in the data step? Any other methods to recommend? 

Thanks. 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

If you switch from a DATA step to PROC SQL, you can do it in one step.  My SQL is rusty at best so you will need to test for syntax errors:

 

proc sql noprint;
create table MyData as
select ColA, ColB, NewCol as ColA - ColB,
from SomeDataSet, order by ColA; quit;

View solution in original post

6 REPLIES 6
andreas_lds
Jade | Level 19

The first step is not necessary at all, just use

proc sort data= SomeDataset out = MyData; 
   by ColumnA; 
run; 
SasStatistics
Pyrite | Level 9
Thanks, please view my edit, my pseudo code example was bad.
PaigeMiller
Diamond | Level 26

I think your idea of "compact code" and my idea of "compact code" are probably different.

 

If you need to do a subtraction in a DATA step, and then a sort, then these are two different processes. You can't do them both in one step * so you are left with the need to do these two different processes in two steps. NOTE: there are times when you can do two different processes in one step, this isn't one of them.

 

* — I suppose you could use hash objects to do all of this in one data step, if you want to use an advanced method.

--
Paige Miller
andreas_lds
Jade | Level 19

@SasStatistics wrote:
Thanks, please view my edit, my pseudo code example was bad.

Yes, it was 😉

 

As @PaigeMiller pointed out: one could use a hash-object to do the sorting in the data step, but you would have to write more than three lines of code to get the same result, so using easy-to-read steps is by far more desirable than having one big step.

Kurt_Bremser
Super User

You could do a sort in a data step on the fly, but it can only work as long as the complete dataset (including the B-tree) can be kept in memory, and it would be A LOT MORE code.

If you want to avoid storing an intermediate dataset, you can use your data step to define a view and use that as input in the PROC SORT, but you will still have to write the code (even a little more, as you need the /VIEW= in the DATA statement).

Astounding
PROC Star

If you switch from a DATA step to PROC SQL, you can do it in one step.  My SQL is rusty at best so you will need to test for syntax errors:

 

proc sql noprint;
create table MyData as
select ColA, ColB, NewCol as ColA - ColB,
from SomeDataSet, order by ColA; quit;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 605 views
  • 6 likes
  • 5 in conversation