Date: Thu, 20 Aug 2009 21:31:10 -0400
Reply-To: Ian Whitlock <iw1sas@GMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Ian Whitlock <iw1sas@GMAIL.COM>
Subject: Question in CALL EXECUTE
Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes
Summary: Understanding the pattern for simple CALL EXECUTE code
#iw-value=1
Ramsathish,
CALL EXECUTE is for generating code.
If you write
data _null_;
set sashelp.class
call execute("proc print data = class noobs; run;");
run ;
Then the same PROC PRINT CODE will be generated as many times as there
are records in SASHELP.CLASS. This, of course, is not very reasonable
and thus indicates a big misunderstanding of how to use the code
generator.
Compare this with
data _null_ ;
set sashelp.vtable ;
call execute
("proc print data = WORK."|| memname|| "obs=5;"
||"where libname = 'WORK'; run;");
run ;
Now we have something useful a sample print from all the data sets in
WORK at the time the code is generated. Change 'WORK' to 'SASHELP'
and we can look at 5 obs in each data set of SASHELP.
The lesson here is that CALL EXECUTE is handy for generating
repetitive code, but you need something worth repeating. Printing the
same data set over and over is not useful repetition.
Now consider the renaming problem posted today by rahul alawadhi
<rahulalawadhi@GMAIL.COM> under the subject: renaming variables. He
wanted to rename hundreds of variables beginning with "OF_" to remove
the "OF_". We might hit upon PROC DATASETS to do the renaming but we
do not want to call the proc hundreds of times.
proc datasets lib = mess ;
modify example ;
rename .... ;
run ;
quit ;
Here the repetion is indicated by the dots. Aha, we need some constant
code in the front and some more in the back, with repetition in the
middle.
data _null_ ;
if _n_ = 1 then
call execute ("proc datasets lib = mess; modify example; rename
") ;
if eof then
call execute ("; run; quit;") ;
set control end = eof ;
if upcase(name) =: "OF_" then
call execute (cats (name, "=", substr(name,4)) ) ;
run ;
Where did CONTROL come from? I'll leave it to you to decide how to
create
CONTROL. One thing you can be sure is that CONTROL is not EXAMPLE.
Why?
You might take a look at "The Pragmatic Programmer: From Journeyman to
Master"
by Andrew Hunt and David Thomas. It is not language specific but it
has a
wonderful chapter on the role of code generators. Or more
specifically, search
for "call execute" at http://lexjansen.com/sugi/.
Ian Whitlock
==================
Date: Mon, 17 Aug 2009 05:11:40 -0700
Reply-To: RAMS <ramsathish@GMAIL.COM>
Subject: Question in CALL EXECUTE
Dear all,
i am just wondering why call execute creates same table for
number of observations in the dataset. I know if i use _n_ = 1 then
call execute to get only one output.
However how I can get only one output for each if condition without
using macros, when I write a syntax like below,
data class;
set sashelp.class
if sex = M then call execute("proc print data = class noobs; run;");
else call execute("proc means data = class; var weight;run;");
run;
Thanks in advance,
Regards,
Ramsathish S