DATA Step, Macro, Functions and more

Proc sql inline view alternative

Reply
Regular Contributor
Posts: 234

Proc sql inline view alternative

I wonder if this can code have inline view alternative ie. nested query in FROM clause?

PROC SQL;
  CREATE TABLE T1 AS 
  SELECT *
   FROM T2 
     WHERE ID NOT IN (SELECT ID FROM T3) AND
          AGE IN (11,12,13,15);
 QUIT;	       
RUN;
%PUT &SQLOBS;
Super User
Super User
Posts: 7,392

Re: Proc sql inline view alternative

Seems fine to me, whats the problem.  Test data/log output etc.

Regular Contributor
Posts: 234

Re: Proc sql inline view alternative

[ Edited ]

@RW9 I want to have all source data from coming FROM clause and use WHERE clause to subset the data after that. If the nested query in WHERE clause can be moved in FROM clause? Like below.

 

PROC SQL;
  CREATE TABLE T1 AS 
  SELECT *
   FROM (SELECT........)
     WHERE AGE IN (11,12,13,15);
 QUIT;	       
RUN;
%PUT &SQLOBS;
Super User
Posts: 17,744

Re: Proc sql inline view alternative

Yes it can be moved to the JOIN and conditioning on your WHERE statement.

 

To figure out your conditions I would suggest bringing in both of the ID's, examing the case and determing what you need to get only the rows you want.  Here's a starting point for you:

data class;
set sashelp.class;
where name not in ('Alfred', 'John');
run;

proc sql;
create table want as
select a.*, a.name as ID1, b.name as ID2
from sashelp.class as a
full join class as b 
on a.name=b.name
/*ADD WHERE CLAUSE HERE*/;
quit;
Super User
Super User
Posts: 7,392

Re: Proc sql inline view alternative

Wrap it the other way round then:

proc sql;
  create table T1 as
  select *
  from   (select *
            from (select........))
  where AGE in (11,12,13,15);
quit;

Note, you don't need the run; part.  If you can provide test data (as a datastep) examples and wht you want the output to look like we can be more accurate.

 

 

 

Regular Contributor
Posts: 234

Re: Proc sql inline view alternative

@RW9. I thought about that way. Was looking for elegant code. I have really huge and confidential data that's why I did no put it. I should have put some mock data set.

PROC SQL;
CREATE TABLE T1 AS
SELECT *
FROM (
SELECT * FROM T2
WHERE ID NOT IN (SELECT ID FROM T3)
)
WHERE
AGE IN (11,12,13,15);
QUIT;
RUN;
%PUT &SQLOBS;
Super User
Super User
Posts: 7,392

Re: Proc sql inline view alternative

Not sure what your trying to gain though, it doesn't actually change anything, just a positiiong on the code, won't save you time or processing:

proc sql;
  create table T1 as
  select  *
  from    T2
  where   ID not in (select ID from t3)
    and   age in (11,12,13,15);
quit;

The above is exactly the same, and will run the same?  

Regular Contributor
Posts: 234

Re: Proc sql inline view alternative

That makes sense.  I  also thought inline view could create virtual table and processing could have been faster.  Not sure this is correct tough.

Super User
Posts: 17,744

Re: Proc sql inline view alternative

How is this code different than your original question?

 

Did the code I provided not work?

 

Regular Contributor
Posts: 234

Re: Proc sql inline view alternative

[ Edited ]

@Reeza. The code you provided makes sense.  I have not tried your code at production yet. But will give  a shot.  Thanks !

Ask a Question
Discussion stats
  • 9 replies
  • 261 views
  • 2 likes
  • 3 in conversation