This code gives the deired output data set but throws error in the log.
data _null_; set sashelp.class; if _n_=1 then call execute("data test; set sashelp.class;"); call execute("if AGE < 15 then output;"); call execute("run;"); run;
Unfortunately, CALL EXECUTE doesn't do this. A simple SAS program would, however:
data test;
set sashelp.class;
if age > 15 then preteen='YES';
else preteen='NO';
run;
CALL EXECUTE would only be useful if you wanted the program to change, depending on the contents of a SAS data set. (In the case above, the program doesn't change at all.) For example, suppose you wanted to code only one of these:
data test;
set sashelp.class;
preteen='YES';
run;
data test;
set sashelp.class;
preteen='NO';
run;
Further, suppose that you wanted SAS to generate one DATA step or the other depending on the largest value of AGE. You could conceivably code:
proc sort data=sashelp.class;
by age;
run;
data _null_;
set sashelp.class end=done;
if _n_=1 then call execute('data test; set sashelp.class;');
if done;
if age > 15 then call execute("preteen='YES';");
else call execute("preteen='NO';");
call execute('run;');
run;
It's just a simple to example to illustrate ... the DATA step that SAS sees will change, depending on the largest value of AGE.
data _null_;
set sashelp.class;
if _n_=1 then do;
call execute("data test; set sashelp.class;");
call execute("if AGE < 15 then output;");
call execute("run;");
end;
run;
data _null_;
set sashelp.class;
if _n_=1 then
call execute(" data test;
set sashelp.class;
if AGE < 15 then output;
run;");
run;
Looks like the entire data step must be in the same execute function.
Perhaps easiest of all:
data _null_;
call execute("data test; set sashelp.class;"); call execute("if AGE < 15 then output;"); call execute("run;"); run;
There is no need to read SASHELP.CLASS. You can use CALL EXECUTE without it.
CALL EXECUTE is usually helpful when you need data from a SAS data set to construct the program. Here, you don't so the program becomes much simpler.
May be I did not code correctly what I mean. I want to perform different operations based on the values of the Age in the sashelp.class. Let's say if age<15 create varialbe pre-teen="YES" and if age>15, pre-teen="NO" with the use of call execute.
Unfortunately, CALL EXECUTE doesn't do this. A simple SAS program would, however:
data test;
set sashelp.class;
if age > 15 then preteen='YES';
else preteen='NO';
run;
CALL EXECUTE would only be useful if you wanted the program to change, depending on the contents of a SAS data set. (In the case above, the program doesn't change at all.) For example, suppose you wanted to code only one of these:
data test;
set sashelp.class;
preteen='YES';
run;
data test;
set sashelp.class;
preteen='NO';
run;
Further, suppose that you wanted SAS to generate one DATA step or the other depending on the largest value of AGE. You could conceivably code:
proc sort data=sashelp.class;
by age;
run;
data _null_;
set sashelp.class end=done;
if _n_=1 then call execute('data test; set sashelp.class;');
if done;
if age > 15 then call execute("preteen='YES';");
else call execute("preteen='NO';");
call execute('run;');
run;
It's just a simple to example to illustrate ... the DATA step that SAS sees will change, depending on the largest value of AGE.
You have to think about it as a process:
data _null_; set sashelp.class;
These two lines are self explanatory, it sets each row in the dataset sashelp.class iteratively. Then for each row returned from that dataset the condition is checked:
if _n_=1 then call execute("data test; set sashelp.class;");
As there is only one _n_ then this row gets called once and puts the code below into the compiler after the dataset finishes executing.
data test; set sashelp.class;
Which is probably what you want. The next two statements are not part of the conditional:
call execute("if AGE < 15 then output;"); call execute("run;");
So these two rows execute for every row of dataset, so (assuming there is 3 rows in sashelp.class) the following code is sent to the compiler:
if AGE < 15 then output; run; if AGE < 15 then output; run; if AGE < 15 then output; run;
You will note that after the first run; the rest of the code becomes illegal.
What is it your trying to do, as @Astounding has stated there is no need to supply a dataset if you want code run once - as a datastep is a loop, each row being pulled in iteratively. TBH though, from the code you have posted there doesn't appear to be any reason to do this in the first place. Maybe if you post an example of what you are trying to do.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.