BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SAS_inquisitive
Lapis Lazuli | Level 10

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;
1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

6 REPLIES 6
mohamed_zaki
Barite | Level 11
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;
DanZ
Obsidian | Level 7
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.

Astounding
PROC Star

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.

SAS_inquisitive
Lapis Lazuli | Level 10

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.

Astounding
PROC Star

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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-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!

How to Concatenate Values

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.

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
  • 6 replies
  • 1430 views
  • 6 likes
  • 5 in conversation