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

An error is being generated in between the order by and outer union corr lines.

I'm trying to sort dart_tot in descending order and then have 'Total' dart_tot appear on the last row.

Does anyone know how to fix this?

proc sql;

CREATE TABLE loc_sum as

SELECT loc_grp,

     sum(dart_new) as dart_tot

FROM new_file1

where dart_new=1

group by loc_grp

order by dart_tot desc

outer union corr

select 'Total' as loc_grp,

     sum(dart_new) as dart_tot

from new_file1

;

QUIT;

RUN;

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

The ORDER BY clause must be last.

If you want to order the detail rows separately from the total row then you should add another grouping variable that you can use to order.

Also did you mean to only use the WHERE clause on the detail lines?

proc sql;

create table loc_sum as

    select 0 as sort_grp

       , loc_grp

       , sum(dart_new) as dart_tot

   from new_file1

   where dart_new=1

   group by loc_grp

outer union corr

   select 1 as sort_grp

       , 'total' as loc_grp

       , sum(dart_new) as dart_tot

   from new_file1

   order by sort_grp

        , dart_tot desc

;

quit;

View solution in original post

6 REPLIES 6
Tom
Super User Tom
Super User

The ORDER BY clause must be last.

If you want to order the detail rows separately from the total row then you should add another grouping variable that you can use to order.

Also did you mean to only use the WHERE clause on the detail lines?

proc sql;

create table loc_sum as

    select 0 as sort_grp

       , loc_grp

       , sum(dart_new) as dart_tot

   from new_file1

   where dart_new=1

   group by loc_grp

outer union corr

   select 1 as sort_grp

       , 'total' as loc_grp

       , sum(dart_new) as dart_tot

   from new_file1

   order by sort_grp

        , dart_tot desc

;

quit;

PGStats
Opal | Level 21

Or, since OUTER UNION CORR keeps non-matching columns, you may let loc_grp be missing on the total line and put the 'total' label in a new sorting variable, like this:

proc sql;

create table loc_sum as

     select

          loc_grp,

          sum(dart_new) as dart_tot

     from new_file1

     where dart_new=1

     group by loc_grp

outer union corr

     select

          'total' as sort_grp,

          sum(dart_new) as dart_tot

     from new_file1

/* where dart_new=1 */

     order by

          sort_grp,

          dart_tot desc

;

quit;

Note: If you want to see the cases where dart_tot=0, you should use sum(dart_new=1) as dart_tot and remove the where clause.

PG

PG
jwillis
Quartz | Level 8

PG STATS,

I have never seen the convention function(argument equals value)  before.  What is the effect of  "sum(dart_new equals 1)" on each row of the table produced by the sql?

PGStats
Opal | Level 21

In SAS-SQL expressions, the result of a comparison operator is 1 (=TRUE) or 0 (=FALSE). Thus sum(dart_new=1) counts the number of cases where dart_new = 1. - PG

PG
jwillis
Quartz | Level 8

PG,

Please forgive my ignorance.  I am trying to understand a neat concept.  Your note and what the SAS book says are confusing me.  I understand  that any posiitve number greater than zero is "true" and zero and negative numbers are "false".  I do not understand how "sum(dart_new=1)" is equivalent to "where dart_new=1".  If I understand the SQL code above dart_new is a variable containing a numeric value of 1 or zero and the sum(dart_new) statement in the unioned query will sum all the non-missing values of dart_new.

Note: If you want to see the cases where dart_tot=0, you should use sum(dart_new=1) as dart_tot and remove the where clause.

SUM Function (SAS 9.3 Functions and Call Routines Reference Volume 2. Page 877

"Returns the sum of the nonmissing arguments".

Tom
Super User Tom
Super User

You are mostly there.

SAS interprets non-missing values other than 0 as TRUE.

What you also need to know is that when it evaluates a logic expression, such as (dart_new=1), it will return 1 when true and 0 otherwise.

So if you code SUM( (dart_new=1) ) in SQL statement then you are asking to sum the results of that logic expression. So basically you are counting the number of rows where dart_new=1.

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 5774 views
  • 4 likes
  • 4 in conversation