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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

3 REPLIES 3
Astounding
PROC Star

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.

ballardw
Super User

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.

Sinistrum
Quartz | Level 8

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.

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!

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
  • 3 replies
  • 1036 views
  • 2 likes
  • 3 in conversation