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

Hello everyone,

 

I wonder may I ask why in the output of the code below,

the average value of "Jones" which is 100,

would be ordered higher than that for "Smith", which is 400.

 

Thank you very much!

 

data work.one;
  input Rep $ Cost;
  cards;
  Smith 200
  Smith 400
  Jones 100
  Smith 600
  Jones 100
  Chang 400
  Jones .
  Chang 600
  ;

proc sql;
  select Rep, avg(Cost) as Average
  from one
  group by Rep
  having Average ge 100
  order by Rep
  ;
  quit
  ;
  
1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

You have the expression

   "order by rep"

which tells sql to sequence results sorted by rep  (change, jones, smith).

 

But even if you drop this expression, you also have "group by rep".   proc sql needs a way to establish those groups, and it seems that sql is using alphabetic order to determine how to assign an individual row to a group.

 

So you need to order by the new variable average.

  "order by average"

or 
  "order by average descending"

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

2 REPLIES 2
mkeintz
PROC Star

You have the expression

   "order by rep"

which tells sql to sequence results sorted by rep  (change, jones, smith).

 

But even if you drop this expression, you also have "group by rep".   proc sql needs a way to establish those groups, and it seems that sql is using alphabetic order to determine how to assign an individual row to a group.

 

So you need to order by the new variable average.

  "order by average"

or 
  "order by average descending"

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
jc3992
Pyrite | Level 9

Thanks!!

Yes, I found that later on that it used "alphabetic" order. Thank you!!!

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
  • 2 replies
  • 533 views
  • 0 likes
  • 2 in conversation