Question

if you use proc summary with class-clause, it will sort your observations in order of this class-clause.

proc summary data=One;
   by var_1;
   class var_2 var_3 var_4;
   output out = Two(drop= _freq_ _type_);
run;

1) am i right?

2) what happens, if i don't specify all fields?

proc summary data = Three(keep= var_1 var_2 var_ 3 var_4 var_5 var_6);
   by var_1;
   class var_2 var_3;
   output out = Four(drop= _freq_ _type_ );
run;

3) which proc faster: proc summary or proc sort?

Was it helpful?

Solution

A few things to note here.

  • In order to preserve the same number of rows, you need to specify the nway option in the proc summary statement. Without it you will get every 1, 2 and 3 combination of class variables.
  • I'm not sure why you have the BY statement (this obviously indicates the data is already sorted by that variable). You could just as easily include var_1 in the CLASS statement.
  • Proc Summary will sort the output in the order of the BY variables first, followed by the CLASS variables in the order they are specified.
  • This logic applies irrespective of which variables are kept.
  • Proc Sort should work faster in this simple instance, as Proc Summary will carry out further calculations that aren't required.
  • I sometimes use Proc Summary to sort and dedupe data in one step (using the maxid function), e.g. where I have multiple ID's per day and I want to only take the latest one. This saves having to sort the data and then extract the last record per day per ID.

Hope this helps.

Here is an example of my last point. Using _all_ asks to return all variables in the dataset, this does create a warning in the log for the variables previously listed in the CLASS statement, but it can be safely ignored. It's basically me being lazy in not wanting to specify the remaining variables separately for wide datasets.

data have;
input unique_id custno log_dt :datetime15.;
format log_dt datetime15.;
cards;
1 123 01jul2012:13:23
2 265 01jul2012:13:56
3 342 01jul2012:15:02
4 123 01jul2012:17:12
5 342 01jul2012:18:33
6 265 02jul2012:08:41
7 123 02jul2012:10:14
8 265 02jul2012:11:05
;
run;

proc summary data=have nway;
class custno log_dt;
format log_dt dtdate9.;
output out=want (drop=_:) maxid(log_dt(_all_))=;
run;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top