BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Junyong
Pyrite | Level 9

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

Accepted Solutions
Tom
Super User Tom
Super User

"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;

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

"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;
ChrisNZ
Tourmaline | Level 20

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;
yabwon
Amethyst | Level 16

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 788 views
  • 0 likes
  • 4 in conversation