BookmarkSubscribeRSS Feed
rileyd
Quartz | Level 8

Hey,

I have a Data step like follows:

Data Table3;

     Set Table1

          Table2;

Run;

I replaced it using a Sql Insert step. I decided not to use Proc Append because it would require me to use the force option. The Sql Insert option lets me defulat some fields to 0 that are not in Table2 but are in Table1. So my insert looks something like:

Proc Sql;

     Insert Into Table1

     Select Field1,

          Field2,

          0 as Field3 /* Defaulted field. */

     From Table2

     ;

Quit;

I would have expected the Sql Insert to be more efficient because I'm inserting only the records from Table2 into Table1 versus the Data step which is reading all the records in Table1 and Table2 to create Table3. But when I checked the log the CPU time for the Data step was 38.61 and the CPU time for the Sql Insert was 2:00.35.

Just curious to know why the Sql Insert appears to be so much less efficient.

Thanks!

-Andrew

15 REPLIES 15
blom0344
Calcite | Level 5

What is the CPU time of:

proc sql;

create table3 as

select t.* from table1 t

union

select v.* from table2 v;

quit;

since this closely resembles building a table from scratch as the datastep does..

PGStats
Opal | Level 21

Good suggestion, but that should be:

proc sql;

create table table3 as

select * from table1

union all corresponding

select *, 0 as Field3 from table2;

quit;

PG

PG
blom0344
Calcite | Level 5

Yes, you are right. My idea was to point to the use of set operators. I wasn't sure SAS supports union all, so I sticked to the union

Cynthia_sas
SAS Super FREQ

Hi:

  In the Programming 3 class, we discuss these kinds of efficiencies and illustrate the proper way to do benchmarking. Here's the description of the Programming 3 class: SAS Training in the U.S. -- SAS Programming 3: Advanced Techniques and Efficiencies

cynthia

rileyd
Quartz | Level 8

Thanks Cynthia. I have the Programming 3 book so I'll look into doing some benchmarking as outlined in it. I guess I was a little surprised in general that the Sql Insert wasn't less CPU intensive. In the past when I've replaced similar Data steps with Proc Append the CPU difference (improvement) is typically noticeable and significant (2-3 times faster). I assumed with the Sql Insert I would see similar results so when the CPU actually increased I was surprised.

rileyd
Quartz | Level 8

The real time increased as well although I generally don't pay much attention to real time. They're not unreliable when it comes to determining the efficiency of a program.

Kurt_Bremser
Super User

For me, efficiency means the time spent to achieve a result. 100 CPU seconds in 100 seconds real time is therefore more efficent than 5 seconds CPU time in 10 minutes real time.

Granted that real time depends very much on what else is going on in a certain system at a certain time, so you need more than one run to come to a valid conclusion.

SAS SQL is often a notorious I/O resource hog (which reflects mainly in real time), that's why I asked the question.

Watch which files (work directory, utility location) are created during the steps and to what size they grow.

LinusH
Tourmaline | Level 20

PROC APPEND should be the most efficient technique.

Perhaps you could assign 0 to field3 when creating Table2?

Data never sleeps
rileyd
Quartz | Level 8

That's not an option in this situation. Thanks for the suggestion though Linus.

Howles
Quartz | Level 8

What I don't know is how the SQL statement is optimized by PROC SQL.

Maybe the SELECT is evaluated and the result set is spooled somewhere. Then the rows are batch-inserted into the target table. That seems to be the original poster's theory.

Maybe the SELECT is evaluated and as each row is generated it triggers a single-row insertion operation. That's my hunch.

The results will of course be the same either way, but the incurrence of overhead could differ.

ballardw
Super User

A possibly obnoxious confounder in some of this is SAS will attempt to keep some of the data in memory. So sometimes a second test involving the same data set(s) may run quicker just because it isn't being read from disk.

Kurt_Bremser
Super User

Any operating system that deserves to be called one will do caching on its own, so the second operation on the same data will usually be faster. A thorough test needs to clean the system cache out first and run the compared steps in reverse order to allow an educated guess about efficiency.

PGStats
Opal | Level 21

I suspect that the default UNDO_POLICY=REQUIRED option forces SAS to do the inserts one at a time. Setting UNDO_POLICY=NONE may improve performance significantly.

PG

PG

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
  • 15 replies
  • 3249 views
  • 7 likes
  • 8 in conversation