DATA Step, Macro, Functions and more

proc means - "class" instead of "by" - really that easy?

Accepted Solution Solved
Reply
Contributor
Posts: 35
Accepted Solution

proc means - "class" instead of "by" - really that easy?

Dear community,

 

this time, I would be glad if you could help me with a very embarrassing issue indeed.

 

The few months I use SAS, I always utlized

proc means data = input noprint;
by groupvar1 groupvar2;
var x;
output out = means_output
Mean(x) = mean_x;
run;

"The thing" with by is: I have to sort the data a priori.

 

Now, here, I read:

proc means data = input noprint;
class groupvar1 groupvar2;
var x;
output out = means_output
Mean(x) = mean_x;
run;

No sorting necessary.

 

This would imply an enormous time advantage, for the sorting of the colossi takes most of the time.

 

So, my question is, is it really that simple?

As I said, it is an embarrassing question, but I am scared to destroy the anyhow 'fragile' and dilettante, programs of mine.

 

Yours sincerely,

Sinistrum


Accepted Solutions
Solution
‎09-02-2016 11:08 AM
Super User
Posts: 5,074

Re: proc means - "class" instead of "by" - really that easy?

Yes, you can avoid sorting.  But there are things to watch out for.

 

If you were to test these programs, you would see that the number of records in the output data sets changes.  To get them to be the same, you would have to add the word NWAY to the second PROC MEANS statement.  Without it, PROC MEANS with a CLASS statement generates the same summaries as the first PROC MEANS, plus additional summaries for the entire data set, for each value of GROUPVAR1, and for each value of GROUPVAR2.  Of course those may be helpful, but it adds complications to the structure of your output data set that you have to be aware of.

 

Secondly, if you have formats connected with either GROUPVAR1 or GROUPVAR2, the results can be different.

 

 

So the bottom line is that you can achieve those savings, but you have to understand when the results would be different.

View solution in original post


All Replies
Solution
‎09-02-2016 11:08 AM
Super User
Posts: 5,074

Re: proc means - "class" instead of "by" - really that easy?

Yes, you can avoid sorting.  But there are things to watch out for.

 

If you were to test these programs, you would see that the number of records in the output data sets changes.  To get them to be the same, you would have to add the word NWAY to the second PROC MEANS statement.  Without it, PROC MEANS with a CLASS statement generates the same summaries as the first PROC MEANS, plus additional summaries for the entire data set, for each value of GROUPVAR1, and for each value of GROUPVAR2.  Of course those may be helpful, but it adds complications to the structure of your output data set that you have to be aware of.

 

Secondly, if you have formats connected with either GROUPVAR1 or GROUPVAR2, the results can be different.

 

 

So the bottom line is that you can achieve those savings, but you have to understand when the results would be different.

Super User
Posts: 10,466

Re: proc means - "class" instead of "by" - really that easy?

There is a very useful feature, depending on how you use the resulting data with the Class that you do not get with By.

In your example with: class groupvar1 groupvar2

without the NWAY you will get a summary of All var values, vars within each level of groupvar1, vars withing each level of groupvar2 and vars within each combination of values of groupvar1 and groupvar2. There is an automatic variable _type_ that indicates which one.

 

So with one pass through Proc means or Summary you have 4 summaries that can be selected for print or input into another procedure using where _type_ =1 or similar.

I do this one project where I report on overall, 6 different variables at single levels and combinations of 2 or in some cases 3 of them.

One pass through Proc summary and then feed the results into a bunch of Proc Tabulate or Proc Print with where clauses.

Contributor
Posts: 35

Re: proc means - "class" instead of "by" - really that easy?

Thank you very much for your responses (and not yelling at me).
As is see it now, via the "_type_" variables, I may get the data set I used to get with the proc means by statement - but with no sorting in beforehand.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 275 views
  • 2 likes
  • 3 in conversation