New SAS User

Completely new to SAS or trying something new with SAS? Post here for help getting started.
BookmarkSubscribeRSS Feed
MikeAAC
Calcite | Level 5

Hi all.

 

Assume the code below. I am using 'inobs=1000' or 'obs=1000' options on the proc sql statement to select the top 1000 records. Is there a faster way to get the same result? I thought I could use the WHERE or SELECT TOP  statement but I getting errors. 

PROC SQL;

   CONNECT TO ORACLE as EstCon

    (PATH=mypath authdomain=Oracledev);

            CREATE TABLE MyTable AS

            SELECT *

            FROM CONNECTION TO EstCon (

                 SELECT

                       t1.ID,  t2.name, t3.gender, t4.Age

                      FROM Table1 t1

                      INNER JOIN table2 t2 ON (t1.ID = t2._ID)

                      INNER JOIN table3 t6 ON (t1.ID = t3.ID)

                      INNER JOIN table4 ON (t1.PARTY_ID = t4.PARTY_ID));

             DISCONNECT FROM EstCon;

QUIT;

6 REPLIES 6
Reeza
Super User
You can use select top, but it has to be in the inner most query since it's only available in Oracle, not SAS.
MikeAAC
Calcite | Level 5

Thanks Reeza.

I tried it but I got this error msg:

ERROR: ORACLE prepare error: ORA-00923: FROM keyword not found where expected. SQL statement: SELECT TOP 100 * FROM table1 t1 inner join table2 on (t1.ID = t2.ID)...

r_behata
Barite | Level 11

The most efficient way is to filter the rows in oracle :

 

PROC SQL;

   CONNECT TO ORACLE as EstCon

    (PATH=mypath authdomain=Oracledev);

            CREATE TABLE MyTable AS

            SELECT *

            FROM CONNECTION TO EstCon (

                 SELECT 
                       t1.ID,  t2.name, t3.gender, t4.Age

                      FROM Table1 t1

                      INNER JOIN table2 t2 ON (t1.ID = t2._ID)

                      INNER JOIN table3 t6 ON (t1.ID = t3.ID)

                      INNER JOIN table4 ON (t1.PARTY_ID = t4.PARTY_ID)
				WHERE rownum  <=1000
);

             DISCONNECT FROM EstCon;

QUIT;

 

This is the alternate query to filter in sas:

PROC SQL outobs=1000;

   CONNECT TO ORACLE as EstCon

    (PATH=mypath authdomain=Oracledev);

            CREATE TABLE MyTable(obs=2) AS

            SELECT *

            FROM CONNECTION TO EstCon (

                 SELECT 

                       t1.ID,  t2.name, t3.gender, t4.Age

                      FROM Table1 t1

                      INNER JOIN table2 t2 ON (t1.ID = t2._ID)

                      INNER JOIN table3 t6 ON (t1.ID = t3.ID)

                      INNER JOIN table4 ON (t1.PARTY_ID = t4.PARTY_ID));

             DISCONNECT FROM EstCon;

QUIT;


 

r_behata
Barite | Level 11

May be try this :

 

PROC SQL;

   CONNECT TO ORACLE as EstCon

    (PATH=mypath authdomain=Oracledev);

            CREATE TABLE MyTable AS

            SELECT *

            FROM CONNECTION TO EstCon (

             SELECT * FROM (   SELECT 
                       t1.ID,  t2.name, t3.gender, t4.Age

                      FROM Table1 t1

                      INNER JOIN table2 t2 ON (t1.ID = t2._ID)

                      INNER JOIN table3 t6 ON (t1.ID = t3.ID)

                      INNER JOIN table4 ON (t1.PARTY_ID = t4.PARTY_ID)) SAMP
				WHERE rownum  <=1000
				ORDER BY rownum
);

             DISCONNECT FROM EstCon;

QUIT;
kiranv_
Rhodochrosite | Level 12

try rownum.

 

PROC SQL;

   CONNECT TO ORACLE as EstCon

    (PATH=mypath authdomain=Oracledev);

            CREATE TABLE MyTable AS

            SELECT *

            FROM CONNECTION TO EstCon (
          select * from 

               (  SELECT

                       t1.ID,  t2.name, t3.gender, t4.Age

                      FROM Table1 t1

                      INNER JOIN table2 t2 ON (t1.ID = t2._ID)

                      INNER JOIN table3 t6 ON (t1.ID = t3.ID)

                      INNER JOIN table4 ON (t1.PARTY_ID = t4.PARTY_ID))
WHERE ROWNUM <= 1000);

             DISCONNECT FROM EstCon;

QUIT;
SuryaKiran
Meteorite | Level 14

If you want to limit the results of a query to see the first 1000 where order by matters than you might use a sub-query technique additionally. 

 

SELECT *
FROM (SELECT t1.ID,  t2.name, t3.gender, t4.Age
          FROM Table1 t1
             INNER JOIN table2 t2 ON (t1.ID = t2._ID)
             INNER JOIN table3 t6 ON (t1.ID = t3.ID)
             INNER JOIN table4 ON (t1.PARTY_ID = t4.PARTY_ID)
          Order by t1.PARTY_ID)
			 
WHERE ROWNUM <=1000;
Thanks,
Suryakiran

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 5136 views
  • 0 likes
  • 5 in conversation