DATA Step, Macro, Functions and more

call execute error

Accepted Solution Solved
Reply
Regular Contributor
Posts: 234
Accepted Solution

call execute error

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;

Accepted Solutions
Solution
‎01-25-2016 03:26 PM
Super User
Posts: 5,099

Re: call execute error

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.

View solution in original post


All Replies
Super Contributor
Posts: 490

Re: call execute error

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;
Contributor
Posts: 38

Re: call execute error

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.

Super User
Posts: 5,099

Re: call execute error

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.

Regular Contributor
Posts: 234

Re: call execute error

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.

Solution
‎01-25-2016 03:26 PM
Super User
Posts: 5,099

Re: call execute error

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.

Super User
Super User
Posts: 7,433

Re: call execute error

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.

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 281 views
  • 6 likes
  • 5 in conversation