BookmarkSubscribeRSS Feed
pchegoor
Pyrite | Level 9

Hello Folks,

  I have a quick question on  Data Remerging  in PROC SQL.

I was going through the Book SAS Certification Prep Guide: Advanced Programming for SAS 9, Fourth Edition    and  in   Chapter 2 (Performing Advanced Queries with PROC SQL)  under the topic Understanding Data Remerging


The book says one of the condition under which Data Remerging will occur in PROC SQL is the following :

The HAVING clause specifies one or more columns or column expressions that are not included in a subquery or a GROUP BY clause.


The same Condition is also provided here under the section : Remerging Summary Statistics

SAS(R) 9.3 SQL Procedure User's Guide



I am trying to understand the above  condition.  I understand the Data remerging  when the HAVING Clause has a Column/Expression which is not in the  GROUP BY  Clause

For eg  , the below PROC SQL will show Data Remerging because the HAVING Clause has a column Salary which is NOT in the GROUP BY Clause  assuming the dataset  sasuser.payrollmaster  has columns  JobCode  and Salary

proc sql;

       select jobcode, avg( salary) as AvgSalary format = dollar11.2

       from sasuser.payrollmaster

       group by JobCode

      having salary > avg( salary);

Quit;

But unable to understand how  the Data remerging occurs when the Column/Expression is not present in a Subquery.

Can someone  help me with a PROC SQL  example which shows Data remerging occuring if the HAVING clause has a Column/Expression which is NOT present in a Sub query.

Thanks,

Prashant

4 REPLIES 4
Reeza
Super User

Although I think this illustrates the issue, in practice it usually occurs when the variable is in the SELECT statement and not summarized or used in a HAVING/GROUP BY that you'll get the remerge warning.

proc sql;

title 'Remerging example';

select sex,  mean(weight) as weight

from sashelp.class

group by sex

having age>12;

title 'Values from original data, no HAVE clause';

select sex, age, mean(weight) as weight

from sashelp.class

group by sex;

title 'Not equivalent to WHERE';

select sex, age, mean(weight) as weight

from sashelp.class

where age>12

group by sex;

quit;

pchegoor
Pyrite | Level 9

Thanks Reeza. 

According to the Advanced SAS  Prep Guide   Data  Remerging occurs  in PROC SQL whenever any of the following conditions exist:

  1. The values returned by a summary function are used in a calculation.
  2. The SELECT clause specifies a column that contains a summary function and other column( s) that are not listed in a GROUP BY clause.
  3. The HAVING clause specifies one or more columns or column expressions that are not included in a subquery or a GROUP BY clause.

The Examples you  provided do not involve any Sub queries  but  includes  Scenario  2 above  3.

Is there a scenario where the Subquery in HAVING Clause causes Data Remerging  as mentioned by scenario 3 above?

Reeza
Super User

Where does the first example fall under?

pchegoor
Pyrite | Level 9

Reeza,

Your First Example falls under Scenario  3  above where the HAVING Clause has a Column or Expression not in the GROUP BY Clause.

I was looking for a  PROC SQL  Example which involves a Sub query  and HAVING Clause  in Scenario 3  which causes  Data Remerging.

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