The following uses a data set HAVE to create three variables JACK JAMES JOHN in a data set WANT.
data have;
input name $;
cards;
Jack
James
John
;
data want;
run;
data _null_;
set have;
call execute("data want;set want;"||name||"=1;run;");
run;
So, this is equivalent to
data want;
run;
data want;
set want;
Jack=1;
run;
data want;
set want;
James=1;
run;
data want;
set want;
John=1;
run;
But, can I iterate one specific row via CALL EXECUTE using the data as follows?
data want;
Jack=1;
James=1;
John=1;
run;
"iterate" is not the right word here.
The word you are looking for is CONDITIONAL. As in how can I conditionally generate code using call execute. In this specific case you need to generate special code before the first observation. You could also generate code after the last observation, but that is is not really required as you could always just type that code directly into the program after the data step.
Your final data step is NOT the same as the previous example as it is skipping the SET WANT statement. But since you made want as a dataset with one observation and no variables there will not be any difference. However is there was some other dataset WANT that you want to modify then the SET statement would make a difference.
To test if you are on the first iteration of the data step use the _N_ automatic variable. To test if you are on the last use the END= option on the SET statement.
data _null_;
set have end=eof;
if _n_=1 then call execute('data want; set want;');
call execute(cats(name,'=1;'));
if eof then call execute('run;');
run;
"iterate" is not the right word here.
The word you are looking for is CONDITIONAL. As in how can I conditionally generate code using call execute. In this specific case you need to generate special code before the first observation. You could also generate code after the last observation, but that is is not really required as you could always just type that code directly into the program after the data step.
Your final data step is NOT the same as the previous example as it is skipping the SET WANT statement. But since you made want as a dataset with one observation and no variables there will not be any difference. However is there was some other dataset WANT that you want to modify then the SET statement would make a difference.
To test if you are on the first iteration of the data step use the _N_ automatic variable. To test if you are on the last use the END= option on the SET statement.
data _null_;
set have end=eof;
if _n_=1 then call execute('data want; set want;');
call execute(cats(name,'=1;'));
if eof then call execute('run;');
run;
A variation:
data _null_;
set HAVE nobs=NOBS;
if _N_=1 then call execute('data WANT; set WANT;');
call execute(cats(NAME,'=1;'));
if _N_=NOBS then call execute('run;');
run;
one more variation:
data have;
input name $;
cards;
Jack
James
John
;
run;
data want;
run;
data _null_;
call execute('data want; set want;');
do until(eof);
set have end=eof;
call execute(cats(name,'=1;'));
end;
call execute('run;');
stop;
run;
Bart
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.