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

Hi,

I am running a do until condition and I want the output at the end of each iteration to be added to the same dataset ie.

 

%macro ..;

%do %until ();

more processing....

data x y;

if ...output x;

else output y;

end;

%mend.

 

What I am looking to do is at the end of each do until iteration, one dataset that meets the condition is generated and I want to keep adding records to it as I go through the do loop in each iteration. How is this possible?

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Each loop iteration is going to replace both data sets X and Y as currently written. You may need to decide on desired data set names for the final result. Then us Proc Append to add x and y to the final set within each loop.

Something conceptually like:

%macro ..;
   %do %until ();
   /*more processing....*/
      data x y;
         if ...output x;
         else output y;
      run;

      proc append base=finalx data=x;
      run;
      Proc append base=finaly data=y;
      run;
   %end;
%mend.

Note, your current data steps would not have generated anything as there is nothing providing values that I can see, such as a SET statement.

This assumes that the variables in the X and Y data sets will be the same name, type and length each time. Otherwise you really need to provide examples as appending random stuff runs into issues of variable type compatibility and lengths of character variables.

 

It is an extremely good idea in macro programming to explicitly end every data step or procedure properly with Run or Quit statements.

 

 

You really should provide some example data and what you expect the output to look like.

View solution in original post

2 REPLIES 2
ballardw
Super User

Each loop iteration is going to replace both data sets X and Y as currently written. You may need to decide on desired data set names for the final result. Then us Proc Append to add x and y to the final set within each loop.

Something conceptually like:

%macro ..;
   %do %until ();
   /*more processing....*/
      data x y;
         if ...output x;
         else output y;
      run;

      proc append base=finalx data=x;
      run;
      Proc append base=finaly data=y;
      run;
   %end;
%mend.

Note, your current data steps would not have generated anything as there is nothing providing values that I can see, such as a SET statement.

This assumes that the variables in the X and Y data sets will be the same name, type and length each time. Otherwise you really need to provide examples as appending random stuff runs into issues of variable type compatibility and lengths of character variables.

 

It is an extremely good idea in macro programming to explicitly end every data step or procedure properly with Run or Quit statements.

 

 

You really should provide some example data and what you expect the output to look like.

Tommer
Obsidian | Level 7
This worked!! I used proc append. I think I need to work on my macro logic better but what i was looking for from this query worked! Thank you so so much!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 2 replies
  • 495 views
  • 0 likes
  • 2 in conversation