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
Onyx | Level 15

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



SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 3 replies
  • 531 views
  • 0 likes
  • 4 in conversation