## Splitting the compacted SQL into simple two pieces?

Solved
Super Contributor
Posts: 338

# Splitting the compacted SQL into simple two pieces?

Hi SAS Forum,

Another worker has written below compacted code.

/*COMPACTED CODE*/

%let age0=1303;

%let yymm=1303 1304 1305 1306 1307  ;

proc sql;

create table OUR_age0 as

select               a.*,

b.LOAN_LIMIT,

b.DEPOSIT,

"&age0" as year_month

from   (select       a.*

from   LOCATION.FILE_1    as             a inner join

ABCD.INCOME_&age0  as             b on

a.ACT_NO=b.ACCOUNT_NUMBER

where b.STATUS^="CLOSED")    as            a inner join

ABCD.EXPENSES_&age0      as b on a.ACT_NO=b.ACCOUNT_NUMBER

;

quit;

/*I HAVE SPLIT ABOVE COMPACTED CODE INTO simple 2 PIECES*/

/*PIECE 1 – YELLOW PORTION ABOVE*/

Proc sql;

Create table any_name as

select        a.*

from   LOCATION.FILE_1      as            a inner join

ABCD.INCOME_&age0    as            b on

a.ACT_NO=b.ACCOUNT_NUMBER

where b.STATUS^="CLOSED";

quit;

run;

/*PIECE 2*/

proc sql;

create table OUR_age0_testing as

select               a.*,

b.LOAN_LIMIT,

b.DEPOSIT,

"&age0" as year_month

from   any_name             as            a inner join

ABCD.EXPENSES_&age0        as            b on

1. a.ACT_NO=b.ACCOUNT_NUMBER

;

Quit;

Run;

/*QUESTION*/

Final files produced by the compacted code and split two pieces have to be identical but following shows it is not.

proc compare base=Our_age0_testing compare=Our_age0 ;run;

Could anyone help me to split the compacted code correctly?

Thanks

Accepted Solutions
Solution
‎05-09-2014 04:38 PM
Super Contributor
Posts: 282

## Re: Splitting the compacted SQL into simple two pieces?

You are asking how to re-write the query for testing purposes, while my answer was primarily related to Parsing the entire query, from a SQL syntax point of view!

Here is what I think the correct re-write should be:

%let age0=1303;

%let yymm=1303 1304 1305 1306 1307  ;

PROC SQL;

CREATE TABLE Table1 AS /* Original In-Line Query named as Table1 */

SELECT    a.*

FROM    LOCATION.FILE_1 as a inner join ABCD.INCOME_&age0 AS b

ON    a.ACT_NO=b.ACCOUNT_NUMBER

WHERE b.STATUS^="CLOSED";

CREATE TABLE OUR_age0_testing AS

SELECT    a.*,

b.LOAN_LIMIT,

b.DEPOSIT,

"&age0" as year_month

FROM        Table1 /* Using Table1 as a replacement to the original In-Line Query */

INNER JOIN ABCD.EXPENSES_&age0 AS b

ON a.ACT_NO=b.ACCOUNT_NUMBER;

QUIT;

Thanks,

Ahmed

All Replies
Super Contributor
Posts: 282

## Re: Splitting the compacted SQL into simple two pieces?

I would recommend using Regular Expression to extract the "Compacted Code" into separate file.

That could be one solution to your question.

You are asking for help in Parsing standard SQL query syntax.

Alternatively, search the Web for Open Source SQL Query Parser.

Good luck,

Ahmed

Super Contributor
Posts: 338

## Re: Splitting the compacted SQL into simple two pieces?

Hi Ahmed,

However, my level is not enough to understand what you suggested. I googled what is "Regular Expression". It looks like an another entire areas which I have not even touched.

Could you or someone help me to understand what is the wrong I have done in splitting the compacted code (which has a sub query) into the two simple pieces?

Thanks for the expertise and time.

Solution
‎05-09-2014 04:38 PM
Super Contributor
Posts: 282

## Re: Splitting the compacted SQL into simple two pieces?

You are asking how to re-write the query for testing purposes, while my answer was primarily related to Parsing the entire query, from a SQL syntax point of view!

Here is what I think the correct re-write should be:

%let age0=1303;

%let yymm=1303 1304 1305 1306 1307  ;

PROC SQL;

CREATE TABLE Table1 AS /* Original In-Line Query named as Table1 */

SELECT    a.*

FROM    LOCATION.FILE_1 as a inner join ABCD.INCOME_&age0 AS b

ON    a.ACT_NO=b.ACCOUNT_NUMBER

WHERE b.STATUS^="CLOSED";

CREATE TABLE OUR_age0_testing AS

SELECT    a.*,

b.LOAN_LIMIT,

b.DEPOSIT,

"&age0" as year_month

FROM        Table1 /* Using Table1 as a replacement to the original In-Line Query */

INNER JOIN ABCD.EXPENSES_&age0 AS b

ON a.ACT_NO=b.ACCOUNT_NUMBER;

QUIT;

Thanks,

Ahmed

Super Contributor
Posts: 338

Hi Ahmed,